Skip to content

unixTimestampToDate β€” GTM Variable Template for Date

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

Converts a Unix timestamp to a date string in YYYY-MM-DD format.


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.


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

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


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