Skip to content

Conditional Logic,Date/Time

VARIABLES β€Ί LOGIC
isBusinessHours EXTENDED Logic

Checks if a given timestamp falls within business hours (Monday-Friday, between a specific start and end hour), adjusted for timezone.


Checks if a given timestamp falls within business hours (Monday-Friday


Valid Business Time
INPUT
Timestamp: 1713960000000 (Wed Apr 24 2024 12:00:00 GMT+0000)
Hours: 9 to 17
OUTPUT
true
Weekend Time
INPUT
Timestamp: 1714219200000 (Sat Apr 27 2024 12:00:00 GMT+0000)
Hours: 9 to 17
OUTPUT
false

This is what you'll see when you open this variable in Google Tag Manager. Hover the icons for details.

isBusinessHours
Timestamp
πŸ’Ύ A Unix timestamp in milliseconds.

Supported formats:
  βœ“ Number
  βœ“ String
Timezone Offset (Hours)
βš™οΈ The number of hours to add or subtract from UTC to get the local time. Example: use 2 for CET (UTC+2), or -5 for EST (UTC-5). Leave blank or 0 for UTC.
Start Hour (0-23)
⏰ The hour when business starts (e.g., 9 for 9:00 AM). Defaults to 9.
End Hour (0-23)
⏰ The hour when business ends (e.g., 17 for 5:00 PM). Business hours are strictly before this hour. Defaults to 17.
Input Setup
Input Function (optional)
βš™οΈ Optional pre-processing function applied to the input before internal logic (e.g., convert object to string, normalize case). Internal transformations such as case handling will still apply afterward.
Result Handling
Output Function (optional)
βš™οΈ Optional function to apply to the result before returning it (e.g., str => str + ' €', val => val !== undefined for boolean conversion). Useful for chaining transformations on the output.
Timestamp number
πŸ’‘ Type any text to see the result update live
🎯 Using special value β€” click input to type instead
Test with:
Falsy
Truthy
Timezone Offset (Hours) string
Start Hour (0-23) string
End Hour (0-23) string
isBusinessHours(...)


πŸ“œ View Implementation Code
/**
* Checks if a given timestamp falls within business hours.
*
* @param {number|string} data.src - A Unix timestamp in milliseconds.
* @param {number|string} data.offset - The timezone offset in hours.
* @param {number|string} data.startHour - Start hour (default 9).
* @param {number|string} data.endHour - End hour (default 17).
* @param {Function|string} [data.out] - Optional output handler.
*
* Direct-mode specific parameters:
* @param {Function} [data.pre] - Optional pre-processor function.
*
* @returns {boolean|undefined} True if within business hours, false otherwise. Undefined if invalid timestamp.
*
* @framework ggLowCodeGTMKit
*/
const makeNumber = require('makeNumber');
const Math = require('Math');
const isBusinessHours = function(timestamp, offset, startHour, endHour) {
const ts = makeNumber(timestamp);
if (ts !== ts) return undefined;
let tzOffset = makeNumber(offset);
if (tzOffset !== tzOffset) tzOffset = 0;
let start = makeNumber(startHour);
if (start !== start) start = 9;
let end = makeNumber(endHour);
if (end !== end) end = 17;
const localTs = ts + (tzOffset * 3600000);
const localDaysSinceEpoch = Math.floor(localTs / 86400000);
// 0 = Thursday, 1 = Friday, 2 = Saturday, 3 = Sunday, 4 = Monday...
const dayOfWeek = (localDaysSinceEpoch + 4) % 7;
const positiveDayOfWeek = (dayOfWeek + 7) % 7; // Ensure positive: 0 = Sunday, 6 = Saturday
if (positiveDayOfWeek === 0 || positiveDayOfWeek === 6) {
return false;
}
let hoursSinceEpoch = Math.floor(localTs / 3600000);
let hourOfDay = hoursSinceEpoch % 24;
if (hourOfDay < 0) hourOfDay += 24;
return (hourOfDay >= start && hourOfDay < end);
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// isBusinessHours - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
return out(isBusinessHours(value, data.offset, data.startHour, data.endHour));
// ===============================================================================
// isBusinessHours(...) – Apply Mode
// ===============================================================================
/*
return function(value) {
return out(isBusinessHours(value, data.offset, data.startHour, data.endHour));
};
*/
πŸ§ͺ View Test Scenarios (6 tests)
βœ… '[example] Valid Business Time'
βœ… '[example] Weekend Time'
βœ… Outside Business Hours (Before Start)
βœ… Outside Business Hours (After End)
βœ… Within Business Hours with Timezone Offset
βœ… Invalid timestamp returns undefined