-1

How to format date to some weeks ago with moment js relative time? For example, as of today moment("20150306", "YYYYMMDD").fromNow(true); would return a year. But I'd like it return 52 weeks, similar to instagram dates. Any tips for a custom function to convert months and years to weeks? Thanks

Zaya
  • 41
  • 1
  • 7
  • 1
    Possible duplicate of [How to get duration in weeks with Moment.js?](http://stackoverflow.com/questions/11448340/how-to-get-duration-in-weeks-with-moment-js) – Rax Mar 05 '16 at 16:58
  • Well I looked at the answers on that question, no selected answer nor any of them helped me. So I posted this one here and I got the answer. I don't see any problem. – Zaya Mar 05 '16 at 17:17

1 Answers1

0

Use moment.duration().asWeeks and the following:

var a = Math.round(moment.duration(moment("20150306", "YYYYMMDD").diff(moment())).asWeeks()) * -1;
alert(a + (a > 1 ? ' weeks ago' : ' week ago'));

DEMO

baao
  • 71,625
  • 17
  • 143
  • 203