Set date in input type date
JavascriptHtmlJqueryDateJavascript Problem Overview
I will set today's date in the datepicker input type date in Chrome.
$(document).ready(function() {
let now = new Date();
let today = now.getDate() + '/' + (now.getMonth() + 1) + '/' + now.getFullYear();
console.log(today);
$('#datePicker').val(today);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input id="datePicker" type="date">
However, it is not working.
Please try the code snippet in Chrome.
Javascript Solutions
Solution 1 - Javascript
Fiddle link : http://jsfiddle.net/7LXPq/93/
Two problems in this:
- Date control in HTML 5 accepts in the format of Year - month - day as we use in SQL
- If the month is 9, it needs to be set as 09 not 9 simply. So it applies for day field also.
Please follow the fiddle link for demo:
var now = new Date();
var day = ("0" + now.getDate()).slice(-2);
var month = ("0" + (now.getMonth() + 1)).slice(-2);
var today = now.getFullYear()+"-"+(month)+"-"+(day) ;
$('#datePicker').val(today);
Solution 2 - Javascript
document.getElementById("datePicker").valueAsDate = new Date()
should work.
Solution 3 - Javascript
var today = new Date().toISOString().split('T')[0];
$("#datePicker").val(today);
Above code will work.
Solution 4 - Javascript
Update: I'm doing this with date.toISOString().substr(0, 10)
. Gives the same result as the accepted answer and has good support.
Solution 5 - Javascript
to me the shortest way to solve this problem is to use moment.js and solve this problem in just 2 lines.
var today = moment().format('YYYY-MM-DD');
$('#datePicker').val(today);
Solution 6 - Javascript
Your code would have worked if it had been in this format: YYYY-MM-DD
, this is the computer standard for date formats http://en.wikipedia.org/wiki/ISO_8601
Solution 7 - Javascript
I usually create these two helper functions when using date inputs:
// date is expected to be a date object (e.g., new Date())
const dateToInput = date =>
`${date.getFullYear()
}-${('0' + (date.getMonth() + 1)).slice(-2)
}-${('0' + date.getDate()).slice(-2)
}`;
// str is expected in yyyy-mm-dd format (e.g., "2017-03-14")
const inputToDate = str => new Date(str.split('-'));
You can then set the date input value as:
$('#datePicker').val(dateToInput(new Date()));
And retrieve the selected value like so
const dateVal = inputToDate($('#datePicker').val())
Solution 8 - Javascript
You can only use date in input type="date"
as in format YYYY-MM-DD
I have implemented helper as formatDate
in NODE.js express-handlebars, don't need to be worry ... just use format as described in first line.
e.g:
< input type="date" id="date" name="date" class="form-control" value="{{formatDate invoice.date 'YYYY-MM-DD'}}" />
Solution 9 - Javascript
For me the shortest way to get locale date and in correct format for input type="date"
is this :
var d = new Date();
var today = d.getFullYear()+"-"+("0"+(d.getMonth()+1)).slice(-2)+"-"+("0"+d.getDate()).slice(-2);
Or this :
var d = new Date().toLocaleDateString().split('/');
var today = d[2]+"-"+("0"+d[0]).slice(-2)+"-"+("0"+d[1]).slice(-2);
Then just set the date input value :
$('#datePicker').val(today);
Solution 10 - Javascript
2021
A great way I found to do this is using locale for the formatting. You can use it in many different ways. This works for when you aren't using the jquery datepicker, but the html datepicker.
$("#datepickerEnd").val(new Date().toLocaleDateString('en-CA'));//YYYY-MM-dd
Or split up for manipulation or adjustments or other uses.
let theDate = new Date();
$("#datepickerEnd").val(theDate.toLocaleDateString('en-CA'));//YYYY-MM-dd
Manipulation example.
theDate.setDate(theDate.getDate()-7);
$("#datepickerStart").val(theDate.toLocaleDateString('en-CA'));//YYYY-MM-dd
The 'en-ca' is the needed format for my datepicker. If yours is different you can look for a string format that matches. Since it turns it to a string it does not contain extra information for messing up per region. So, in this way you can use it for purely formatting.
For more information
Solution 11 - Javascript
Datetimepicker always needs input format YYYY-MM-DD, it doesn't care about display format of your model, or about you local system datetime. But the output format of datetime picker is the your wanted (your local system). There is simple example in my post.
Solution 12 - Javascript
Try This,
$('#datePicker').val(moment().format('YYYY-MM-DD'));
Note : You need to add Moment.js as a dependency
Solution 13 - Javascript
Jquery version
var currentdate = new Date();
$('#DatePickerInputID').val($.datepicker.formatDate('dd-M-y', currentdate));