moment.js to compare 2 dates

7,273 views
Skip to first unread message

Satish B

unread,
Feb 25, 2015, 5:26:41 PM2/25/15
to nod...@googlegroups.com
I'm using node.js and npm moment.js

I've 2 dates in the following format:
Thu Feb 25 2015 02:57:08 GMT+0530 (India Standard Time)
Thu Feb 26 2015 03:34:29 GMT+0530 (India Standard Time)

I need to check if there is at least 30 min difference between the 2 timigns.

Moment.js http://momentjs.com/

Thanks ..

Hartti Suomela

unread,
Feb 26, 2015, 10:04:44 AM2/26/15
to nod...@googlegroups.com
Are you asking how to do that?
If yes, then (even though I am not familiar with moment.js, meaning that there could be more eloquent way to achieve this and there could be also simpler ways to check this using JavaScript core functionality)

With that you can check if there timestamp2 is between timestamp1-30min and timestamp1+30min. If this is false then there is at least 30 minutes between the two timestamps (=your test is the negation of the above test case)

Hartti

--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
To post to this group, send email to nod...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/fea454b2-2e60-41a1-9a0e-cf8c79d2a73d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Daniel Rinehart

unread,
Feb 26, 2015, 10:05:06 AM2/26/15
to nodejs
// note with the "(India Standard Time)" at the end of the string it won't pass a strict moment parse check
moment('Thu Feb 25 2015 02:57:08 GMT+0530 (India Standard Time)', 'ddd MMM D YYYY HH:mm:ss Z', true).isValid(); // false
// if you relax it, it works
var a = moment('Thu Feb 25 2015 02:57:08 GMT+0530 (India Standard Time)', 'ddd MMM D YYYY HH:mm:ss Z');
a.isValid(); // true
var b = moment('Thu Feb 26 2015 03:34:29 GMT+0530 (India Standard Time)', 'ddd MMM D YYYY HH:mm:ss Z');
b.isValid(); // true
Math.abs(a.diff(b, 'minutes')) >= 30; // true

--

Satish B

unread,
Feb 27, 2015, 4:54:12 PM2/27/15
to nod...@googlegroups.com
Thanks @Hartti and @Daniel.

I used diff() and could get the result.

I changed the date format and removed the Standard Timing information and I was able to get the results.

Thanks a lot once again :)
Reply all
Reply to author
Forward
0 new messages