How to determine if NOW is within Business Days and Hours

  • 5 March 2023
  • 2 replies
  • 244 views

Userlevel 7
Badge +8

Hey everyone! Been a while since I posted I know 😆

 

I have seen many questions on the community asking how to use Paths by Zapier to have 2 different outcomes based on the time (NOW()) (or a timestamp coming from the trigger) and determining whether it is within business hours or outside of business hours. the script below does that and it bases it on the timezone of your choosing ! 

 

let dateToCheck = new Date().toLocaleString("en-US", {timeZone: "America/Los_Angeles"}); 

// This sets the code to PST timezone. You can change the timezone as needed

// In case you want to use a timestamp from the Trigger / Previous steps, check the screenshot at the end of the code on how to map it, and replace the first line with "let dateToCheck = inputData.date;" . Keep in mind that this time is PROBABLY in UTC.


let dateUnformatted = new Date(dateToCheck);

let dayDateFormatted = dateUnformatted.getDay();
var hours = dateUnformatted.getHours();
var minutes = dateUnformatted.getMinutes();

function formatTime(dateUnformatted) {
var hours = dateUnformatted.getHours();
var minutes = dateUnformatted.getMinutes();
hours = hours < 10 ? '0'+hours : hours;
minutes = minutes < 10 ? '0'+minutes : minutes;
var strTime = hours + ':' + minutes;
return strTime;
}

let workDay = "";
if (dayDateFormatted == 0) {
workDay = "Weekend"} // This assumes Sunday is a weekend day
else if (dayDateFormatted == 6)
{workDay = "Weekend"} // This assumes Saturday is a weekend day
else {workDay = "Workday"}



let dayHourFormatted = formatTime(dateUnformatted);

let workHour = "";
if (dayHourFormatted >= "08:00" && dayHourFormatted <= "17:00") {
workHour = "Working Hour"} 
else {workHour = "Not Working Hour"}

// This assumes working hours are from 8:00 AM to 5:00 PM, without taking into consideration the timezone.


output = [{dayDateFormatted, dayHourFormatted, workDay, workHour}]


// The outcome will be the day, hour, "Weekend"/"Workday" based on the day of the week, "Working Hour"/"Not Working Hour" based on the time.

 

Based on the outcome above, you can then use a Paths by Zapier step with the conditions "Weekend"/"Workday" and "Working Hour"/"Not Working Hour"

 

** In case you need to use a timestamp coming from the trigger instead of NOW(), you need to map the time from the trigger as shown in the below screenshot and follow the comment in the code. 

 


This post has been closed for comments. Please create a new post if you need help or have a question about this topic.

2 replies

Userlevel 7
Badge +8

Good to see you, @MohSwellam !! Thanks for sharing this! 🙌🏽

Userlevel 7
Badge +8

Thanks @Liz_Roberts ! Its always my pleasure to share something with the community!