Few Interesting things with jQuery UI DatePicker setDate()

Earlier today, I posted about "How to Set Current Date in jQuery UI Datepicker" using setDate() method provided by jQuery UI Datepicker library. But there are few interesting things which you can also do with setDate() method which you might not be knowing.

setDate() method takes 'date' as argument but this argument either can be date object or string object.

$('#txtDate').datepicker().datepicker('setDate', new Date());
//OR
$('#txtDate').datepicker().datepicker('setDate', '12/31/2000');

But you can also pass number of days from today which you want to set as date. Consider below jQuery code,

$('#txtDate').datepicker().datepicker('setDate', '+7');

This code will set the date as "12-Dec-2012", considering today is "5-Dec-2012". Interesting?? Wait, there is more. You can also pass this in weeks, month, years or combination of any of these. Default is days.

Use "y" for years, "m" for months, "w" for weeks, "d" for days.

For 1 month from current date,

$('#txtDate').datepicker().datepicker('setDate', '+1m');

For 1 year from current date,

$('#txtDate').datepicker().datepicker('setDate', '+1y');

For 1 month and 7 days from current date,

$('#txtDate').datepicker().datepicker('setDate', '+1m +7d');

So next time, while using setDate() method remember these little but useful things.

Feel free to contact me for any help related to jQuery, I will gladly help you.



Responsive Menu
Add more content here...