How do I pre-populate a jQuery Datepicker textbox with today's date?
Solution 1
Update: There are reports this no longer works in Chrome.
This is concise and does the job (obsolete):
$(".date-pick").datepicker('setDate', new Date());
This is less concise, utilizing chaining allows it to work in chrome (2019-06-04):
$(".date-pick").datepicker().datepicker('setDate', new Date());
Solution 2
You must FIRST call datepicker() > then use 'setDate' to get the current date.
$(".date-pick").datepicker();
$(".date-pick").datepicker("setDate", new Date());
OR chain your setDate method call after your datepicker initialization, as noted in a comment on this answer
$('.date-pick').datepicker({ /* optional option parameters... */ })
.datepicker("setDate", new Date());
It will NOT work with just
$(".date-pick").datepicker("setDate", new Date());
NOTE : Acceptable setDate parameters are described here
Solution 3
var myDate = new Date();
var prettyDate =(myDate.getMonth()+1) + '/' + myDate.getDate() + '/' +
myDate.getFullYear();
$("#date_pretty").val(prettyDate);
seemed to work, but there might be a better way out there..
Solution 4
The setDate()
method sets the date and updates the associated control. Here is how:
$("#datepicker1").datepicker({
dateFormat: "yy-mm-dd"
}).datepicker("setDate", "0");
As mentioned in documentation, setDate()
happily accepts the JavaScript Date object, number or a string:
The new date may be a Date object or a string in the current date format (e.g. '01/26/2009'), a number of days from today (e.g. +7) or a string of values and periods ('y' for years, 'm' for months, 'w' for weeks, 'd' for days, e.g. '+1m +7d'), or null to clear the selected date.
In case you are wondering, setting defaultDate
property in the constructor does not update the associated control.
Solution 5
Set to today:
$('#date_pretty').datepicker('setDate', '+0');
Set to yesterday:
$('#date_pretty').datepicker('setDate', '-1');
And so on with any number of days before or after today's date.
Related videos on Youtube
Marcus
I am a programmer working with the University of New Hampshire, primarily on open source solutions.
Updated on October 16, 2020Comments
-
Marcus over 3 years
I have a very simple jQuery Datepicker calendar:
$(document).ready(function(){ $("#date_pretty").datepicker({ }); });
and of course in the HTML...
<input type="text" size="10" value="" id="date_pretty"/>
Today's date is nicely highlighted for the user when they bring up the calendar, but how do I get jQuery to pre-populate the textbox itself with today's date on page load, without the user doing anything? 99% of the time, the today's date default will be what they want.
-
ahmadali shafiee over 11 yearsThe most popular answer is better that accepted answer. it's better to change the accepted answer.
-
Adrien Be over 9 yearsmy related question stackoverflow.com/questions/25602649/…
-
Teepeemm about 6 years@ahmadalishafiee (and everyone else). The currently accepted solution was accepted on 2015-05-07.
-
-
Marcus over 15 yearsThis works pretty well except for two things: 1) I was kind of hoping to use jQuery method to get the current date, but maybe it doesn't matter. 2) this solution produces a value exactly one month previous to today's date!
-
lucas over 15 yearsOH - you're right! check it out - w3schools.com/jsref/jsref_obj_date.asp - month is 0-11 - you'll have to add 1.. curiously, getDate is 1-31, but getMonth is 0-11..
-
CiscoIPPhone about 14 yearsMy browser did not recognise the asString() function. I did it like this: $(".date-pick").datepicker('setDate', new Date());
-
rg88 about 14 yearsI think asString() should be toString().
-
Craig Hooghiem almost 14 yearsKevin Luck's DatePicker and the Jquery UI DatePicker are two very different datepickers.
-
Mathias Bynens about 13 years@gamerzfuse jQuery UI’s datepicker is based on that of Kevin Luck.
-
Ravi Ram over 12 yearsThere is an easier way .. see my post below.
-
Afshar Mohebi about 12 yearsThis solution let's to show text directly and set date simultaneously while CiscoIPPhone's solution needs another step to show the value without opening the picker.
-
mutantacule almost 12 yearsFor
setDate
you can also use the usual relative notation of datepicker, for example to get the following day just type.datepicker('setDate', '+1d')
. -
chobo2 over 11 yearsWhy is it in your demo it works. When I copy this code into my site the dialog keeps popping up when the pg loads.
-
brianz over 11 yearsUsing jQueryUI 1.8 I found this solution to be the only (sane) way to accomplish this.
-
jwbensley about 11 yearsGreat, as others have mentioned, you must have called datepicker() once already so,
$(".date-pick").datepicker("setDate", '+0');
-
abc123 almost 11 years$(".selector").datepicker().datepicker("setDate", new Date()); will work and doesn't make jQuery search the DOM twice.
-
Mehdiway over 10 yearsThis is the one that actually worked for me and not the accepted solution. Thank you
-
William over 10 yearsI would like to suggest if the stackoverflow could just put the top voted answer on top, rather than as it's. E.g like what Youtube did is good.
-
Adrien Be over 9 years+1, using the Date object is much cleaner & bug-proof. Note that you can use things such as
var myDate = new Date(); myDate.setFullYear(1985); myDate.setMonth(0); /* month is zero-based */ myDate.setDate(28);
see more developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… -
jjohn over 9 yearsThis also worked as I wished, since my dateFormat needs to match MySQL's date format (yy-mm-dd). This setDate adds zero padding to the month and day.
-
Misha Akopov almost 9 yearsGreat, accepted answer says only $(".date-pick").datepicker("setDate", new Date()); and it doesn't work without first call $(".date-pick").datepicker(); your answer is much better
-
Misha Akopov almost 9 yearsThis will not work, first you should call $(".date-pick").datepicker(); and then $(".date-pick").datepicker('setDate', new Date());
-
Max Flex over 8 yearsNot longer works in Chrome after recent browser update
-
CiscoIPPhone over 8 years@QuinnDaley Thanks - I'm adding a warning to my answer.
-
lydiat over 8 yearsThis worked for me - the accepted answer and other options above didn't.
-
adamj about 7 yearsThis is a much, much better solution than any of the above.
-
John81 about 7 yearsBest answer for this question at the present time although I needed the date format to be "mm-dd-yy" to match the date picker.
-
seebiscuit almost 7 yearsWorks. I'm setting
$(".date-pick").datepicker().datepicker('setDate', new Date());
tho -
waxingsatirical almost 6 yearsoption A is good because date picker can remain blank until a date is to be selected at which point it defaults to the current date
-
Mike Volmar almost 5 yearsthis solution worked for me in chrome, others listed here did not