简体   繁体   中英

How to show time difference between unix timestamp and current date in moment.js?

I am using moment.js plugin to show updated times for entries in my application. All of the entries have a time_updated property in unix timestamp format. How can I take this time format and compare it to the current time. I have looked through the documentation and found this:

moment([2015, 0, 29]).fromNow()

But no where does it explain what the numbers between the brackets mean or what format should the parameters be in.

Below is the code I am working with

var timestamp = clientObject.topics_projects[x].date_updated;
var then = moment.unix(timestamp).format("HH:mm");
var now = moment(currentDate).format("HH:mm");

So how can I get the time difference between then and now? For example to read, "a few seconds ago" or "3 minutes ago."

Your answer is already in your code. Moment is chainable, so:

var diff = moment.unix(timestamp).from(currentDate);

Or if currentDate is actually the current date, simply:

var diff = moment.unix(timestamp).fromNow();

Look up the various moment constructor formats for the meaning of moment([2015, 0, 29]) (specifically the Array[] one ).

For your specific question,

moment(something).fromNow()

where something can be a variety of things, including a timestamp, a date object, the abovementioned array of time quantities, and much more.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM