How to Customize Humanized Moment js Date Result

2019-03-01 09:37发布

问题:

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

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?

回答1:

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

Moment documentation

So,

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"


标签: momentjs