nehas
nehas

Reputation: 247

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."

Upvotes: 2

Views: 11624

Answers (2)

Teddy Cross
Teddy Cross

Reputation: 321

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();

Upvotes: 7

Amadan
Amadan

Reputation: 198324

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.

Upvotes: 0

Related Questions