Conditional Logic,Date/Time
isBusinessHours EXTENDED Logic
Checks if a given timestamp falls within business hours (Monday-Friday, between a specific start and end hour), adjusted for timezone.
When to Use This
Section titled βWhen to Use ThisβChecks if a given timestamp falls within business hours (Monday-Friday
Examples
Section titled βExamplesβValid Business Time
INPUT
Timestamp: 1713960000000 (Wed Apr 24 2024 12:00:00 GMT+0000)
Hours: 9 to 17
Hours: 9 to 17
OUTPUT
true
Weekend Time
INPUT
Timestamp: 1714219200000 (Sat Apr 27 2024 12:00:00 GMT+0000)
Hours: 9 to 17
Hours: 9 to 17
OUTPUT
false
Live Sandbox
Section titled βLive Sandboxβ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
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
π Result Handling β Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
isBusinessHours(...)
Related Variables
Section titled βRelated VariablesβSame category: Logic
Under the Hood
Section titled βUnder the Hoodβπ 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