Javascript date - Leading 0 for days and months where applicable

date, javascript

Solution

The format you seem to want looks like ISO. So take advantage of `toISOString()`:

var d = new Date();
var date = d.toISOString().slice(0,10); // "2014-05-12"

Problem

Is there a clean way of adding a 0 in front of the day or month when the day or month is less than 10: ``` var myDate = new Date(); var prettyDate =(myDate.getFullYear() +'-'+ myDate.getMonth()) +'-'+ myDate.getDate(); ``` This would output as: ``` 2011-8-8 ``` I would like it to be: ``` 2011-08-08 ```

Original source

Related problems