dateToUnixTimestamp — GTM Variable Template for Time
dateToUnixTimestamp EXTENDED Time
Converts a date string to Unix timestamp in milliseconds.
Examples
Date to Unix timestamp
INPUT
Date String: 2024-01-15
OUTPUT
1705276800000
Epoch returns 0
INPUT
Date String: 1970-01-01
OUTPUT
0
GTM Configuration
This is what you'll see when you open this variable in Google Tag Manager. Hover the icons for details.
dateToUnixTimestamp
Date String
💾 Date string in YYYY-MM-DD format.
Supported formats:
✓ String
Supported formats:
✓ String
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.
Date String string
💡 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.
dateToUnixTimestamp()
Related Variables
Same category: Time
Under the Hood
📜 View Implementation Code
/**
* Convert a date in YYYY-MM-DD format to Unix timestamp (milliseconds since epoch) without using the Date object.
*
* @param {string} data.src - Date string in YYYY-MM-DD format.
* @param {Function|string} [data.out] - Optional output handler: function to transform result or string with format.
*
* Direct-mode specific parameters:
* @param {Function} [data.pre] - Optional pre-processor function to transform src before conversion.
*
* @returns {number|undefined} Unix timestamp in milliseconds, or undefined if input is invalid.
*
* @framework ggLowCodeGTMKit
*/
const makeNumber = require('makeNumber');
const Math = require('Math');
const dateToUnixTimestamp = function(dateStr) {
if (typeof dateStr !== 'string') { return undefined; }
const reDateFormatISO8601 = "^(\\d{4})-(\\d{2})-(\\d{2})$";
const dateMatchParts = dateStr.match(reDateFormatISO8601);
if (dateMatchParts === null) { return undefined; }
const year = makeNumber(dateMatchParts[1]);
const month = makeNumber(dateMatchParts[2]);
const day = makeNumber(dateMatchParts[3]);
function calculateDaysSinceEpoch(year, month, day) {
function isLeapYear(year) {
return (year % 4 === 0 && year % 100 !== 0) || (year % 400 === 0);
}
const daysInFebruary = isLeapYear(year) ? 29 : 28;
// Days in each month including a dummy value for index 0
const daysInMonth = [0, 31, daysInFebruary, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
if (day > daysInMonth[month]) {
return undefined;
}
// Days from 1970 to the year before
let totalDays = 0;
for (let y = 1970; y < year; y++) {
totalDays += isLeapYear(y) ? 366 : 365;
}
// Add days from months in the current year
for (let m = 1; m < month; m++) {
totalDays += daysInMonth[m];
}
// Add days from the current month
totalDays += day - 1;
return totalDays;
}
const daysSinceEpoch = calculateDaysSinceEpoch(year, month, day);
return daysSinceEpoch !== undefined ? daysSinceEpoch * 24 * 60 * 60 * 1000 : undefined;
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// dateToUnixTimestamp - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
return out(dateToUnixTimestamp(value));
// ===============================================================================
// dateToUnixTimestamp() – Apply Mode
// ===============================================================================
/*
return function(value) {
return out(dateToUnixTimestamp(value));
};
*/🧪 View Test Scenarios (5 tests)
✅ '[example] Date to Unix timestamp'
✅ '[example] Epoch returns 0'
✅ Leap year date - should calculate correctly
✅ Invalid date format - should return undefined
✅ Invalid day in month - should return undefined