-1

Please I received a response from a REST API, and in the JSON object, the time value was a timestamp. However, I need to render it as a "number of days" kind of format.

I received this particular time stamp: "2022-01-12T13:05:16Z"

I want to render it as something like "5 days ago"

How do I go about this please?

General Grievance
  • 4,555
  • 31
  • 31
  • 45
  • 3
    What did you try? What *didn't* you find when googleing this problem? Please see [ask]. – 0stone0 Apr 29 '22 at 14:25
  • Calculate how long ago it was, round it to days, and then display it. – Kevin B Apr 29 '22 at 14:28
  • Can you give a code example please? – Emmanuel Eboh Apr 29 '22 at 14:28
  • Does this answer your question? [How to format time since xxx e.g. “4 minutes ago” similar to Stack Exchange sites](https://stackoverflow.com/questions/3177836/how-to-format-time-since-xxx-e-g-4-minutes-ago-similar-to-stack-exchange-site) – pilchard Apr 29 '22 at 15:18
  • Also the built-in [Intl.RelativeTimeFormat.prototype.format()](https://developer.mozilla.org/en-US/docs/web/javascript/reference/global_objects/intl/relativetimeformat/format) – pilchard Apr 29 '22 at 15:19

1 Answers1

1

transform the current date and the date you want to milliseconds with .getTime() then subtract it then split the result to milliseconds in a day and you get the number of days passed, Math.ceil to round the number up

console.log(Math.ceil((new Date().getTime() - new Date("2022-01-12T13:05:16Z").getTime()) / 86400000))
codmitu
  • 636
  • 7
  • 9