最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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

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

Share Improve this question edited Sep 9, 2015 at 3:19 RobG 148k32 gold badges179 silver badges214 bronze badges asked Sep 9, 2015 at 3:14 nehasnehas 2472 gold badges5 silver badges18 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 7

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.

发布评论

评论列表(0)

  1. 暂无评论