Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I initialize a jQuery UI datepicker to a date from the querystring?

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.

like image 329
Michael Haren Avatar asked May 18 '09 20:05

Michael Haren


People also ask

How do I initialize a datepicker?

Initialize the datepicker and specify the date format in "yyyy-mm-dd". JavaScript Code : $( "#datepicker" ). datepicker(); $( "#datepicker" ).

How do I set datepicker to current date?

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.

How can change date format in jQuery UI datepicker?

inside the jQuery script code just paste the code. $( ". selector" ). datepicker({ dateFormat: 'yy-mm-dd' });

How do I set datepicker input?

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.


2 Answers

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));
like image 70
Ben Blank Avatar answered Sep 16 '22 16:09

Ben Blank


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)

like image 41
Trond Avatar answered Sep 19 '22 16:09

Trond