Just leverage the built-in toISOString method that brings your date to the ISO 8601 format:
let yourDate = new Date()
yourDate.toISOString().split('T')[0]
Where yourDate is your date object.
Edit: @exbuddha wrote this to handle time zone in the comments:
const offset = yourDate.getTimezoneOffset()
yourDate = new Date(yourDate.getTime() - (offset*60*1000))
return yourDate.toISOString().split('T')[0]
Answer from Darth Egregious on Stack OverflowJust leverage the built-in toISOString method that brings your date to the ISO 8601 format:
let yourDate = new Date()
yourDate.toISOString().split('T')[0]
Where yourDate is your date object.
Edit: @exbuddha wrote this to handle time zone in the comments:
const offset = yourDate.getTimezoneOffset()
yourDate = new Date(yourDate.getTime() - (offset*60*1000))
return yourDate.toISOString().split('T')[0]
You can do:
function formatDate(date) {
var d = new Date(date),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();
if (month.length < 2)
month = '0' + month;
if (day.length < 2)
day = '0' + day;
return [year, month, day].join('-');
}
console.log(formatDate('Sun May 11,2014'));
Usage example:
console.log(formatDate('Sun May 11,2014'));
Output:
2014-05-11
Demo on JSFiddle: http://jsfiddle.net/abdulrauf6182012/2Frm3/
Convert date in JavaScript to yyyy-mm-dd format
html - How to get current formatted date dd/mm/yyyy in Javascript and append it to an input - Stack Overflow
Javascript date format dd/mm/yy NOT dd/mm/yyyy - do I need to import a library?
Weird JavaScript date problem: new Date('MM/dd/yyyy') !== new Date('yyyy-MM-dd')
Videos
I hope this is what you want:
const today = new Date();
const yyyy = today.getFullYear();
let mm = today.getMonth() + 1; // Months start at 0!
let dd = today.getDate();
if (dd < 10) dd = '0' + dd;
if (mm < 10) mm = '0' + mm;
const formattedToday = dd + '/' + mm + '/' + yyyy;
document.getElementById('DATE').value = formattedToday;
How do I get the current date in JavaScript?
I honestly suggest that you use moment.js. Just download moment.min.js and then use this snippet to get your date in whatever format you want:
<script>
$(document).ready(function() {
// set an element
$("#date").val( moment().format('MMM D, YYYY') );
// set a variable
var today = moment().format('D MMM, YYYY');
});
</script>
Use following chart for date formats:

Some day, someone will invent a JavaScript compiler that handles dates in a consistent manner. Sadly, today is not that day. I'm having an issue where two identical dates that are parsed from differently formatted strings are producing two different results.
For example,
new Date('08/01/2014')will result in a value of "Fri Aug 01 2014 00:00:00 GMT-0400 (Eastern Daylight Time)", as expected.
However,
new Date('2014-08-01')gives "Thu Jul 31 2014 20:00:00 GMT-0400 (Eastern Daylight Time)". An offset of 4 hours. Basically, what happens is that using the MM/dd/yyyy format uses the local timezone and yyyy-MM-dd uses UTC.
Now the weirder part: appending midnight, "00:00:00", to the date value gives the expected result regardless of which of the two formats are used.
This happens in Chrome, Firefox, and IE. I have not tested Safari and Opera but, since they both use Webkit, I expect the same problem.
I can code around this easily enough but does anyone know the reasoning behind this?