Given this markup:
// Calendar.html?date=1/2/2003
<script>
$(function() {
$('.inlinedatepicker').datepicker();
});
</script>
...
<div class="inlinedatepicker" id="calendar"/>
This will display an inline datepicker with very little effort (awesome!). How do I preset the datepicker to the date passed in via the query string?
Note that in this case, I don't have an input box--just a calendar attached to a div.
Initialize the datepicker and specify the date format in "yyyy-mm-dd". JavaScript Code : $( "#datepicker" ). datepicker(); $( "#datepicker" ).
To set current date in control to which jQuery UI datepicker bind, use setDate() method. Pass date object which needs to be set as an argument to setDate() method. If you want to set it to current date then you can pass 'today' as argument.
inside the jQuery script code just paste the code. $( ". selector" ). datepicker({ dateFormat: 'yy-mm-dd' });
If you want to set a value for an input type 'Date', then it has to be formatted as "yyyy-MM-dd" (Note: capital MM for Month, lower case mm for minutes). Otherwise, it will clear the value and leave the datepicker empty.
This should work, though you may run into locale issues (specifically, M/D/Y vs. D/M/Y).
var date = location.match(/(?:\?|&)date=(\d+\/\d+\/\d+)(&|$)/)[1];
$('.inlinedatepicker').datepicker().datepicker("setDate", new Date(date));
I managed to do this with
$('.inlinedatepicker').datepicker().datepicker("setDate", new Date(2011,11,01)); // Date equals December 1, 2011.
I had to subtract one from the month though to get the right date as it seems that date is starting from zero (0 = January)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With