subtractDays β GTM Variable Template for Date
subtractDays EXTENDED Date
Subtracts a given number of days from a date string and returns the new date in YYYY-MM-DD format.
When to Use This
Section titled βWhen to Use ThisβDate & Time
Calculate durations, differences, and time-based operations on date values.
Examples
Section titled βExamplesβSubtract One Week
INPUT
Target Date: 2024-04-20
Days to Subtract: 7
Days to Subtract: 7
OUTPUT
2024-04-13
Cross Month Boundary
INPUT
Target Date: 2024-05-01
Days to Subtract: 1
Days to Subtract: 1
OUTPUT
2024-04-30
Live Sandbox
Section titled βLive SandboxβThis is what you'll see when you open this variable in Google Tag Manager. Hover the icons for details.
subtractDays
Target Date
πΎ The starting date in YYYY-MM-DD format.
Supported formats:
β String
Supported formats:
β String
Days to Subtract
π’ The number of days to subtract from the target date.
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.
Target Date string
π‘ Type any text to see the result update live
π― Using special value β click input to type instead
Test with:
Falsy
Truthy
Days to Subtract number
π Result Handling β Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
subtractDays()
Related Variables
Section titled βRelated VariablesβSame category: Date
Under the Hood
Section titled βUnder the Hoodβπ View Implementation Code
/*** Subtracts a specific number of days from a target date string.** @param {string} data.src - The starting date in YYYY-MM-DD format.* @param {number|string} data.days - The number of days to subtract.* @param {Function|string} [data.out] - Optional output handler.** Direct-mode specific parameters:* @param {Function} [data.pre] - Optional pre-processor function to transform src.** @returns {string|undefined} The new date in YYYY-MM-DD format, or undefined if invalid.** @framework ggLowCodeGTMKit*/const makeNumber = require('makeNumber');
const subtractDays = function(dateString, daysToSubtract) { if (typeof dateString !== 'string') { return undefined; }
var days = makeNumber(daysToSubtract); if (days !== days) { days = 0; }
var matchParts = dateString.match("^(\\d{4})-(\\d{2})-(\\d{2})$"); if (matchParts === null) { return undefined; }
var year = makeNumber(matchParts[1]); var month = makeNumber(matchParts[2]); var day = makeNumber(matchParts[3]);
var isLeapYear = function(y) { return (y % 4 === 0 && y % 100 !== 0) || (y % 400 === 0); };
var dateToAbsoluteDays = function(y, m, d) { if (y < 1900 || y > 2100) { return undefined; } if (m < 1 || m > 12) { return undefined; }
var daysInMonth = [0, 31, isLeapYear(y) ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; if (d < 1 || d > daysInMonth[m]) { return undefined; }
var total = 0; var i = 1900;
while (i < y) { total = total + (isLeapYear(i) ? 366 : 365); i = i + 1; }
i = 1; while (i < m) { total = total + daysInMonth[i]; i = i + 1; }
return total + d; };
var absoluteDaysToDate = function(absDays) { var y = 1900; var yearDays = isLeapYear(y) ? 366 : 365;
while (absDays > yearDays) { absDays = absDays - yearDays; y = y + 1; yearDays = isLeapYear(y) ? 366 : 365; }
var m = 1; var daysInMonth = [0, 31, isLeapYear(y) ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
while (absDays > daysInMonth[m]) { absDays = absDays - daysInMonth[m]; m = m + 1; }
var d = absDays;
var mm = m < 10 ? '0' + m : '' + m; var dd = d < 10 ? '0' + d : '' + d;
return y + '-' + mm + '-' + dd; };
var absDays = dateToAbsoluteDays(year, month, day); if (absDays === undefined) { return undefined; }
var newAbsDays = absDays - days; if (newAbsDays < 1) { return undefined; }
return absoluteDaysToDate(newAbsDays);};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;const out = safeFunction(data.out);
// ===============================================================================// subtractDays - Direct mode// ===============================================================================constπ§ͺ View Test Scenarios (7 tests)
β
'[example] Subtract One Week'β
'[example] Cross Month Boundary'β
Cross Leap Year Boundaryβ
Cross Non-Leap Year Boundaryβ
Subtract 365 Daysβ
Subtract negative daysβ
Invalid date returns undefined