简体   繁体   中英

How can I get the difference in the two dates in js which are in string?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM