timestampToDayIndex β GTM Variable Template for Date
timestampToDayIndex EXTENDED Date
Converts a Unix timestamp in milliseconds to a day index since epoch. Useful for measuring days between events.
When to Use This
Section titled βWhen to Use ThisβDate Formatting
Format and transform date values into human-readable or machine-readable strings.
Type Conversion
Safely convert between data types β strings, numbers, booleans, arrays, objects.
Date & Time
Calculate durations, differences, and time-based operations on date values.
Examples
Section titled βExamplesβConvert timestamp to day index
INPUT
Timestamp: 1718726400000
OUTPUT
19892
Invalid input returns undefined
INPUT
Timestamp: invalid
OUTPUT
undefined
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.
timestampToDayIndex
Timestamp
πΎ The Unix timestamp in milliseconds to convert to a day index.
Supported formats:
β Number: 1718726400000
β Stringified Number: "1718726400000"
β Variable: {{currentTimestamp}}
Supported formats:
β Number: 1718726400000
β Stringified Number: "1718726400000"
β Variable: {{currentTimestamp}}
Input Setup
Input Function (optional)
βοΈ Optional pre-processing function applied to the input before internal logic (e.g., parse string to number, apply mathematical operations). Internal transformations such as absolute value calculation will still apply afterward.
Result Handling
Output Function (optional)
βοΈ Optional function to apply to the day index before returning it (e.g.,
x => x * -1 to negate, x => x.toString() to convert to string).Timestamp number
π‘ Type any text to see the result update live
π― Using special value β click input to type instead
Test with:
Falsy
Truthy
π Result Handling β Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
timestampToDayIndex()
Related Variables
Section titled βRelated VariablesβSame category: Date
Under the Hood
Section titled βUnder the Hoodβπ View Implementation Code
/** * Converts a Unix timestamp in milliseconds to a day index since epoch (January 1, 1970). * Useful for calculating the number of days between two events by subtracting day indices. * * @param {number} data.src - The Unix timestamp in milliseconds. * @param {Function|string} [data.out] - Optional output handler. * * Direct-mode specific parameters: * @param {Function} [data.pre] - Optional pre-processor function. * * @returns {number} The day index since epoch, or undefined if input is invalid. * * @framework ggLowCodeGTMKit */const makeNumber = require('makeNumber');const Math = require('Math');const timestampToDayIndex = function(timestamp) { const ts = makeNumber(timestamp); if (typeof ts !== 'number' || ts !== ts) return undefined; return Math.floor(ts / 86400000);};const safeFunction = fn => typeof fn === 'function' ? fn : x => x;const out = safeFunction(data.out);// ===============================================================================// timestampToDayIndex - Direct mode// ===============================================================================const applyCast = (castFn, value) => safeFunction(castFn)(value);const value = applyCast(data.pre, data.src);return out(timestampToDayIndex(value));// ===============================================================================// timestampToDayIndex(...) β Apply Mode// ===============================================================================/*return function(timestamp) { return out(timestampToDayIndex(timestamp));};*/π§ͺ View Test Scenarios (5 tests)
β
'[example] Convert timestamp to day index'β
Difference between two day indices equals number of daysβ
Same day returns same indexβ
Stringified number is convertedβ
'[example] Invalid input returns undefined'