Home > Mobile >  Analyzing Date difference between two dates to text : "before: x Days, x Hours, x Minutes"
Analyzing Date difference between two dates to text : "before: x Days, x Hours, x Minutes"

Time:10-04

I have done a lot of research to find code examples for this, or simillar question but I didn't find any.

I have a react app that has some customer requests in it, and I want to show next to each request something like: 'before: 2 Days, 4 Hours, 56 Minutes.'

I want to calculate the difference between the current Date and the time the request was sent, and analyze this difference to days, hours, and minutes.

note: The date difference won't be longer than one month.

I know that I can do: dt1 - dt2 to get the difference, But this returns a long integer which is hard to analyze.

Edit: It suppose to be like this:

days   hours   minutes == difference

So if you try to convert days,hours,minutes to milliseconds and sum them together, it should result simillar difference of (d1-d2)

Any ideas? Thanks for the help!

CodePudding user response:

What you are looking for better human readability. There are multiple packages that would achieve that.

For your use case user this https://www.npmjs.com/package/humanize-duration

humanizeDuration(dt1-dt2) // Will result in what you want
  • Related