Skip to content

isUnixTimestamp β€” GTM Variable Template for Logic

VARIABLES β€Ί LOGIC
isUnixTimestamp EXTENDED Logic
Direct (.tpl) Apply (.tpl)

Check if the input is a valid Unix timestamp.



Seconds timestamp
INPUT
Value To Check: 1609459200
OUTPUT
true
Milliseconds timestamp
INPUT
Value To Check: 1609459200000
OUTPUT
true

This is what you'll see when you open this variable in Google Tag Manager. Hover the icons for details.

isUnixTimestamp
Value To Check
πŸ’Ύ The value to check.

Supported formats:
  βœ“ All
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.
Value To Check number
πŸ’‘ Type any text to see the result update live
🎯 Using special value β€” click input to type instead
Test with:
Falsy
Truthy
isUnixTimestamp()


πŸ“œ View Implementation Code
/**
* Checks if the provided input is a valid Unix timestamp.
*
* @param {any} data.src - The input value to check (could be a string or number).
* @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 checking.
*
* @returns {boolean} True if the input is a valid Unix timestamp (seconds or milliseconds), false otherwise.
*
* @framework ggLowCodeGTMKit
*/
const makeInteger = require('makeInteger');
const makeNumber = require('makeNumber');
const isUnixTimestamp = function(value) {
if (value === null || value === "") { return false;}
const valueNum = makeNumber(value);
const valueInt = makeInteger(value);
if (typeof valueInt === 'number' && valueNum === valueInt) {
const isMilliseconds = valueInt >= 1000000000000 && valueInt <= 8640000000000; ; // Valid range for milliseconds
const isSeconds = valueInt >= -2147483648 && valueInt <= 2147483647; // Valid range for seconds
return isMilliseconds || isSeconds;
}
return false;
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// isUnixTimestamp - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
return out(isUnixTimestamp(value));
// ===============================================================================
// isUnixTimestamp() – Apply Mode
// ===============================================================================
/*
return function(value) {
return out(isUnixTimestamp(value));
};
*/
πŸ§ͺ View Test Scenarios (7 tests)
βœ… '[example] Seconds timestamp'
βœ… '[example] Milliseconds timestamp'
βœ… String timestamp - should return true
βœ… Decimal timestamp - should return false
βœ… Too large timestamp - should return false
βœ… Zero Timestamp - should return true
βœ… Empty String - should return false