Results 1 to 4 of 4

Thread: Comparing for equality on Date objects

  1. #1
    Sencha User
    Join Date
    Jan 2009
    Posts
    11
    Vote Rating
    0
      0  

    Question Comparing for equality on Date objects

    Hello, i have a simple problem, and question:

    How do you ask if two Date objects are the same?

    I am writing simple code that compares two dates created exactly the same:

    However after performing a simple assertion {throw exception if (!=) } they fail and return me this message:

    Code:
    assert(new Date('3/12/2012 13:05:01 GMT-0600'), new Date('3/12/2012 13:05:01 GMT-0600') , 'CALENDAR property');
    AssertException: CALENDAR property: 'Mon Mar 12 2012 13:05:01 GMT-0600 (CST)' != 'Mon Mar 12 2012 13:05:01 GMT-0600 (CST)'

    Thanks in advance

  2. #2
    Sencha Premium User mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    40,385
    Answers
    3997
    Vote Rating
    1540
      0  

    Default

    This returns true:

    Code:
    new Date('3/12/2012 13:05:01 GMT-0600') !== new Date('3/12/2012 13:05:01 GMT-0600')
    This returns false:

    Code:
    new Date('3/12/2012 13:05:01 GMT-0600') === new Date('3/12/2012 13:05:01 GMT-0600')
    This is because it is checking to see if it is the same Object not same value.

    So you can format the date objects to unix timestamp ('U') and compare that.
    Mitchell Simoens @LikelyMitch
    Modus Create, Senior Frontend Engineer
    ________________
    Need any sort of Ext JS help? Modus Create is here to help!

    Check out my GitHub:
    https://github.com/mitchellsimoens

  3. #3
    Sencha User
    Join Date
    Jan 2009
    Posts
    11
    Vote Rating
    0
      0  

    Default Thanks

    Thank you, that worked really well.

    I really don't see much use of comparing dates by reference.

  4. #4
    Sencha User
    Join Date
    Feb 2012
    Posts
    7
    Vote Rating
    0
      0  

    Default

    try to change any element of date:


    new Date('3/12/2012 14:05:01 GMT-0600') !== new Date('3/12/2012 13:05:01 GMT-0600')

    result is still TRUE

Tags for this Thread

Posting Permissions

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