datepicker. formatDate("dd-mm-yy", new Date())); $("#return_on").
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.
Initialize the datepicker and specify the date format in "yyyy-mm-dd". JavaScript Code : $( "#datepicker" ). datepicker(); $( "#datepicker" ).
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());
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
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..
The setDate()
method sets the date and updates the associated control. Here is how:
$("#datepicker1").datepicker({
dateFormat: "yy-mm-dd"
}).datepicker("setDate", "0");
Demo
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.
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.
See jQuery UI › Methods › setDate.
The solution is:
$(document).ready(function(){
$("#date_pretty").datepicker({
});
var myDate = new Date();
var month = myDate.getMonth() + 1;
var prettyDate = month + '/' + myDate.getDate() + '/' + myDate.getFullYear();
$("#date_pretty").val(prettyDate);
});
Thanks grayghost!
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