Check if time in minutes since start of week is between two values (value in minutes since start of week) at different intervals

Answered


Posted Dec 21, 2021

So i'm using the Zendesk API to fetch the schedule of an instance, the API returns an object that contains an array called intervals which looks like this:

[
  { "start_time": 1980, "end_time": 2460 },
  { "start_time": 3420, "end_time": 3900 },
  { "start_time": 4860, "end_time": 5340 },
  { "start_time": 6300, "end_time": 6780 },
  { "start_time": 7740, "end_time": 8220 }
]

This example just translates to 9AM to 5PM in minutes for each business day of the week with the value being in minutes since the start of the week which is actually Midnight on Sunday (meaning a value of 720 would be noon on Sunday).

I'm trying to implement some server-side logic that loops through the intervals and checks if the current time is between the intervals for that given day making it possible to detect if it's currently within business hours or not.

I was thinking the moment.js library might be useful for this and I have been playing around with the .startOf('week') and .diff() functions but I keep getting blocked and I fear it might not be as reliable as I'd like given that i'm unsure if the moment starts the week on a Monday or Sunday.

So far I've got

let isBusinessHours = false;

function checkBusinessHours() {
  intervals.map((interval, key) => {
    //offset weekdays to match interval keys
    let currentDay = moment().weekday() -1; 

    //create comparison start & end times based on interval start & end times
    let startTime = moment().startOf('week').add(interval.start_time, 'minutes'); 
    let endTime = moment().startOf('week').add(interval.end_time, 'minutes');

    //check to see if it's business hours
    if (moment().isBetween(startTime, endTime) && currentDay === key) {
      isBusinessHours = true;
    } 
  })
  return isBusinessHours;
}

But i'm unsure if this is the best way to go about checking this?

 
---------------------------------------

1

1

1 comment

Hey there,
 
Thanks for reaching out!
 
Here's an example of how this could work without the use of any additional libraries:
 
let intervals = [
{ start_time: 1980, end_time: 2460 },
{ start_time: 3420, end_time: 3900 },
{ start_time: 4860, end_time: 5340 },
{ start_time: 6300, end_time: 6780 },
{ start_time: 7740, end_time: 8220 },
];

let isBusinessHours = false;

function checkBusinessHours() {
  intervals.map((interval) => {

  let day = new Date().getDay();
  let hour = new Date().getHours();
  let minutes = new Date().getMinutes();

  // Total minutes for current day
  let combinedDayMinutes = hour * 60 + minutes;

  // Total minutes since Sunday
  let minutesSinceSunday = day * 1440 + combinedDayMinutes;

  //check to see if it's business hours
  if (minutesSinceSunday >= interval.start_time && minutesSinceSunday <= interval.end_time) {
    isBusinessHours = true;
  }
});
  console.log(isBusinessHours);
}

checkBusinessHours()
 
The getDay() method returns the day of the week for the specified date according to local time, where 0 represents Sunday.
 
I hope this helps! Feel free to reach out with any questions. 
 
Tipene

0


Sign in to leave a comment.

Didn't find what you're looking for?

New post