【问题标题】:Check interval between midnight of a certain date and current moment in JS / node /moment在JS / node /moment中检查某个日期的午夜和当前时刻之间的间隔
【发布时间】:2020-08-09 09:22:33
【问题描述】:

我有一个 JS Date 对象(比如09.08.2020 15.45),想写一个表达式 “当前时刻距下一个日期的午夜不到 3 分钟”(因此,从 09.09.2020 00:0009.09.2020 00:03 的时间为 true,否则为 false)。

什么是最佳、最优雅的表达方式?我可以使用任何 npm 包,例如 moment

【问题讨论】:

    标签: javascript node.js typescript momentjs


    【解决方案1】:

    我会选择isBetween。 它也可以在day.js

    function isLessThanThreeMinutesPastMidnight(date) {
      const nextMidnight = moment().add(1, 'days').startOf('day');
      const threeMinutesPastNextMidnight = moment(nextMidnight).add(3, 'minute');
    
      return moment(date).isBetween(nextMidnight, threeMinutesPastNextMidnight, null, '[)'); 
    }
    

    Playground

    如果你觉得null, '[)' 不是很优雅,你可以考虑使用今天的结束而不是明天的开始:

    function isLessThanThreeMinutesPastMidnight(date) {
      const now = moment.now(); // helps to avoid problems with changing the date between `now` calls
      const endOfDay = moment(now).endOf('day');
      const threeMinutesPastNextMidnight = moment(now).add(1, 'days').startOf('day').add(3, 'minute');
    
      return moment(date).isBetween(endOfDay, threeMinutesPastNextMidnight);
    }
    

    【讨论】:

    • 谢谢!效果很好。我只补充一点,时刻是可变的。因此,如果有人使用预定义的时刻而不是时刻(现在),那么 const threeMinutesPastNextMidnight = someMoment.add(1, 'days') 将改变 someMoment 也向前移动 1 天,即使它是违反直觉的。感谢@Edeph 对此注释的回答。
    • 你是对的,使用 moment.js 是一个很好的做法,在使用前将任何日期/时刻包装到一个时刻调用中。
    【解决方案2】:

    您可以将 momentJS (docs here) 中的 isBefore()isAfter() 结合起来,将您的日期与午夜日期和午夜日期加 3 分钟进行比较。

    小心照docs:It should be noted that moments are mutable. Calling any of the manipulation methods will change the original moment.

    示例代码:

    const t1 = moment().add(2, 'minutes'); // one moment in time
    
    setTimeout(() => {
        const now = moment();
        const threeMinutesOver = moment().add(3, 'minutes');
    
        console.log(t1.isAfter(now) && t1.isBefore(threeMinutesOver)); // t1 is between 
    }, 2000);
    

    【讨论】:

      【解决方案3】:

      使用纯 JavaScript 的解决方案:通过从今天和 24 小时创建一个包含年、月和日的新日期,从午夜到第二天。通过这种方式,您甚至可以获得正确的日期/时间,如果它是从夏季/冬季独立于实际白天的时间变化的一天。
      取 testing-date 和午夜之间的差异,看看结果是否在 0 到 180.000 毫秒之间。

      function chechMidnight3Minutes(date) {
        let today = new Date();
        let midnight = new Date(today.getFullYear(),today.getMonth(),today.getDate(),24);
        let diff = date.getMilliseconds() - midnight.getMilliseconds(); 
        diff = (date - midnight); 
        return diff>=0 && diff<=180000;
      }
      
      let date = new Date('2020-08-10T00:02:59');
      console.log(chechMidnight3Minutes(date));
      
      date = new Date('2020-08-10T00:03:00');
      console.log(chechMidnight3Minutes(date));
      
      date = new Date('2020-08-10T01:02:00');
      console.log(chechMidnight3Minutes(date));
      
      date = new Date('2020-08-0910T00:00:00');
      console.log(chechMidnight3Minutes(date));
      
      date = new Date('2020-08-0910T23:59:59');
      console.log(chechMidnight3Minutes(date));

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-01-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-16
        • 1970-01-01
        • 2021-10-04
        相关资源
        最近更新 更多