- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
Admin
Use ISO 8601 date format and this would actually work...
Admin
Except for dates that start with+/- , which ISO 8601 allows. +3000 is smaller than 2021. Just don't use strings.
Admin
That's why proper date format is yyyymmdd in integer/string/real or yyyy[-,/,,whatever]mm[-,/,,whatever]dd in string. Will always sort correctly
Admin
What if midnight occurs between the creation of MyDate and Get/Set date? [honestly asking, not sure]
Admin
It won't change the behavior, as those are just property accessors. They're not checking the time.
Admin
I think you meant gone the first three months entirely and the first two weeks of April through December. Technically that means you made the same type of mistake as the original author! ;)
Admin
No, the proper way to format dateTime is decimal yyyy.yyyy hh:mm:ss. As in, 2013.23 4:22:11. Although I will accept French Revolutionary Month Names as well.
Admin
Delete! Delete! :) Too early
Admin
My brain hurts. What is ('0' + MyDate.getDate()).slice(-2) supposed to do? Should it be getDay() or is the default format with the day of the month at the end?
Admin
getArticle(setArticle())
Admin
Which doesn't even work because the 0 is at the start. Oh god.
Admin
Well, at least they didn't compare the date string to the date object.
Admin
That's what I thought at first, but '14-01-2020' > '13-04-2020'.
Admin
getDate()
returns the day of the month.('0' + MyDate.getDate()).slice(-2)
just zero-pads the day of the month to make it two digits.If MyDate is June 15, 2021,
MyDate.getDate()
returns 15MyDate.getMonth()
returns 5 (Yes, it's zero-based, as if it were an index into a months array. It's not the month number that you would write.)MyDate.getYear()
(which wasn't used in the code snippet) returns 121.MyDate.getFullYear()
returns 2021Addendum 2021-06-15 09:50: Also, MyDate.getDay() returns 2. It's the day of the week, with Sunday = 0.
Admin
I kept reading
('0' + MyDate.getDate()).slice(-2)
as'0' + MyDate.getDate().slice(-2)
and thought it would add an extra 0 to 2-digit dates.Why doesn't JavaScript have a sprintf-like function for formatting numbers?
Admin
The baffling line is probably where the original coder was manually setting the date to spot-check (apparently not very thoroughly) the behavior, and then instead of removing or commenting the line left it in place but made it do nothing but waste a little time.
Or alternatively, maybe I'm not psychic.
Admin
Did you really think it didn't already exist?
https://www.npmjs.com/package/printf
Admin
First convert to Star-Date. Problem solved.
Admin
There must be some language which looks at "13-04-2020" and converts it to -2011
Admin
Admin
That's not exactly "JavaScript having it" imo
Admin
Of course, the real WTF is ... JavaScript. :)
Admin
There's a difference between getting and setting a property, Remy.
In this particular case, it makes little or no difference. But if you work on a humongous server system, say Visa or American Airlines ... there is a difference. Yesterday suddenly becomes today.
Follow Nathan Myhrvold's advice. If it looks like a once in a million chance -- it will come back and bite you next Tuesday.
Admin
Maybe even old-fashioned "day of the year" date notation such as YY/NNN or YYYY/NNN - where NNN is the day within the year, 001...366, as %j per strftime() description - would have worked, but then, how does one most worst manually calculate NNN from -MM-DD in a stringly manner? Hmmm...
Admin
In here it would fail in year 10k :) new Date(10000, 1 , 1).toISOString() -> "+010000-01-31T23:00:00.000Z" I know it will not run for so long. I am just nitpicking.
In real life you might get this too if your're analyzing dates outside current time (history, far off modeling), or your users are being malicious, or if you use something like LocalDate.MAX in java for "far future/never" const.
Additionally it is faster to compare 2 numbers instead of 2 strings (if you need to worry about performance)
Admin
According to my calendar, 13-04-2014 has already passed, so they could, you know, just add the banner without any ifses and elseses. It not like the date would not be passed sometime in the future.
Admin
TRWTF is why these idiots insist on using string math. Formatting numbers into strings should be done for no reason other than display.