When initializing a new Date
object in JavaScript using the below call, I found out that the month argument counts starting from zero.
new Date(
I know it's not really an answer to the original question, but I just wanted to show you my preferred solution to this problem, which I never seem to memorize as it pops up from time to time.
The small function zerofill does the trick filling the zeroes where needed, and the month is just +1
added:
function zerofill(i) {
return (i < 10 ? '0' : '') + i;
}
function getDateString() {
const date = new Date();
const year = date.getFullYear();
const month = zerofill(date.getMonth()+1);
const day = zerofill(date.getDate());
return year + '-' + month + '-' + day;
}
But yes, Date has a pretty unintuitive API, I was laughing when I read Brendan Eich's Twitter.