getHourOfDay β GTM Variable Template for Date
getHourOfDay EXTENDED Date
Extracts the hour (0-23) from a given timestamp, adjusted for timezone.
Examples
Section titled βExamplesβGet UTC Hour
INPUT
Timestamp: 1713960000000
Timezone Offset: 0
Timezone Offset: 0
OUTPUT
12
Get Local Hour (UTC+2)
INPUT
Timestamp: 1713960000000
Timezone Offset: 2
Timezone Offset: 2
OUTPUT
14
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.
getHourOfDay
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.
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
π Result Handling β Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
getHourOfDay()
Related Variables
Section titled βRelated VariablesβSame category: Date
Under the Hood
Section titled βUnder the Hoodβπ View Implementation Code
/*** Returns the hour of the day (0-23) from a Unix timestamp.** @param {number|string} data.src - A Unix timestamp in milliseconds.* @param {number|string} data.offset - The timezone offset in hours.* @param {Function|string} [data.out] - Optional output handler.** Direct-mode specific parameters:* @param {Function} [data.pre] - Optional pre-processor function to transform src before processing.** @returns {number|undefined} The hour of the day (0-23), or undefined if invalid.** @framework ggLowCodeGTMKit*/const makeNumber = require('makeNumber');const Math = require('Math');
const getHourOfDay = function(timestamp, offset) { const ts = makeNumber(timestamp); if (ts !== ts) return undefined;
let tzOffset = makeNumber(offset); if (tzOffset !== tzOffset) tzOffset = 0;
let hoursSinceEpoch = Math.floor(ts / 3600000);
hoursSinceEpoch += tzOffset;
let hourOfDay = hoursSinceEpoch % 24;
if (hourOfDay < 0) { hourOfDay += 24; }
return hourOfDay;};const safeFunction = fn => typeof fn === 'function' ? fn : x => x;const out = safeFunction(data.out);// ===============================================================================// getHourOfDay - Direct mode// ===============================================================================const applyCast = (castFn, value) => safeFunction(castFn)(value);const value = applyCast(data.pre, data.src);return out(getHourOfDay(value, data.offset));// ===============================================================================// getHourOfDay(...) β Apply Mode// ===============================================================================/*return function(value) { return out(getHourOfDay(value, data.offset));};*/π§ͺ View Test Scenarios (5 tests)
β
'[example] Get UTC Hour'β
'[example] Get Local Hour (UTC+2)'β
Negative offset wrapping past midnightβ
String inputsβ
Invalid timestamp returns undefined