【问题标题】:Compare timestamp and date in angular以角度比较时间戳和日期
【发布时间】:2023-03-22 05:35:01
【问题描述】:

我想比较日期和时间戳。这里 dates 是日期数组, dateTimestamp 是时间戳数组,我想要的是如果日期匹配则增加计数,否则不会。谁能帮帮我。 我尝试的是-

this.dates.forEach(e => {
     dateTimestamp.forEach(f => {
       if(e == f) {
         console.log('here', c++);
       } else {
         console.log('hyyy', c);
       }
     });
   });

【问题讨论】:

标签: angular date timestamp


【解决方案1】:

这样试试

this.dates.forEach(e => {
const dateA: any = new Date(e);
     dateTimestamp.forEach(f => {
       const dateB: any = new Date(f);
       if(dateA == dateB) {
         console.log('here', e);
       } else {
         console.log('hyyy', f);
       }
     });
   });

【讨论】:

  • 同时安慰 dateA 和 dateB '无效日期'即将到来。
  • 能否提供stackblitz.com 演示或添加日期和dateTimestamp 数组。
【解决方案2】:

您可以使用

将时间戳转换为日期对象
 new Date(13456543);

然后使用下面的函数进行比较

calcDateDifference(date1, date2) {
        let a = moment(date1);
        let b = moment(date2);
        const diff = a.diff(b, "days");
        return diff;
    }

其中 date1 和 date2 是 Date 对象;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-18
    相关资源
    最近更新 更多