5 ms·
Wahoo, it doesn't use 0-indexed months! Probably the biggest cause of bugs with js Date objects.
by mikeocool 5y ago
Wahoo, it doesn't use 0-indexed months! Probably the biggest cause of bugs with js Date objects.
- postalrat 5y agoYears aren't 0-indexed either. Nobody has a problem with that.
- jessaustin 5y agoHow do you know they aren't? What happened in 1 CE? What happened in 1 BCE? What happened during the year in between?
- postalrat 5y agoThere isn't a year between them.
- jessaustin 5y agoIn most calendar systems there is. Perhaps there isn't in your favorite calendar, or perhaps you just don't know about it. For instance, the subject of TFA is based on the "Proleptic Gregorian", which does have a year zero.
- mkotowski 5y agoThat does remind me: why that even is a thing? Ordering days of a week from zero can make sense since there is Monday/Sunday as a week start divide, and this can be seen as a bypass (0/7 as Sunday and 1 as a Monday), but with months?
- mikewhy 5y agohttps://stackoverflow.com/a/41992352 https://stackoverflow.com/a/41992352
- SippinLean 5y ago> it was copied from Java's JDK1.0 (1995) java.util.Date, which also had this quirk
- jauco 5y agoNo. It’s definitely older than that. https://mobile.twitter.com/hillelogram/status/1329228419628998665 https://mobile.twitter.com/hillelogram/status/13292284196289...
- Taywee 5y agoTwitter is such a bad platform for this. Code in images means it's completely inaccessible to the blind and very inaccessible to people with poor vision, and you can't copy and paste it into an editor for syntax highlighting or better examination.
- BrendanEich 5y agohttps://twitter.com/bahstgwamt/status/867609109276872704?s=20 https://twitter.com/bahstgwamt/status/867609109276872704?s=2...
- mikeryan 5y agoJust because the issue predated Java doesn’t preclude the fact that the initial JavaScript Date implementation was lifted from java.util.Date. Brandon Eich has explicitly stated it was so. https://maggiepint.com/2017/04/09/fixing-javascript-date-getting-started/ https://maggiepint.com/2017/04/09/fixing-javascript-date-get...
- tshaddox 5y agoZero indexing for arrays/collections generally makes sense. If you had the months in a normal array they would obviously be zero-indexed (unless you deliberately put a placeholder value in the first position in the array, which would be pretty weird since all the normal ways of getting the first thing in the array would break). The reason it apparently feels weird to many people to have months be zero indexed is that months are often referred to by their ordinal number, and ordinal numbers obviously start at 1.
- WA 5y agoEven if this is true, days in JS still start at 1. It is inconsistent.
- dmz73 5y agoZero indexing of arrays only makes sense in C because array variable is a pointer to first element of the array. In all languages that have actual array type with length and bounds checking, 1 would be a better way to index arrays most of the time and having specified valid range would be the best way.
- yunohn 5y ago> months are often referred to by their ordinal number What do you mean by “often”? Humans always use ordinal numbers for counting things like months, days, weeks, etc. There is no concept of 0 to be applied here, except for programming array indexing purposes.
- tshaddox 5y agoWhat I mean is that people use the ordinal number as the name of the month (as well as the day of the month). If you ask someone their birthday they’ll often say something like “five twenty, nineteen eighty-four.” This is a potential cause for confusion when writing computer code, because it’s very common for code to refer to items in a collection starting at zero.
- kevin_thibedeau 5y agoIt is lifted from C's struct tm. If you're maintaining tables of month names you're going to 0-index them so tm_mon is 0-based to facilitate that.
- lucian1900 5y agoThe week starts on Monday in most of the world. Date & time APIs must match local reality.
- pavel_lishin 5y agoWhen reality differs by locales, at least one "local reality" won't match the API.
- deleted 5y ago[deleted]
- matsemann 5y agoAnother one is JS parsing numbers with leading zeroes as octal (unless specifying base, which is why linters are so strict on it). Often surfaces when parsing date strings.
- toomanybeersies 5y agoThat hasn't been the case in JS for almost a decade. parseInt('0700') returns 700. But parseInt('0x700') will be parsed as octal, so now it's just a more subtle gotcha. Loose parsers (such as parseInt(), and Date.parse() in JS) are one of the worst ideas ever. Nothing like tracing a bug, and finding that a junior dev is trying to parse DD/MM/YYYY strings using Date.parse(), except that instead of throwing on trying to parse an incorrect [non-RFC] format, the parser makes a best-guess and propagates an incorrect date because it was parsed as MM/DD/YYYY.
- alkonaut 5y agoNot sure why any api ever had functions like float.parse(s) or date.parse(s). Even more modern ones like .NET (circa 2000) had it. And it's literally never a good idea. Not even for a one off script is it convenient or ergonomic to omit the information about what format you expect (or try to suck it out of thin air such as a static locale on the thread). It's just a bad.idea. If it does accept date parse(s) it has to be with a fixed format (e.g. iso) unless specified. Not "system local format" or something. The same applies to number formatting. I haven't met one single person who prefers automatic string formatting to switch decimal point and decimal comma due to the system language.