【问题标题】:How to check current time lies between two given time Moment如何检查当前时间位于两个给定时间之间
【发布时间】:2021-06-18 08:08:39
【问题描述】:

我正在尝试检查时间是否介于两次之间或不使用片刻。我试过这样

import * as moment from 'moment';

current_day_time.current_time = 2021-06-18T08:03Z
batch_info.batch_start_time = 2021-06-17T03:30:29.351Z,
batch_info.batch_end_time = 2021-06-17T12:30:32.191Z


 const current_time = moment(current_day_time.current_time);
 const start = moment(batch_info.batch_start_time);
 const end = moment(batch_info.batch_end_time);


 
current = Fri Jun 18 2021 13:32:00 GMT+0530
start = Thu Jun 17 2021 09:00:29 GMT+0530
end = Thu Jun 17 2021 18:00:32 GMT+0530

 console.log(moment(current_time).isBetween(start, end)); // false 

我们可以清楚地看到 9-18 之间有 13 个谎言,但我还是错了。我怎样才能只比较时间?

【问题讨论】:

  • Jun 18 不属于Jun 17 的两个日期之间。
  • 我只是想比较一下时间@Mr_Green。

标签: javascript angular typescript momentjs


【解决方案1】:

您也可以使用 javascript 方法进行检查。它将以秒为单位返回您的日期时间,然后您可以比较它们,因为它们将以数字的形式出现。例如,看图片:

新日期(your_date).getTime()

import * as moment from 'moment';

current_day_time.current_time = 2021 - 06 - 18 T08: 03 Z
batch_info.batch_start_time = 2021 - 06 - 17 T03: 30: 29.351 Z,
  batch_info.batch_end_time = 2021 - 06 - 17 T12: 30: 32.191 Z


const current_time = moment(current_day_time.current_time);
const start = moment(batch_info.batch_start_time);
const end = moment(batch_info.batch_end_time);

current = Fri Jun 18 2021 13: 32: 00 GMT + 0530
start = Thu Jun 17 2021 09: 00: 29 GMT + 0530
end = Thu Jun 17 2021 18: 00: 32 GMT + 0530


let current_d = new Date("Fri Jun 18 2021 13:32:00 GMT+0530").getTime()
let start_d = new Date("Thu Jun 17 2021 09:00:29 GMT+0530").getTime()
let end_d = new Date("Thu Jun 17 2021 18:00:32 GMT+0530").getTime()

if (current_d > start_d && current_d < end_d) {
  console.log('In')
}

//console.log(moment(current_time).isBetween(start, end)); // false

【讨论】:

    【解决方案2】:
    const format = 'hh:mm:ss'
    
    // const time = moment() gives you current time. no format required.
    const time = moment('09:34:00',format);
    const beforeTime = moment('08:34:00', format);
    const afterTime = moment('10:34:00', format);
    
    if (time.isBetween(beforeTime, afterTime)) {
      console.log('is between')
    } else {
      console.log('is not between')
    }
    

    【讨论】:

      【解决方案3】:

      我想您比较完整的日期,在这种情况下,从 2021 年 6 月 18 日开始的时间永远不会介于 2021 年 6 月 17 日的两个日期之间。如果不使用moment.js,也许下一个sn-p 是一个想法?

      let startTime = new Date('2021-06-17T03:30:29.351Z');
      let endTime = new Date('2021-06-17T12:30:32.191Z');
      let currentTime = new Date('2021-06-18T08:03Z');
      
      console.log(timeWithin(currentTime, startTime, endTime));
      
      const compareTimes = TimeComparerFactory()
      console.log(compareTimes(currentTime, startTime, endTime));
      
      // time comparison for complete dates
      function timeWithin(time, start, end) {
        return time > start && time < end;
      }
      
      // time comparison for time only
      // where the time of a date is converted to a number
      // (disregarding milliseconds)
      function TimeComparerFactory() {
        const pad = v => `${v}`.padStart(2, `0`);
        const t2Nr = date => {
          const dStr = date.toISOString();
          return +( dStr.split("T")[1].split(/[:+-.]/, 3).map(pad).join("") );
        };
        
        return (time, start, end) => {
          time = t2Nr(time);
          return time > t2Nr(start) && time < t2Nr(end);
        }
      }

      【讨论】:

        猜你喜欢
        • 2021-06-29
        • 1970-01-01
        • 1970-01-01
        • 2013-01-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多