Results 1 to 4 of 4

Thread: Comparing for equality on Date objects

  1. #1

    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,444
    Answers
    3997

    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 Fullstack Engineer
    ________________
    Modus Create is based on the model of an open source team. We’re a remote, global team of experts in our field. To find out more about the work we do, head over to our website.

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

  3. #3

    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

    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
  •