0

I have 2 Epoch times, e.g. 1673252582, 1673253317

Now i am trying to calculate Difference in seconds between these two using date-fns: differenceInSeconds(1673252582, 1673253317).

But this is giving me -0 as result.

Please help.

raju
  • 6,448
  • 24
  • 80
  • 163
  • 1
    const time1 = 1673252582; const time2 = 1673253317; const seconds_diff = (time1 - time2)/1000; // Epochs are just times in milliseconds from 1 January, 1979. – Henry Hunt Jan 09 '23 at 08:45
  • The difference between those 2 timestamps is less than 1 second – Evert Jan 09 '23 at 09:41

1 Answers1

1

You can calculate the diff by subtracting one timestamp from the other.

If you need it in seconds and current input is in milliseconds, you will need to convert milliseconds to seconds by dividing by 1000.

for example:

const diffInSeconds = (timestampA, timestampB) => {
  //  absolute value added incase you just want the diff but don't care which came first
  return (Math.abs(timestampB - timestampA)) / 1000
}

const res = diffInSeconds(1673256249000, 1673256240000)
console.log(res) // 9
tamir maoz
  • 36
  • 3