SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Jun 2012
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Why does comparing two dates that are exactly the same produce a false result?

    I got two questions about JavaScript Date objects.

    1. I've created two date objects in my code and from all "appearances" they are the same values, yet, a comparison with the equality operator(==) returns the boolean value of false. Here they are as shown in Chrome's JavaScript console:

    beginEditingLastUpdate;
    Tue Mar 12 2013 18:10:56 GMT-0500 (Central Daylight Time)

    endEditingLastUpdate;
    Tue Mar 12 2013 18:10:56 GMT-0500 (Central Daylight Time)

    Those were copied'n'pasted right out of the console. So I am confused on this one.

    2. Why do all my Date objects show a protype as "InvalidDate". I have created date objects by simply calling the Date() constructor and they still have a prototype(actually __proto__ if it matters) of "InvalidDate". What's up with this?

    Thanks for all the help.

  2. #2
    SitePoint Member
    Join Date
    Feb 2013
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My first thought is try logging to the console with beginEditingLastUpdate.getTime() as this will give you a result accurate to the microsecond, instead of the iso-format string which is only displays with an accuracy of one second. I'd say there's a difference between the two times but it's less than one second.

    I'm not sure why you're seeing invalid date, however if you're calling new Date() - ie with the new keyword and no arguments - it would certainly be unusual to see InvalidDate. If you're calling new Date(some_value) please provide what some_value is.

    edit: If you can provide the context of how you're using yourdate.__proto__ I may be able to better respond to part 2 of your question. As a guess, if you're checking if an object is a date, try using

    yourdate instanceof Date // returns true if yourdate is a Date object


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •