Thursday, July 29, 2010

jquery datepicker with current date and custom date format

I searched the net to find a more simpler solution to set the current date with jQuery UI datepicker but didn't find anything. So here is the solution I used. Mixture of both normal JavaScript and jQuery. You can also check out how to use a custom date format with jQuery UI datepicker.

var currentTime = new Date();
var month = currentTime.getMonth() + 1;
var day = currentTime.getDate();
var year = currentTime.getFullYear();
var d = year + "-" + month + "-" + day;

$('.date-pick').datepicker({ dateFormat: 'yy-mm-dd' }).val(d);

5 comments:

  1. Great! Except when I choose a date from the calendar, the field is displayed with an extra yyyy on the end. ???

    ReplyDelete
  2. Great it working fine, For my urgency it helped a lot.

    ReplyDelete
  3. just do it in 2 line after initialize the datepicker.

    $('.date-pick').datepicker({ dateFormat: 'yy-mm-dd' });
    $('.date-pick').datepicker("setDate", new Date());

    ReplyDelete