• Prime Mover (unregistered)
  • Stu (unregistered)

    Use ISO 8601 date format and this would actually work...

  • Pete (unregistered) in reply to Stu

    Except for dates that start with+/- , which ISO 8601 allows. +3000 is smaller than 2021. Just don't use strings.

  • Hasseman (unregistered)

    That's why proper date format is yyyymmdd in integer/string/real or yyyy[-,/,,whatever]mm[-,/,,whatever]dd in string. Will always sort correctly

  • (nodebb)

    What if midnight occurs between the creation of MyDate and Get/Set date? [honestly asking, not sure]

  • (author) in reply to TheCPUWizard

    It won't change the behavior, as those are just property accessors. They're not checking the time.

  • Every Month? (unregistered)

    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! ;)

  • 516052 (unregistered) in reply to Hasseman

    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.

  • Every Month? (unregistered) in reply to Every Month?

    Delete! Delete! :) Too early

  • D J Hemming (unregistered)

    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?

  • Edd (unregistered)


  • D J Hemming (unregistered) in reply to D J Hemming

    Which doesn't even work because the 0 is at the start. Oh god.

  • (nodebb)

    Well, at least they didn't compare the date string to the date object.

  • Moarn (unregistered) in reply to Every Month?

    That's what I thought at first, but '14-01-2020' > '13-04-2020'.

  • (nodebb) in reply to D J Hemming

    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 15
    • MyDate.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 2021

    Addendum 2021-06-15 09:50: Also, MyDate.getDay() returns 2. It's the day of the week, with Sunday = 0.

  • (nodebb)

    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?

  • scriptninja (github)

    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.

  • Shill (unregistered) in reply to Barry Margolin 0

    Did you really think it didn't already exist?


  • Carl Witthoft (google)

    First convert to Star-Date. Problem solved.

  • The Shadow Knows (unregistered)

    There must be some language which looks at "13-04-2020" and converts it to -2011

  • I'm not a robot (unregistered) in reply to Pete
    Except for dates that start with+/-
    You do realise that the code is generating the string itself, right? If it was converted to use ISO format, that wouldn't cause strings starting with + or - to just magically appear, and therefore there's no reason why the comparison would need to handle them properly.
  • t (unregistered) in reply to Shill

    That's not exactly "JavaScript having it" imo

  • Lurk (unregistered)

    Of course, the real WTF is ... JavaScript. :)

  • Sole Purpose Of Visit (unregistered) in reply to Remy Porter

    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.

  • Officer Johnny Holzkopf (unregistered) in reply to Carl Witthoft

    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...

  • Pete (unregistered)

    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)

  • aalien (unregistered)

    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.

  • I dunno LOL ¯\(°_o)/¯ (unregistered) in reply to Barry Margolin 0

    TRWTF is why these idiots insist on using string math. Formatting numbers into strings should be done for no reason other than display.

  • guest (unregistered)
    Comment held for moderation.
  • Prashant (unregistered)
    Comment held for moderation.
  • strong wilder (unregistered)
    Comment held for moderation.
  • goodwinrosales (unregistered)
    Comment held for moderation.

Leave a comment on “A Date With Yourself”

Log In or post as a guest

Replying to comment #:

« Return to Article