0

I can't figure this out from the documentation, but I want to set the default date of a datepicker input in the same format as the datepicker's display without having to do my own parsing. E.g.:

$("#dp").datepicker({
    altField: '#helper',
    altFormat: 'yy-mm-dd',
    formatDate: 'm/d/Y',
}).val($("#helper").val())

This will display the datepicker in put as 2013-03-07 (as you can see: http://jsfiddle.net/zaG5z/) However when you select a date it does display it as "m/d/Y." Even selecting the same date makes it 03/07/2013. Is there any way to trigger that format from the initial value?

Explosion Pills
  • 188,624
  • 52
  • 326
  • 405

1 Answers1

2

Try this:

$("#dp").datepicker({
    altField: '#helper',
    altFormat: 'yy-mm-dd',
    dateFormat: 'm/d/yy',
}).datepicker("setDate", new Date($("#helper").val()));

The important thing here is to use the setDate method. Previously you were simply setting the val(), which was setting the value of the input but was not having any effect on the datepicker.

nick_w
  • 14,758
  • 3
  • 51
  • 71
  • That's great, but is there any way to prevent the datepicker from opening too (or just hide it immediately)? I tried with `hide`, but that didn't do it – Explosion Pills Mar 07 '13 at 20:04
  • @ExplosionPills I think this could be a bug. In the fiddle, if I choose `jQuery UI 1.9.2` from under `Frameworks & Extensions` and remove the reference to `jquery-ui-git.js` from `External References`, then the datapicker does not open on calling `setdate`. – nick_w Mar 07 '13 at 21:17