简体   繁体   English

如何使用moment.js获取24小时内日期之间的时差?

[英]How can I get the hour difference between dates within 24 hours with moment.js?

I have an object with the field of "created' with a date in which the object was created. Now I want to keep track of "How many hours ago" the object was created with moment.js . 我有一个对象,其字段为“创建”,其创建日期为创建日期。现在,我想跟踪使用moment.js创建的对象“多少小时前”。

My goal is to have the time display like the example below. 我的目标是像下面的示例一样显示时间。

Created: 1hr ago

Created: 5hrs ago

Created: 9hrs ago

Created: 12hrs ago

Created: 14hrs ago

Created: 18hrs ago

Created: 24hrs ago

All the way within a 24 hour day. 一天24小时之内。

So I tried this with moment: 因此,我尝试了一下:

    var $created = "2016-05-01T09:58:26.796Z";
    var $now = new Date();

    var $lastCreatedHour = moment( moment( moment().format('H') ).diff( moment( $created ).format('H') ) ).format('H') + " hrs ago";

console.log("Created: " + $lastCreatedHour);

My logic here is simple, just tried getting the difference between the current times hour and the $created hour . 我的逻辑很简单,只是尝试获取当前时间hour$ created hour之间差。

I actually tried many different things and none of them worked either they are a number off or they wont work within a 24 hour day and only within a 12 hour span. 我实际上尝试了许多不同的方法,但是它们都不起作用,或者是数量不多,或者它们将不会在24小时之内且仅在12小时之内起作用。

JSFIDDLE JSFIDDLE

I'm not the best with Dates help is appreciated! 我不是最好的日期帮助表示赞赏!

Thank you. 谢谢。

If you are looking for all times to be in hours, then the answer from stdob-- is fine. 如果您希望所有时间都在几小时内,那么stdob--的答案就很好。

It is worth noting that Moment has .fromNow() built in. Simply take the date string for when your item was created, pass that to the moment constructor, and call the function: 值得注意的是,Moment内置了.fromNow() 。只需将日期字符串作为创建项目的时间,将其传递给当下的构造方法,然后调用函数:

moment($created).fromNow();

As a practical example: 作为一个实际的例子:

moment("2016-05-01T09:58:26.796Z").fromNow()
"2 hours ago"

or, for longer ago: 或者,更长时间之前:

moment("2016-04-28T09:58:26.796Z").fromNow()
"3 days ago"

This function will give a little more user friendly output, going from 'a few seconds ago' to 'x minutes ago' to 'y hours ago' and into days, months and years. 此功能将提供更多用户友好的输出,从“几秒钟前”到“ x分钟前”再到“ y小时前”,再到几天,几个月和几年。

The output text of fromNow can be customized. fromNow的输出文本可以自定义。 See http://momentjs.com/docs/#/displaying/fromnow/ for the basic documentation and http://momentjs.com/docs/#/customization/relative-time/ for customization instructions. http://momentjs.com/docs/#/displaying/fromnow/的基本文件和http://momentjs.com/docs/#/customization/relative-time/定制指令。

As a final best practice note, it should never be necessary to use a javascript Date object with Moment.js unless you have a third party API that requires a date. 作为最后的最佳实践注释,除非您有需要日期的第三方API,否则永远不需要在Moment.js中使用javascript Date对象。 For what you are doing though, no Date needed. 对于您正在执行的操作,不需要日期。

If you don't need to check the date as well, this may be the answer for your question. 如果您也不需要检查日期,这可能是您问题的答案。 Also, I'd like to say, your function chaining was not necessary IF (as I already said) this is what you'd like to achieve. 另外,我想说的是,如果要实现此功能,则不必(如我已经说过的那样)进行函数链接。

My result would look like this: 我的结果看起来像这样:

var $created = moment("2016-04-30T00:58:26.796Z");
var $now = moment();
var $lastFeedHour = moment($now.diff($created)).hour();

console.log("Created: " + $lastFeedHour + " hrs ago");

You can also check it live here . 您也可以在此处实时查看

var $created = "2016-04-30T10:58:26.796Z";
var $now = "2016-05-01T10:58:26.696Z";
var $lastFeedHour =  moment( moment().diff( moment( $created ) ) ).format('H');

console.log("Created: " + $lastFeedHour + " hrs ago");

JS Fiddle JS小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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