JavaScript Date Object



  • Witness the JavaScript Date Object.

    WTF I can spot :

    • getTime() returns... the time-of-day part of timestamp ? Nope : the internal counter wich is milliseconds since epoch.
    • getDay() returns... monthday ? Nope : weekday.
    • getDate() returns... the calendar part of timestamp ? Nope : monthday.
    • getYear() returns... the year part of timestamp ? YES, but on 2 digits. As a string then ? Nope : as an integer.
    • getFullYear() returns... the full year ? Not quite : on 4 digits, because y10k bug quota is not met yet.
    • getTimezoneOffset() returns... millisenconds just like getTime() ? Nope : minutes.
    • Being named just Date instead of DateTime or Timestamp since it tracks calendar and clock time.
    • Having both getTime() and valueOf(), wich actually return the same thing.
    • Having had at first year on 2 digits, for a language designed as far back as... 1995.
    • toDateString() & toTimeString() and toLocaleDateString() & toLocaleTimeString() split timestamp into calendar & clock parts... unlike get/setDate() & get/setTime().
    • The toISOString() and toUTCString() variants to split into calendar & clock parts are... whoops they don't have any.
    • Handling calendar & clock splitting only as output formatting. Because nobody ever computes on these.
    • parse(datestring) returns... a Date object ? Nay, it's internal counter.
    • The UTC equivalent of parse() is... parseUTC() ? Try again : UTC().
    • Handling the UTC vs local difference as... apparently a unique and global timezone, with duplicate methods for input & output. So in order to develop a traveller's agenda, one has to... erm.

    Can you spot more ?

     

    Disclaimer : I am not a real developper. But I know crap when there is a big pile of it.



  •   Is this badly done satire or something? You have

    • A standard programming convention that guarantees a unique representation for the time and for which the solution to your complaint is provided by two other functions: getHours() and getMinutes().
    • A function using a completely sensible definition of the word day. (hint: use getDate() if you really want that day of the month.)
    • Okay, getDate() is badly named. Big deal, it's not even in WTF territory yet. If it was named getVorpalSword(), perhaps you might have a point.
    • A deprecated function? (hint: that means you aren't supposed to use it.)
    • Please tell me this is a joke...
    • UTC timezone offsets are only defined in minutes. This would be a WTF if it gave you the longitudinal offset from the prime meridian in minutes and left you to figure out the UTC offset, but it doesn't.


     



  • Date.Now() returns the current time.. in Milliseconds.

    Also, sometimes.. if you call Date.Now() too soon.. it will return a ridiculous time as well.

    That's a fucking mindfuck.



  • The month argument of the constructor expects a zero-based month index. And getMonth returns the month zero-based.

    At least they are consistent.Still does not make it less of a WTF though.

     

    The parse method and the string-based constructor can not parse dates using hyphens in some browsers... because the specs never specced what formats should be supported.



  • @gu3st said:

    Also, sometimes.. if you call Date.Now() too soon.. it will return a ridiculous time as well. That's a fucking mindfuck.

    ...because it's probably not a JS WTF, but a browser one.



  • At least it's not Java's Date, DateTime, Time or Calendar object...



  • You forgot the part where there is no way to set the object to the current time.

    Oh, and how are you supposed to do date arithmetics?



  •  I am pretty sure getYear() returns 4 digit year in some browsers and 2 digits in others. (My favorite WTF)



  • In before Blakeyrat starts bitching about how this is the DOM date object and that the same developers who can't tell the difference between HTML and CSS (sure webkit-linear-gradient is HTML5) should know the difference between a language and the most popular runtime for it and blah blah blah.


  • Considered Harmful

    @henke37 said:

    You forgot the part where there is no way to set the object to the current time.

    Oh, and how are you supposed to do date arithmetics?

    The newly constructed Date object (using the nullary constructor) is set to the current time. Date arithmetic can be done by passing values to the constructor, added to the ones you got when you didn't pass any values to the constructor.

    It is a bit weird, but tolerable.



  • @joe.edwards said:

    Date arithmetic can be done by passing values to the constructor, added to the ones you got when you didn't pass any values to the constructor.
    inb4 "the user shouldn't be the one doing the arithmetic"



  • @MiffTheFox said:

    In before Blakeyrat starts bitching about how this is the DOM date object and that the same developers who can't tell the difference between HTML and CSS (sure webkit-linear-gradient is HTML5) should know the difference between a language and the most popular runtime for it and blah blah blah.

    It's not a DOM object, it's part of the ECMA spec. Meaning, part of JavaScript itself.



  • @TGV said:

    At least it's not Java's Date, DateTime, Time or Calendar object...

    It shares a few WTFs with Java's Date object. But if JodaTime gets into JDK8, will JavaScript get JodaTime then too? :)

    JodaTime is definitely the most consistent Date/Time API i've ever seen across any programming languages. One of its best design goals is that you you can (and have to) decide if you want to work with timezone-free time (where 01:00 + 3 hours will always be 04:00) or in a timezone (where 01:00 + 3 hours can once a year be 05:00 or 03:00) - that alone can save yourself a lot of grief...


  • ♿ (Parody)

    @georgir said:

    The month argument of the constructor expects a zero-based month index. And getMonth returns the month zero-based.

    Date APIs that do this are TRWTF. I always wonder why people who write that sort of code give in on days of the month. C'mon, at least have the courage of your convictions!



  • @Musaran said:

    Witness the JavaScript Date Object.


    Trooooololololoool :)



  • @mihi said:

    @TGV said:
    At least it's not Java's Date, DateTime, Time or Calendar object...

    It shares a few WTFs with Java's Date object. But if JodaTime gets into JDK8, will JavaScript get JodaTime then too? :)

    JodaTime is definitely the most consistent Date/Time API i've ever seen across any programming languages. One of its best design goals is that you you can (and have to) decide if you want to work with timezone-free time (where 01:00 + 3 hours will always be 04:00) or in a timezone (where 01:00 + 3 hours can once a year be 05:00 or 03:00) - that alone can save yourself a lot of grief...

    Actually Joda time won't be in Java 8, JSR 310 might be in Java 8. The author of both explains it himself.



  • @mihi said:

    or in a timezone (where 01:00 + 3 hours can once a year be 05:00 or 03:00)

    This is TRWTF. Instead of making the clock move forward and backward and break all kinds of assumptions (like that an hour always unambiguously represents an instant in time) they could have just added or removed an hour in the day (so that it ends on 23:00 or 25:00). Sure, it would mean that sometimes hour > 24, but I can't see how this would be more problematic than the other way (and it's easier to understand for humans).



  • @spamcourt said:

    ...all kinds of assumptions (like that an hour always unambiguously represents an instant in time)...

    Those are TRWTF. Like for instance, assuming that minutes can't last several hours, because on some systems they can (I shit you not).



  • @henke37 said:

    You forgot the part where there is no way to set the object to the current time.
     

    myDate.setTime(new Date().getTime());

     



  • @gu3st said:

    Date.Now() returns the current time
     

    Date.Now() throws an error, because there is no Now() method.



  • @dhromed said:

    Date.Now() the Javascript engine throws an error, because there is no Now() only a now() method.

    fTFY



  • @TGV said:

    only a now() method.
     

    wooo!



  • @Musaran said:

    getYear() returns... the year part of timestamp ? YES, but on 2 digits. As a string then ? Nope : as an integer.

    I thought it was "years since 1900". Which is why it currently returns 112. But IIRC IE returns it as the full year? I remember websites in January 2000 displaying the date as "19100" or "192000" on Netscape and IE respectively, since they used string concatenation instead of addition. Others got "3900" on IE. Whoops!

    @Musaran said:

    getFullYear() returns... the full year ? Not quite : on 4 digits, because y10k bug quota is not met yet.

    Well we have 7988 years to ensure it's a integer and will return "10000" when the time is right?



  • @Musaran said:

    getFullYear() returns... the full year ? Not quite : on 4 digits, because y10k bug quota is not met yet.

    No. That would be a browser limitation. For instance:

    new Date(36e4 + Date.parse("9999-12-31T23:59")

    will present itself as being on "Sat Jan 01 10000" in Firefox. The getFullYear method will also correctly return the integer value 10000. However you should not try to use Date.parse on the string representation "+10000-01-01" just yet, because that will still return NaN.

    (Extension into years of 5 or more digits is an optional part of ISO 8601 and Firefox does not support it when parsing dates. Or atleast; not yet.)

    @Musaran said:

    The UTC equivalent of parse() is... parseUTC() ? Try again : UTC().

    And this is also not correct. The signature is Date.UTC(year, month[, date[, hrs[, min[, sec[, ms]]]]]) and you use it together with the regular Date constructor to specify a component-wise date time in UTC instead of the local timezone. E.g.

    
    // Start of December 31 2012 in UTC
    new Date(Date.UTC(2012,12,31))
    
    // Start of December 31 2012 in the local timezone
    new Date(2012,12,31)
    

    (I still agree with you that JavaScript's Date is WTF worthy, though...)



  • @spamcourt said:

    @mihi said:
    or in a timezone (where 01:00 + 3 hours can once a year be 05:00 or 03:00)
    This is TRWTF. Instead of making the clock move forward and backward and break all kinds of assumptions (like that an hour always unambiguously represents an instant in time) they could have just added or removed an hour in the day (so that it ends on 23:00 or 25:00). Sure, it would mean that sometimes hour > 24, but I can't see how this would be more problematic than the other way (and it's easier to understand for humans).
     

    Actually, if you store a combination of local time + local timezone, that IS unambiguous.

    Daylight saving time doesn't actually change the time within a time zone, it changes a geographical region from one time zone to another.  So a state in Central Standard Time (GMT - 6) moves to Central Daylight Time (GMT - 5) and back.  If you store whether the instant was in CST or CDT it's unambiguous.  In the fall, five minutes after 1:58 AM CDT is 1:03 AM CST.

    It's only ambiguous if you store local time without a timezone.

     



  • @Cat said:

    @spamcourt said:

    @mihi said:
    or in a timezone (where 01:00 + 3 hours can once a year be 05:00 or 03:00)

    This is TRWTF. Instead of making the clock move forward and backward and break all kinds of assumptions (like that an hour always unambiguously represents an instant in time) they could have just added or removed an hour in the day (so that it ends on 23:00 or 25:00). Sure, it would mean that sometimes hour > 24, but I can't see how this would be more problematic than the other way (and it's easier to understand for humans).
     

    Actually, if you store a combination of local time + local timezone, that IS unambiguous.

    Daylight saving time doesn't actually change the time within a time zone, it changes a geographical region from one time zone to another.  So a state in Central Standard Time (GMT - 6) moves to Central Daylight Time (GMT - 5) and back.  If you store whether the instant was in CST or CDT it's unambiguous.  In the fall, five minutes after 1:58 AM CDT is 1:03 AM CST.

    It's only ambiguous if you store local time without a timezone.

     

    I tried to explain this to my boss the other day, and he didn't get it either: the difference between local time and UTC on one hand, and the timezone on the other, are two different concepts. Most countries don't span over more than one time zone, so they don't go around calling it a different timezone when the time changes. It's different for the U.S. because they span what, eight timezones? So it's understandable that they have lots of different names for times and then confuse those names for timezones. But AFAIK, New York City (which I believe is in the timezone you're talking about) doesn't suddenly up and move hundreds of miles, twice a year. It's going to be in the same location, and therefore in the same timezone, no matter what TLA you give the time.



  • @toon said:

    It's different for the U.S. because they span what, eight timezones?
     

    Four.

    @toon said:

    doesn't suddenly up and move hundreds of miles, twice a year.

    Wouldn't that be a sight!



  • @dhromed said:

    @toon said:

    It's different for the U.S. because they span what, eight timezones?
     

    Four.

    Are you sure about that? I knew the mainland spans four, but there's also Hawaii and Alaska. Maybe not eight but I'm pretty sure it's more than four.



  • @toon said:

    there's also Hawaii and Alaska.
     

    Everybody knows Hawaii and Alaska aren't really states.

     

    But yeah, looks like that makes six.


  • ♿ (Parody)

    @toon said:

    @dhromed said:
    @toon said:
    It's different for the U.S. because they span what, eight timezones?

    Four.

    Are you sure about that? I knew the mainland spans four, but there's also Hawaii and Alaska. Maybe not eight but I'm pretty sure it's more than four.

    It's actually <a href=http://aa.usno.navy.mil/faq/docs/us_tzones.php">nine when you include territories.



  • @dhromed said:

    @toon said:

    there's also Hawaii and Alaska.
     

    Everybody knows Hawaii and Alaska aren't really states.

     

    But yeah, looks like that makes six.

    A state where you can see the Rooskies from your porch: how American is that, really? I'll leave the answer to the reader.

    @boomzilla said:
    It's actually nine when you include territories.

    I just remembered that mine and dhromed's native country of the Netherlands, has a couple of municipalities in the Carribean. So we probably span a few as well...

    The other day, I read that Puerto Rico wants to be a state, and that Obama is willing to listen if the Puerto Rican people are really up for it. If they go through with that, then that ought to generate more than a few WTF's in U.S.A.'s IT-land. Think of all those SELECT elements that have to be manually updated...


  • ♿ (Parody)

    @toon said:

    A state where you can see the Rooskies from your porch: how American is that, really? I'll leave the answer to the reader.

    Sure, but only on Saturday Nights after 11:30. And it used to be much sillier.



  • @toon said:

    dhromed's native country of the Netherlands, has a couple of municipalities in the Carribean.
     

    I really don't count those.



  • @dhromed said:

    @toon said:

    dhromed's native country of the Netherlands, has a couple of municipalities in the Carribean.
     

    I really don't count those.

    It probably means we get to have mountains. We're always the only country without mountains. :(



  •  Mountains only get in the way of roadbuilding.



  • @dhromed said:

     Mountains only get in the way of roadbuilding.

    Not to mention the ocean. Another reason to discount the Caribbean.

    And don't even get me started on the Mid-Atlantic Ridge.

     



  •  Ocean biomes are annoying. You can't really mine the surface.



  • I'm sure those Delta boys can figure something out.



  • Oh right, how could I forget. A large part of your country was reclaimed from the sea.

    I suppose you could keep at it until you unify your territory. I don't think the UK would like being annexed though, so you'd have to go around them. I'm pretty sure the Royal Navy could defeat you just by making a couple of waves.



  • @Zecc said:

    I'm pretty sure the Royal Navy could defeat you just by making a couple of waves.
     

    Doubtful. This shit is designed to repel storms.



  • @Zecc said:

    Oh right, how could I forget. A large part of your country was reclaimed from the sea.

    I suppose you could keep at it until you unify your territory. I don't think the UK would like being annexed though, so you'd have to go around them. I'm pretty sure the Royal Navy could defeat you just by making a couple of waves.

    That wasn't even meant chauvinistically, it was just a joke. A couple of Dutch guys are talking about roads, and all of a sudden the country's at war with the UK! Sorry for hijacking the thread, I guess?



  • @toon said:

    That wasn't even meant chauvinistically, it was just a joke. A couple of Dutch guys are talking about roads, and all of a sudden the country's at war with the UK! Sorry for hijacking the thread, I guess?
    In retrospect I see my tone may have been sounded different, but I wasn't being sarcastic or anything, I was just joining in on the joke.

    Edit: I guess this goes to approve that self-deprecating humor is perfectly fine, but when used by other people it always sounds wrong. I had no intention to offend, I hope that's clear.



  • @Zecc said:

    @toon said:

    That wasn't even meant chauvinistically, it was just a joke. A couple of Dutch guys are talking about roads, and all of a sudden the country's at war with the UK! Sorry for hijacking the thread, I guess?
    In retrospect I see my tone may have been sounded different, but I wasn't being sarcastic or anything, I was just joining in on the joke.

    Edit: I guess this goes to approve that self-deprecating humor is perfectly fine, but when used by other people it always sounds wrong. I had no intention to offend, I hope that's clear.

    I'm just glad I didn't come across as a self-righteous look-at-us-Dutch-being-all-cool-with-water-y dickweed. There are more than enough chauvinistic folks on the Net as it is; no need for me to add to that particular onslaught of holier-than-thouiness. It's all good. Glad we got things straight. :)

    To be sure, self-deprecating humor can easily be misunderstood, especially without body language to accompany it. I do think it's the best kind though. I like people who don't take themselves too seriously.



  • @dhromed said:

    Mountains only get in the way of roadbuilding.

    Plus how is that little kid going to get to the dikes to plug them up with his fingers if he has to cross a mountain? Is that the right country? Eh, whatever, it's all europe-y bullshit anyway.

    The US spans virtually all (if not all) timezones, if you include our military bases and embassies which we count as US territory. (Depending on the lease agreement with the parent country, but generally they're considered US soil. Even the one in Cuba.) So if you all were planning out a product to serve US citizens and you limited it to 9 timezones, congratulations, you failed at it.



  • @blakeyrat said:

    The US spans virtually all (if not all) timezones, if you include our military bases and embassies which we count as US territory.
    I thought all embassies in general were considered territory of their respectives countries.

    But Wikipedia is telling me I was wrong.



  • @blakeyrat said:

    Plus how is that little kid going to get to the dikes to plug them up with his fingers if he has to cross a mountain? Is that the right country?

    Yes. Not a true story, btw.

    @blakeyrat said:
    Even the one in Cuba.

    Obama closed that one. Oh wait...



  • @Zecc said:

    I had no intention to offend, I hope that's clear.
     

    I wasn't.

    Offended, that was.

    As a Limey.


  • ♿ (Parody)

    @toon said:

    @blakeyrat said:
    Even the one in Cuba.

    Obama closed that one. Oh wait...

    To be accurate, he was never planning to close the base. Just the prison.



  • @boomzilla said:

    Filed under: <font color="#698d73">YOU CAN'T HANDLE THE TRUTH</font>
    Funny story, I watched that movie with my wife within the last year.  She had never seen it.  When that line came up she rolled her eyes and said "OMG that is so cheesy, I can't believe they used that."

    >.>



  • @Sutherlands said:

    @boomzilla said:

    Filed under: <font color="#698d73">YOU CAN'T HANDLE THE TRUTH</font>
    Funny story, I watched that movie with my wife within the last year.  She had never seen it.  When that line came up she rolled her eyes and said "OMG that is so cheesy, I can't believe they used that."

    >.>

    I'm not sure whether to file that under the woman who finally saw Hamlet and didn't like it because "it's just a bunch of cliches strung together", or with the movie producer pitching his brilliant idea to the studio head "we'll do The Wiz, only White!"

     


Log in to reply