I have several reports in my ASP.NET MVC application that are date-oriented. I had start date and end date text boxes, and things worked fine. What I really wanted was something that would allow those date boxes to be populated with a variety of canned date ranges (e.g. Year to Date, Last Month, etc.). I found just what I was looking for in a pure JavaScript implementation
here.
The one problem I found was that "Last Month" was being calculated incorrectly. Below is the simple fix. Thanks to epalla for the original code.
// last month
case "lastmo":
// we need a new month variable for month-1, also formatted correctly
var lastmonth = ((month - 1) < 10) ? "0" + (month - 1) : (month - 1);
startbox.value = lastmonth + "/01/" + year;
// now grab the last day of the month (30, 31? we don't know!)
var moend = new Date(year, (month - 1), 0);
endbox.value = lastmonth + "/" + moend.getDate() + "/" + year;
break;