Skip to content

Javasript check current datetime with loop 2 between datetimes

I’m working with time loop.

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
}

Now I have this FlipClock plugin,

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");
            }
        }
    }
});

My question, is it possible to check if there is a currentTime between startShow and endShow match with my above code?

Answer

Instead of two arrays for startShow and endShow, take one array as showTime and add object with startShow and endShow value. Also do not format time as it will convert value to string. Instead left it as moment object.

Similarly inside interval: function() get currentTime with var currentTime = moment(moment(new Date()).format('MM/DD/YYYY ') + time); only. Now your condition will be like showTime.some(s => s.startShow <= currentTime && s.endShow >= currentTime) which will return true if currentTime is between any startShow & endShow from any showTime array.

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);
        }
      });
    }
  }
});

Try it at below.

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>