unixTimestampToDate β GTM Variable Template for Date
unixTimestampToDate EXTENDED Date
Converts a Unix timestamp to a date string in YYYY-MM-DD format.
When to Use This
Section titled βWhen to Use ThisβTime Operations
Measure durations, calculate elapsed time, and format timestamps.
Type Conversion
Safely convert between data types β strings, numbers, booleans, arrays, objects.
Formatting
Normalize casing, spacing, encoding, and presentation of data values.
Date & Time
Calculate durations, differences, and time-based operations on date values.
Examples
Section titled βExamplesβTimestamp to date string
INPUT
Unix Timestamp: 1735084800000
OUTPUT
2024-12-25
Epoch returns 1970-01-01
INPUT
Unix Timestamp: 0
OUTPUT
1970-01-01
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.
unixTimestampToDate
Unix Timestamp
πΎ Unix timestamp in seconds since epoch.
Supported formats:
β Number
β String
Supported formats:
β Number
β 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.
Unix 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.
unixTimestampToDate()
Related Variables
Section titled βRelated VariablesβSame category: Date
Under the Hood
Section titled βUnder the Hoodβπ View Implementation Code
/*** Convert a Unix timestamp (seconds since epoch) to YYYY-MM-DD format without using the Date object.** @param {number} data.src - Unix timestamp in seconds.* @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 {string|undefined} Date in YYYY-MM-DD format, or undefined if input is invalid.** @framework ggLowCodeGTMKit*/const makeNumber = require('makeNumber');const Math = require('Math');
const unixTimestampToDate = function(timestamp) { const timestampNum = makeNumber(timestamp); if (timestampNum !== timestampNum) { return undefined; }
const daysSinceEpoch = Math.floor(timestampNum / (1000 * 60 * 60 * 24));
const daysSinceEpochToDate = function (days) { let year = 1970; let remainingDays = days;
function isLeapYear(yr) { return (yr % 4 === 0 && yr % 100 !== 0) || (yr % 400 === 0); }
const daysInMonth = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
while (true) { const daysInYear = isLeapYear(year) ? 366 : 365; if (remainingDays < daysInYear) { break; } remainingDays = remainingDays - daysInYear; year++; }
if (isLeapYear(year)) { daysInMonth[2] = 29; }
let month = 1;
while (month <= 12 && remainingDays >= daysInMonth[month]) { remainingDays = remainingDays - daysInMonth[month]; month++; }
const day = remainingDays + 1;
return { year: year, month: month, day: day }; };
const dateParts = daysSinceEpochToDate(daysSinceEpoch); const year = dateParts.year.toString(); // Handle padding const month = dateParts.month < 10 ? '0' + dateParts.month : dateParts.month.toString(); const day = dateParts.day < 10 ? '0' + dateParts.day : dateParts.day.toString(); return year + "-" + month + "-" + day;};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;const out = safeFunction(data.out);
// ===============================================================================// unixTimestampToDate - Direct mode// ===============================================================================const applyCast = (castFn, value) => safeFunction(castFn)(value);const value = applyCast(data.pre, data.src);return out(unixTimestampToDate(value));
// ===============================================================================// unixTimestampToDate() β Apply Mode// ===============================================================================/*return function(value) { return out(unixTimestampToDate(value));};*/π§ͺ View Test Scenarios (5 tests)
β
'[example] Timestamp to date string'β
'[example] Epoch returns 1970-01-01'β
Leap year date - converts correctlyβ
End of year timestamp - converts correctlyβ
Invalid input (NaN) - returns undefined