Skip to content

addDays β€” GTM Variable Template for Date

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

Adds a given number of days to a date string and returns the new date in YYYY-MM-DD format.


Date & Time

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


Add One Week
INPUT
Target Date: 2024-04-20
Days to Add: 7
OUTPUT
2024-04-27
Cross Month Boundary
INPUT
Target Date: 2024-04-30
Days to Add: 1
OUTPUT
2024-05-01

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
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
addDays()


πŸ“œ 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