Skip to content

unixTimestampToDate — GTM Variable Template for Time

VARIABLES › TIME
unixTimestampToDate EXTENDED Time

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


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

GTM Configuration

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


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