简体   繁体   English

如何获得 js 中字符串中两个日期的差异?

[英]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.您可以在 javascript 中使用 Date 模块。

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.您现在可以将其转换为秒或分钟或小时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何计算不同日期的 24 小时格式的两个时间之间的差异? - How can I calculate the difference between two times that are in 24 hour format which are in different dates? 我可以通过momentjs得出年份和月份中两个日期之间的差额吗? - Can I get the difference between two dates in years and months with momentjs? 如何以小时为单位计算两个日期之间的差异,省略周末的时间? - How can I get the difference between two dates in hours, omitting the hours on weekends? 使用字符串输入获取两个日期之间的差异 - Get difference between two dates with inputs of string 如何使用moment.js获取24小时内日期之间的时差? - How can I get the hour difference between dates within 24 hours with moment.js? 如何获得 JavaScript 中两个日期之间的差异? - How do I get the difference between two Dates in JavaScript? 如何获取JavaScript中两个日期之间的差值天? - How do I get the difference days between two Dates in JavaScript? 如何以分钟为单位计算两个日期之间的时差 - How can I calculate difference between two dates with time in minutes 如何使用正则表达式在javascript中的字符串中获取两个日期中的第二个? - How can I use regex to get the second of two dates in a string in javascript? 如何在netsuite中区分两个日期? - How to get difference between two dates in netsuite?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM