0

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!

Tehila
  • 917
  • 4
  • 19
  • Does this answer your question? [Convert seconds to days, hours, minutes and seconds](https://stackoverflow.com/questions/36098913/convert-seconds-to-days-hours-minutes-and-seconds) – derpirscher Oct 04 '22 at 07:02
  • I tried this but it's not sutiable for my case because I want to convert a **difference** to this structure, not just some date which is given in milliseconds. – Tehila Oct 04 '22 at 07:12
  • it's exactly your case. `dt1-dt2` is the number of milliseconds (ie the *difference*) between the two dates and the linked duplicate converts a number of seconds (I'm sure you are able to convert ms to seconds) to the respective number of days hours minutes and seconds – derpirscher Oct 04 '22 at 07:20
  • Yes, I know, I did that. What I meant is :what should I do after I converted to `days, hours, minutes` ? I get **all** hours in between the dates, and **all** minutes - which is not what I want. I need it to be : `days+hours+minutes = diffrence` - the sum of all together will be the diffrence. So how can I calculate the milliseconds to result this? – Tehila Oct 04 '22 at 07:35
  • What do you mean by *all* hours and *all* minutes. For something like `123456` seconds you get 34 hours and 2057 minutes? Then you are not doing it as proposed in the linked duplicate ... – derpirscher Oct 04 '22 at 09:12

1 Answers1

1

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