dateToUnixTimestamp β GTM Variable Template for Date
dateToUnixTimestamp EXTENDED Date
Converts a date string to Unix timestamp in milliseconds.
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βDate to Unix timestamp
INPUT
Date String: 2024-01-15
OUTPUT
1705276800000
Epoch returns 0
INPUT
Date String: 1970-01-01
OUTPUT
0
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.
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
Section titled βRelated VariablesβSame category: Date
Under the Hood
Section titled β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