2

I have a date '2019-05-08T22:15:00-0400'. I want to compare this date with the current date and time . if the above date and time is less than current date and time then i have to do something .

My code is :-

 if(moment('2019-05-08T13:00:00-0400') < moment()){
       //do something
     }

But it returns false .

  • print your moment value and see if your given time is correct – Syed mohamed aladeen May 08 '19 at 13:08
  • I am getting moment('2019-05-08T13:00:00-0400') -- "Wed May 08 2019 22:30:00 GMT+0530 (India Standard Time) {} " and moment() - Wed May 08 2019 18:41:42 GMT+0530 (India Standard Time) {} . But for this moment('2019-05-08T13:00:00-0400') - it is showing time 22.30 . Why ? – priyadarshini puja May 08 '19 at 13:12
  • 2
    Possible duplicate of [Moment js date time comparison](https://stackoverflow.com/questions/22600856/moment-js-date-time-comparison) – Oden May 08 '19 at 13:23
  • actually when i compare moment('2019-05-08T13:00:00-0400') with current date .It's showing difference of 8.30 hrs. Is there any way to convert both the date into same format. – priyadarshini puja May 08 '19 at 18:04

2 Answers2

1

The original code in the question is correct. You were running it at a time where it evaluated to false.

(I'm guessing your timezone is something like +04:30, explaining the 8:30 difference?)

searlea
  • 8,173
  • 4
  • 34
  • 37
1

You can try Date constructor

if (new Date('2019-05-08T22:15:00-0400') < new Date()) {
  // Do something
}

I hope this works for you.

Tejasva Dhyani
  • 1,342
  • 1
  • 10
  • 19
  • I have tried this but it's not working . Actually new Date('2019-05-16T14:15:00-0400') = Thu May 16 2019 23:45:00 GMT+0530 (India Standard Time) . it's 8:30hrs difference between 14:15:00 and 23:45:00. – priyadarshini puja May 16 '19 at 12:34