Skip to content

timestampToDayIndex — GTM Variable Template for Date

VARIABLES › 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

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

Convert timestamp to day index
INPUT
Timestamp: 1718726400000
OUTPUT
19892
Invalid input returns undefined
INPUT
Timestamp: invalid
OUTPUT
undefined

GTM Configuration

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}}
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
timestampToDayIndex()


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'