Skip to content

getHourOfDay β€” GTM Variable Template for Date

VARIABLES β€Ί DATE
getHourOfDay EXTENDED Date
Direct (.tpl) Apply (.tpl)

Extracts the hour (0-23) from a given timestamp, adjusted for timezone.


Get UTC Hour
INPUT
Timestamp: 1713960000000
Timezone Offset: 0
OUTPUT
12
Get Local Hour (UTC+2)
INPUT
Timestamp: 1713960000000
Timezone Offset: 2
OUTPUT
14

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
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
getHourOfDay()


πŸ“œ 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