Moment.js如何使用fromNow()在几小时内返回所有内容? [英] Moment.js how to use fromNow() to return everything in hours?

查看:790
本文介绍了Moment.js如何使用fromNow()在几小时内返回所有内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在搜索 moment.js docs stackoverflow 有一种方法可以使用 fromNow()函数,但可以在几小时内返回所有内容。



我的意思是:

 时刻([2017,01] ,05])。fromNow(); //一天前

应该是

 时刻([2017,01,05])。fromNow(); // 24小时前

我知道可以使用 .diff执行此操作以及其他类似的函数然后添加文本,但是可以使用 .fromNow()来执行此操作吗?

解决方案

您可以使用 relativeTimeThreshold 自定义时刻相对时间的阈值。



正如文档所说:


duration.humanize 具有定义单位何时被视为分钟的阈值,一个小时等等。例如,默认情况下,超过45秒被视为一分钟,超过22小时被视为一天,依此类推。要更改这些截止值,请使用 moment.relativeTimeThreshold(unit,limit)其中unit是 s 之一, m h d M


在您的情况下,您可以增加小时阈值以将相对天数作为小时数。这里有一个工作示例,显示从1小时到26天的小时数:



  var m1 = moment()。subtract(5,'h'); var m2 = moment()。subtract(55,'h'); var m3 = moment()。subtract(1,'d'); //默认resultsconsole.log(m1.fromNow()); console.log(m2.fromNow()); console.log(m3.fromNow()); //更改relativeTimeThresholdmoment.relativeTimeThreshold('m',60);时刻。 relativeTimeThreshold('h',24 * 26); //结果以hoursconsole.log(m1.fromNow()); console.log(m2.fromNow()); console.log(m3.fromNow());  

 < script src =// cdnjs.cloudflare.com/ AJAX /库/ moment.js / 2.17.1 / moment.min.js>< /脚本>  



请注意,如果需要,可以让你更进一步定制相对时间 relativeTime 此处我的一个示例)和 relativeTimeRounding 方法。


I've searching the moment.js docs and stackoverflow for a way to use the fromNow() function but returning everything in hours.

What I mean is:

moment([2017, 01, 05]).fromNow();     // a day ago

should be

moment([2017, 01, 05]).fromNow();     // 24 hours ago

I know it's possible to do this using .diff and probably other similar functions and then adding the text, but is it possible to use .fromNow() to do this?

解决方案

You can use relativeTimeThreshold to customize thresholds for moment relative time.

As the docs says:

duration.humanize has thresholds which define when a unit is considered a minute, an hour and so on. For example, by default more than 45 seconds is considered a minute, more than 22 hours is considered a day and so on. To change those cutoffs use moment.relativeTimeThreshold(unit, limit) where unit is one of s, m, h, d, M.

In your case, you can increase hour thresholds to get relative days as hours. Here a working example showing time as hours from 1 hour to 26 days:

var m1 = moment().subtract(5, 'h');
var m2 = moment().subtract(55, 'h');
var m3 = moment().subtract(1, 'd');

// Default results
console.log(m1.fromNow());
console.log(m2.fromNow());
console.log(m3.fromNow());

// Change relativeTimeThreshold
moment.relativeTimeThreshold('m', 60);
moment.relativeTimeThreshold('h', 24*26);

// Results in hours
console.log(m1.fromNow());
console.log(m2.fromNow());
console.log(m3.fromNow());

<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>

Note that, if you need, moment lets you customize relative time further with relativeTime (here one of my examples) and relativeTimeRounding method.

这篇关于Moment.js如何使用fromNow()在几小时内返回所有内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆