英文:
Find the difference between two dates in js?
问题
我想找到两个日期之间的差异,
我有一个包含对象的数组,这些对象包括一个startDate和endDate。
我一直在尝试这个。
testData.forEach((item) => {
const { endDate, startDate } =
item;
if (workflow_instances_timestamp && workflow_instances_modified_at) {
let startDate_ = new Date(startDate );
let endDate_ = new Date(endDate);
const seconds =
Math.abs(endDate_.getTime() - startDate_.getTime()) / 1000;
console.log(startDate, "startDate");
console.log(endDate, "endDate");
console.log(seconds % 60, "seconds");
}
});
数据包含这个数组
const items = [{endDate: "Fri Jun 30 2023 13:32:05 GMT+0200 (Central European Summer Time)", startDate: "Fri Jun 30 2023 15:31:51 GMT+0200 (Central European Summer Time)"}]
通过这段代码提供的秒数显示为46秒。
这是不正确的。
非常感谢。
英文:
I want to find the difference between two dates,
I have an array which contains objects that include a startDate and endDate.
I have been trying this.
testData.forEach((item) => {
const { endDate, startDate } =
item;
if (workflow_instances_timestamp && workflow_instances_modified_at) {
let startDate_ = new Date(startDate );
let endDate_ = new Date(endDate);
const seconds =
Math.abs(endDate_.getTime() - startDate_.getTime()) / 1000;
console.log(startDate, "startDate");
console.log(endDate, "endDate");
console.log(seconds % 60, "seconds");
}
});
the data contains this array
const items = [{endDate: "Fri Jun 30 2023 13:32:05 GMT+0200 (Central European Summer Time)", startDate: "Fri Jun 30 2023 15:31:51 GMT+0200 (Central European Summer Time)"}]
with this code I have provided the seconds are showing is 46 seconds.
Which its not correct.
Thanks a lot.
答案1
得分: 0
这将以类似于“2 小时,30 分钟,10 秒”的格式打印日期之间的差异。
let items = [
{
endDate: "Fri Jun 30 2023 15:31:51 GMT+0200 (Central European Summer Time)",
startDate: "Fri Jun 30 2023 13:32:05 GMT+0200 (Central European Summer Time)"
}
];
items.forEach((item) => {
const { endDate, startDate } = item;
let startDate_ = new Date(startDate);
let endDate_ = new Date(endDate);
const differenceInMilliseconds = Math.abs(endDate_.getTime() - startDate_.getTime());
const differenceInSeconds = Math.floor(differenceInMilliseconds / 1000);
let seconds = differenceInSeconds % 60;
let minutes = Math.floor(differenceInSeconds / 60) % 60;
let hours = Math.floor(differenceInSeconds / 3600);
console.log(startDate, "startDate");
console.log(endDate, "endDate");
console.log(hours + " 小时, " + minutes + " 分钟, " + seconds + " 秒");
});
英文:
This will print the difference between the dates in a format like "2 hours, 30 minutes, 10 seconds"..
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
let items = [
{
endDate: "Fri Jun 30 2023 15:31:51 GMT+0200 (Central European Summer Time)",
startDate: "Fri Jun 30 2023 13:32:05 GMT+0200 (Central European Summer Time)"
}
];
items.forEach((item) => {
const { endDate, startDate } = item;
let startDate_ = new Date(startDate);
let endDate_ = new Date(endDate);
const differenceInMilliseconds = Math.abs(endDate_.getTime() - startDate_.getTime());
const differenceInSeconds = Math.floor(differenceInMilliseconds / 1000);
let seconds = differenceInSeconds % 60;
let minutes = Math.floor(differenceInSeconds / 60) % 60;
let hours = Math.floor(differenceInSeconds / 3600);
console.log(startDate, "startDate");
console.log(endDate, "endDate");
console.log(hours + " hours, " + minutes + " minutes, " + seconds + " seconds");
});
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论