Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display the number of years and month since a date using moment.js

Tags:

momentjs

I want to display something like this :

member for 1 year, 8 month

The example on the momment.js web site is :

moment("20120620", "YYYYMMDD").fromNow(); // 2 years ago.

How to display also the number of month since the provided date ?

Like : 2 years, 6 months

Thank you

like image 251
Merlin Avatar asked Dec 19 '25 01:12

Merlin


1 Answers

Ok I found the answer so I post it in case it can be useful for others:

var date = new Date(2011,5,24)   
var month = date.getMonth()
var year = date.getFullYear();

var dateString = moment(year, "YYYY").fromNow(true) + 
                 moment(month, "MM").fromNow(true)) +  " ago" 

console.log(dateString)
like image 138
Merlin Avatar answered Dec 21 '25 09:12

Merlin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!