-1

How do I convert this:

2017-08-14T23:28:56.782Z

Into relative time e.g. "21d". If there is an npm dependency I can use that too.

Hassan Imam
  • 21,956
  • 5
  • 41
  • 51
  • 2
    i would look at using `moment`. That is the main js time library – Paul Fitzgerald Sep 04 '17 at 15:40
  • You can get a vanilla Javascript `Date` object doing simply `var date = new Date('2017-08-14T23:28:56.782Z');`, but doing the relative time could get messy. – jorgonor Sep 04 '17 at 15:41
  • Possible duplicate of [Get difference between 2 dates in JavaScript?](https://stackoverflow.com/questions/3224834/get-difference-between-2-dates-in-javascript) – Hassan Imam Sep 04 '17 at 15:42

4 Answers4

0

there are some useful library like momentjs which will easy the way.

var a = moment([2007, 0, 29]);
var b = moment([2007, 0, 28]);
a.diff(b, 'days') // 1
Fazal Rasel
  • 4,446
  • 2
  • 20
  • 31
0

Using moment.js, you can do moment("2017-08-14T23:28:56.782Z").fromNow();. This will give you 21 days ago

console.log(moment("2017-08-14T23:28:56.782Z").fromNow());
<script src='https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js'></script>
Hassan Imam
  • 21,956
  • 5
  • 41
  • 51
Yanki Twizzy
  • 7,771
  • 8
  • 41
  • 68
0

If you mean 21 days ago, then Moment.js is convenient for things like that. There is also react-datetime, but I don't have any experience using it with React, so I can't comment on its efficacy or ease of use.

Daniel Ertel-Moore
  • 91
  • 1
  • 1
  • 12
0

Use moment.js.
npm install moment

moment([2017, 9, 4]).fromNow(); 
codejockie
  • 9,020
  • 4
  • 40
  • 46