ISO Dates: Year & Month

ISO 8601 can be written without specifying the day (YYYY-MM) and will still output a complete date to the browser. An example of year and month:

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

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

Example of only year & month:

The reason it shows Feb 28 is because of the timezone, which has not been specified in the script. By using only the month, this is construed as the first of the month. As for the month being "off" by one month, it's imperative to remember that ISO months are one-based, (not zero-based as in JavaScript) which makes January = 1 and December = 12!

BackTable of ContentsNext