1

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?

Ammar Khan
  • 2,565
  • 6
  • 35
  • 60

1 Answers1

2

"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"
Upperstage
  • 3,747
  • 8
  • 44
  • 67
  • I am running into an issue. Could you please help me again. I have an incoming date from server 2014-07-01T15:30:00Z and I want to take difference from the current time. How can I make it possible, I just can not be able to get the exact difference. – Ammar Khan Jul 01 '14 at 15:17
  • See this question for how to parse a string into a moment: http://stackoverflow.com/questions/17093796/date-formatting-with-without-moment-js – Upperstage Jul 01 '14 at 16:21
  • See this question for how to perform math on moments: http://stackoverflow.com/questions/24428656/subtract-time-from-date-moment-js – Upperstage Jul 01 '14 at 16:22