Javascript 在日期时间之间使用循环 2 检查当前日期时间

Javasript check current datetime with loop 2 between datetimes

我正在使用时间循环。

var startShow = [];
var endShow = [];

for(var i = 0; i < jsonStr[4].length; i < i++)
{
    startShow[i] = moment(jsonStr[4][i].start_show).format('MM/DD/YYYY HH:mm:ss');
    //ex: 01/12/2021 18:32:40,01/12/2021 17:32:40,01/12/2021 16:52:40,01/12/2021 18:10:40
    endShow[i] = moment(jsonStr[4][i].end_show).format('MM/DD/YYYY HH:mm:ss');
    //ex: 01/12/2021 18:53:55,01/12/2021 18:23:40,01/12/2021 19:32:40,01/12/2021 19:50:40
}

现在我有了这个 FlipClock 插件,

var theCounters = $('.clock .value').FlipClock({
    clockFace: 'TwentyFourHourClock',
    callbacks:
    {
        interval:function()
        {
            var time = this.factory.getTime().time;
                            
            var currentTime = (moment(time).format('MM/DD/YYYY HH:mm:ss'));
                            
            //Here I need the currentTime check if there is a time between startShow and endShow matched.
            if(matched)
            {
                alert("matched");
            }
            else
            {
                alert("not matched");
            }
        }
    }
});

我的问题,是否可以检查 startShow 和 endShow 之间的 currentTime 是否与我上面的代码匹配?

代替 startShowendShow 的两个数组,取一个数组作为 showTime 并添加具有 startShowendShow 值的对象。也不要格式化时间,因为它会将值转换为字符串。而是将其保留为 moment 对象。

类似地在 interval: function() 中得到 currentTime 只有 var currentTime = moment(moment(new Date()).format('MM/DD/YYYY ') + time);。现在你的条件将像 showTime.some(s => s.startShow <= currentTime && s.endShow >= currentTime) 如果 currentTime 位于任何 showTime 数组的任何 startShowendShow 之间,则 return 为真。

var showTime = [];

for (var i = 0; i < jsonStr[4].length; i < i++) {
  showTime.push({
    startShow: moment(jsonStr[4][i].start_show),
    endShow: moment(jsonStr[4][i].end_show),
    title: moment(jsonStr[4][i].title)
  });
}

var theCounters = $('.clock .value').FlipClock({
  clockFace: 'TwentyFourHourClock',
  callbacks: {
    interval: function() {
      var time = this.factory.getTime().time;
      var currentTime = moment(moment(new Date()).format('MM/DD/YYYY ') + time);

      //Here I need the currentTime check if there is a time between startShow and endShow matched.
      // Loop over each show time
      showTime.forEach(s => {
        // Check condition for whether current time falls between any show time
        // and alert status with show title
        if (s.startShow <= currentTime && s.endShow >= currentTime) {
          alert("matched - " + s.title);
        } else {
          alert("not matched - " + s.title);
        }
      });
    }
  }
});

在下面试试。

var showTime = [];
// adding dateString for testing only. You should use date from your response
var dateString = moment(new Date()).format('MM/DD/YYYY ');

// add start, end time & title into object to be pushed into array
showTime.push({
  startShow: moment(dateString + '18:32:40'),
  endShow: moment(dateString + '18:53:55'),
  title: "abc"
});
showTime.push({
  startShow: moment(dateString + '17:32:40'),
  endShow: moment(dateString + '18:23:55'),
  title: "def"
});

var time = '18:20:00';
var currentTime = moment(moment(new Date()).format('MM/DD/YYYY ') + time);

// Loop over each show time
showTime.forEach(s => {
  // Check condition for whether current time falls between any show time
  // and alert status with show title
  if (s.startShow <= currentTime && s.endShow >= currentTime) {
    alert("matched - " + s.title);
  } else {
    alert("not matched - " + s.title);
  }
});

//if (showTime.some(s => s.startShow <= currentTime && s.endShow >= currentTime)) {
//  alert("matched");
//} else {
//  alert("not matched");
//}
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.0/moment.min.js" integrity="sha512-lMkd3Y03vWd6SXDKGLaPgbco+3VNI4xtwiuADZvr29hzHhxIdonDcZQF0k/eAf6/1vI4b2eNqkkIm42Hjxiz6A==" crossorigin="anonymous"></script>