addDays β GTM Variable Template for Date
addDays EXTENDED Date
Adds a given number of days to 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βAdd One Week
INPUT
Target Date: 2024-04-20
Days to Add: 7
Days to Add: 7
OUTPUT
2024-04-27
Cross Month Boundary
INPUT
Target Date: 2024-04-30
Days to Add: 1
Days to Add: 1
OUTPUT
2024-05-01
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.
addDays
Target Date
πΎ The starting date in YYYY-MM-DD format.
Supported formats:
β String
Supported formats:
β String
Days to Add
π’ The number of days to add to 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 Add number
π Result Handling β Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
addDays()
Related Variables
Section titled βRelated VariablesβSame category: Date
Under the Hood
Section titled βUnder the Hoodβπ View Implementation Code
/*** Adds a specific number of days to 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 add.* @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 addDays = function(dateString, daysToAdd) { if (typeof dateString !== 'string') { return undefined; }
var days = makeNumber(daysToAdd); 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);// ===============================================================================// addDays - Direct mode// ===============================================================================const applyCast = (castFn, value) => sπ§ͺ View Test Scenarios (7 tests)
β
'[example] Add One Week'β
'[example] Cross Month Boundary'β
Cross Leap Year Boundaryβ
Cross Non-Leap Year Boundaryβ
Add 365 Daysβ
Add negative daysβ
Invalid date returns undefined