I have two datepicker fields (telerik, not jquery UI) and also a radio button list containing buttons for week, month, year, etc.
The user can either select a date range using the two datepickers, or alternatively, they can click one of the radio buttons and the fields should populate based on their choice.
So if the user selects week, the end date should be today and the start date should be 7 days ago.
What I have at the moment is this:
$(function () {
$("#dateRange_week").click(function () {
var now = new Date();
var startDate = now;
$("#StartDate").val(startDate);
$("#EndDate").val(now);
});
});
currently the jQuery inserted dates are strings formatted as follows
Tue Oct 02 2012 12:08:01 GMT-0400 (Eastern Daylight Time)
- How do I calculate startDate as now - 7 days?
- How can I format the dates as mm/dd/yyyy?
EDIT___
The fix: Using Date.js as per the accepted answer below, the jQuery becomes
$("#dateRange_week").click(function () {
var startDate = (7).days().ago();
var start = startDate.toString("M/d/yyyy");
var endDate = new Date();
var end = endDate.toString("M/d/yyyy");
$("#StartDate").val(start);
$("#EndDate").val(end);
});