在JavaScript中找到两个日期之间的差异?

huangapple go评论76阅读模式
英文:

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: &quot;Fri Jun 30 2023 15:31:51 GMT+0200 (Central European Summer Time)&quot;, 
        startDate: &quot;Fri Jun 30 2023 13:32:05 GMT+0200 (Central European Summer Time)&quot;
    }
];

items.forEach((item) =&gt; {
    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, &quot;startDate&quot;);
    console.log(endDate, &quot;endDate&quot;);
    console.log(hours + &quot; hours, &quot; + minutes + &quot; minutes, &quot; + seconds + &quot; seconds&quot;);
});

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年7月3日 19:56:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76604508.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定