Is there any way to set the format of ? if no then how can i set date in this field using JavaScript in the default format of
@cOnstructOr provided a great idea, but it left a comma in place
var today = new Date().toLocaleString('en-GB').split(' ')[0].slice(0,-1).split('/').reverse().join('-');
fixes that
function getDefaultDate(curDate){
var dt = new Date(curDate);`enter code here`
var date = dt.getDate();
var month = dt.getMonth();
var year = dt.getFullYear();
if (month.toString().length == 1) {
month = "0" + month
}
if (date.toString().length == 1) {
date = "0" + date
}
return year.toString() + "-" + month.toString() + "-" + date.toString();
}
In function pass your date string.
new Date().toISOString().split('T')[0];
What you want to do is fetch the value from the input and assign it to a new Date instance.
let date = document.getElementById('dateInput');
let formattedDate = new Date(date.value);
console.log(formattedDate);
Easier than the above is
var today = new Date().toISOString().substring(0,10); # "2013-12-31"
The format is YYYY-MM-DD
. You cannot change it.
$('#myinput').val('2013-12-31');
sets value