如何自定义人性化时刻js日期结果 [英] How to Customize Humanized Moment js Date Result

查看:58
本文介绍了如何自定义人性化时刻js日期结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用力矩js定制人性化的日期结果.假设我有个约会,我想利用剩余的时间在3个月内"返回结果

I want to customize the humanize date result with moment js. Let's say I have a date and I want to take the remaining time that return back a result "in 3 months"

moment("20141001", "YYYYMMDD").fromNow(); // in 3 months

如何自定义生成的字符串,如3 months left?时刻是否提供了任何外部参数来定制结果,还是我可以实现这一目标的任何其他方式?

How can I customize the resultant string like 3 months left ? Is moment provide any external parameter to customize the resultant or any other way I can achieve this?

推荐答案

就像moment#fromNow一样,当第二个参数返回不带后缀的值时传递true.这在需要人类可读的时间长度的地方很有用. "

"Like moment#fromNow, passing true as the second parameter returns value without the suffix. This is useful wherever you need to have a human readable length of time."

时刻文档

所以

var start = moment([2007, 0, 5]);
var end = moment([2007, 0, 10]);
start.from(end);                   // "in 5 days"
start.from(end, true);             // "5 days"
start.from(end, true) + " left";   // "5 days left"

这篇关于如何自定义人性化时刻js日期结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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