calculateLeadTime β GTM Variable Template for Date
calculateLeadTime EXTENDED Date
Calculates the lead time (in days) between the current date and the provided target date.
When to Use This
Section titled βWhen to Use Thisβ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β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
GTM Configuration
Section titled βGTM Configurationβ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
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.
Related Variables
Section titled βRelated VariablesβSame category: Date
Under the Hood
Section titled βUnder the Hoodβπ 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