Skip to content

dateToUnixTimestamp β€” GTM Variable Template for Date

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

Converts a date string to Unix timestamp in milliseconds.


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.


Date to Unix timestamp
INPUT
Date String: 2024-01-15
OUTPUT
1705276800000
Epoch returns 0
INPUT
Date String: 1970-01-01
OUTPUT
0

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


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