-3

I have a certain format which is in the

{
    "updatedAt": "2022-01-04 22:41:32.268897",
    "status": "open",
    "createdAt": "2022-01-04 22:41:26.328339"
}

I want to get the difference in hours or minutes of both the dates I tried many but didn't get the expected result.

Ridham Tarpara
  • 5,970
  • 4
  • 19
  • 39

1 Answers1

0

You can use the Date module in javascript.

const obj = { "updatedAt": "2022-01-04 22:41:32.268897", "status": "open", "createdAt": "2022-01-03 22:41:26.328339" };

const updatedAt = new Date(obj.updatedAt);
const createdAt = new Date(obj.createdAt);

const diffInMilliSecs = updatedAt - createdAt;

This difference is in milliseconds. You can now convert this into seconds or minutes or hours.

abhishekti7
  • 70
  • 1
  • 5
  • Re `new Date(obj.updatedAt)`, see [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) "2022-01-04 22:41:32.268897" is not a format supported by ECMA-262 so parsing is implementation dependent. It produces an invalid Date in Safari at least. – RobG May 31 '22 at 12:21