JavaScript ISO Dates

ISO 8601 is the international standard for the representation of dates and times. The ISO 8601 syntax (YYYY-MM-DD) is also the preferred JavaScript date format. An example of a complete date:

   var d = new Date("2015-03-25");

HTML file: Displayed by browser:
<body>
<h4>Example of a complete date:</h4>
<p id="demo"></p>
<script>
var d = new Date("2015-03-25");
document.getElementById("demo").innerHTML = d;
</script>
</body>

Example of a complete date:

Geesh, this is so confusing! I couldn't understand why I was getting the wrong day and month in my ouput. The reason it shows the day at 24 (instead of 25) is because of the timezone, which has not been specified in the script. As for the month, well, in JavaScript, the months are zero-based so January = 0 and December = 11 -- but in this wacky business, ISO months are one-based, which makes January = 1 and December = 12!

BackTable of ContentsNext