Skip to content

calculateLeadTime β€” GTM Variable Template for Date

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

Calculates the lead time (in days) between the current date and the provided target date.


Formatting

Normalize casing, spacing, encoding, and presentation of data values.

Date & Time

Calculate durations, differences, and time-based operations on date values.


Future Date
INPUT
Target Date: 2024-04-25
OUTPUT
1
Today
INPUT
Target Date: 2024-04-24
OUTPUT
0
Past Date
INPUT
Target Date: 2024-04-23
OUTPUT
-1

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

Read-only Preview
calculateLeadTime
Target Date
▢️ Target date 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.


πŸ“œ View Implementation Code
/**
* Calculates the number of days between today and a future target date.
*
* @param {string} data.src - Target date in YYYY-MM-DD format.
* @param {Function|string} [data.out] - Optional output handler.
*
* Direct-mode specific parameters:
* @param {Function} [data.pre] - Optional pre-processor function.
*
* @returns {number|undefined} The lead time in days (positive if target date is in the future), or undefined if input is invalid.
*
* @framework ggLowCodeGTMKit
*/
const makeNumber = require('makeNumber');
const getTimestampMillis = require('getTimestampMillis');
const Math = require('Math');
const calculateLeadTime = function(targetDate) {
if (typeof targetDate !== 'string') { return undefined; }
const reDateFormatISO8601 = "^(\\d{4})-(\\d{2})-(\\d{2})$";
const matchParts = targetDate.match(reDateFormatISO8601);
if (matchParts === null) { return undefined; }
function calculateDaysSinceEpoch(year, month, day) {
function isLeapYear(year) {
return (year % 4 === 0 && year % 100 !== 0) || (year % 400 === 0);
}
const daysInFebruary = isLeapYear(year) ? 29 : 28;
const daysInMonth = [0, 31, daysInFebruary, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
if (day > daysInMonth[month]) { return undefined; }
let totalDays = 0;
for (let y = 1970; y < year; y++) {
totalDays += isLeapYear(y) ? 366 : 365;
}
for (let m = 1; m < month; m++) {
totalDays += daysInMonth[m];
}
totalDays += day - 1;
return totalDays;
}
const targetDays = calculateDaysSinceEpoch(makeNumber(matchParts[1]), makeNumber(matchParts[2]), makeNumber(matchParts[3]));
if (targetDays === undefined) { return undefined; }
const currentDays = Math.floor(getTimestampMillis() / (1000 * 60 * 60 * 24));
return targetDays - currentDays;
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// calculateLeadTime - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
return out(calculateLeadTime(value));
// ===============================================================================
// calculateLeadTime() – Apply Mode
// ===============================================================================
/*
return function(value) {
return out(calculateLeadTime(value));
};
*/
πŸ§ͺ View Test Scenarios (5 tests)
βœ… '[example] Future Date'
βœ… '[example] Today'
βœ… '[example] Past Date'
βœ… Invalid format returns undefined
βœ… Invalid input - returns undefined