js datetime format yyyy-mm-dd code example
Example 1: javascript convert date to yyyy-mm-dd
const formatYmd = date => date.toISOString().slice(0, 10);
formatYmd(new Date());
Example 2: js date format yyyy-mm-dd
yourDate.toISOString().split('T')[0]
Example 3: get current date javascript yyyy-mm-dd
function pad2(n) {
return (n < 10 ? '0' : '') + n;
}
var date = new Date();
var month = pad2(date.getMonth()+1);
var day = pad2(date.getDate());
var year= date.getFullYear();
var formattedDate = year+"-"+month+"-"+day;
alert(formattedDate);