fatCop fatCop - 1 year ago 148
Javascript Question

How to get time using Moment JS

I've two javascript variables:

. I want to store the current time(unix timestamp in ms) to
and timestamp of 24 hours before in
. I'll use this two variables to query the records for last 1 day. How can I do that using moment JS?

Answer Source

Have you looked at the website yet? It's full of examples - http://momentjs.com/ I think what you are trying to do is as simple as

var startDate = moment(endDate).subtract(1, 'days');

Following your question more literally, you can do this:

var endDate = moment(); //the current time

Or, you can just ignore the endDate part of this problem and go straight to startDate with

var startDate = moment().subtract(1, 'days'); //one day before the current time

Finally, if you need to format it a certain way, you can add that after the subtraction as such:

moment().subtract(1,'days').format('YYYY-MM-DD h:mm:ss a')

Use format without an argument and it gives you a iso format

moment().subtract(1,'days').format() //such as "2015-04-04T01:53:26-05:00"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download