9 ms·
I used the JS Date API for the first time a few months ago, and wrote something like date.getYear() + "-" + date.getMonth() + "-" + date.getDay() in the h
by jasamer 5y ago
I used the JS Date API for the first time a few months ago, and wrote something like
date.getYear() + "-" + date.getMonth() + "-" + date.getDay()
in the hope of getting something like "2022-1-4".
The actual result for that date is "122-0-2" - not a single component of the date was what I expected.
- jazzyjackson 5y agodate.toLocaleDateString ("fr-CA") would have got you there, but yea agreed 0 indexed months are whack. I'm sure you've read the docs since then but just for the viewers at home: s/getYear/getFullYear s/getDay/getDate getYear is oldfashioned, years since 1900. getDay is day-of-the-week, 0 for Sunday, 1 for Monday, etc
- cookie_monsta 5y ago> 0 indexed months are whack Why, if everything else in JS is 0 indexed? Isn't consistency a good thing in a language? If they'd gone the other way, wouldn't there be people here complaining that 1 indexing is whack?
- 6510 5y agohttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getDate https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe... lol
- jazzyjackson 5y agoWell because days are returned 1-indexed and calendars in general are 1-indexed, but of course the internal 'getMonth' method isn't meant to be a calendar, it's meant to provide an array index to ["January", "February", ...] as an interface to the other methods of printing a human-readable form.