isWeekend β GTM Variable Template for Date
isWeekend EXTENDED Date
Determines if a given date string is a weekend (Saturday or Sunday) and returns true or false.
When to Use This
Section titled βWhen to Use ThisβConditional Logic
Branch logic based on conditions β if/else, ternary, boolean gates.
Date & Time
Calculate durations, differences, and time-based operations on date values.
Examples
Section titled βExamplesβSaturday
INPUT
Target Date: 2024-04-27
OUTPUT
true
Wednesday
INPUT
Target Date: 2024-04-24
OUTPUT
false
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.
isWeekend
Target Date
πΎ The 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.
Target Date string
π‘ Type any text to see the result update live
π― Using special value β click input to type instead
Test with:
Falsy
Truthy
π Result Handling β Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
isWeekend()
Related Variables
Section titled βRelated VariablesβSame category: Date
Under the Hood
Section titled βUnder the Hoodβπ View Implementation Code
/*** Checks if a given date string is a weekend.** @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 to transform src.** @returns {boolean|undefined} True if Saturday or Sunday, false if weekday. Undefined if invalid.** @framework ggLowCodeGTMKit*/const makeNumber = require('makeNumber');
const isWeekend = function(dateString) { if (typeof dateString !== 'string') return undefined;
const reDateFormatISO8601 = "^(\\d{4})-(\\d{2})-(\\d{2})$"; const matchParts = dateString.match(reDateFormatISO8601); if (matchParts === null) return undefined;
let year = makeNumber(matchParts[1]); let month = makeNumber(matchParts[2]); let day = makeNumber(matchParts[3]);
function isLeapYear(y) { return (y % 4 === 0 && y % 100 !== 0) || (y % 400 === 0); }
function dateToAbsoluteDays(y, m, d) { if (y < 1900 || y > 2100) return undefined; let total = 0; for (let i = 1900; i < y; i++) { total += isLeapYear(i) ? 366 : 365; } const daysInMonth = [0, 31, isLeapYear(y) ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; if (d > daysInMonth[m]) return undefined; // Invalid day for (let i = 1; i < m; i++) { total += daysInMonth[i]; } total += d; return total; }
const absDays = dateToAbsoluteDays(year, month, day); if (absDays === undefined) return undefined;
// Jan 1, 1900 was a Monday (absDays = 1). // So absDays % 7 returns: 1 = Mon, 2 = Tue, 3 = Wed, 4 = Thu, 5 = Fri, 6 = Sat, 0 = Sun const dayOfWeek = absDays % 7;
return dayOfWeek === 0 || dayOfWeek === 6;};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;const out = safeFunction(data.out);// ===============================================================================// isWeekend - Direct mode// ===============================================================================const applyCast = (castFn, value) => safeFunction(castFn)(value);const value = applyCast(data.pre, data.src);return out(isWeekend(value));// ===============================================================================// isWeekend(...) β Apply Mode// ===============================================================================/*return function(value) { return out(isWeekend(value));};*/π§ͺ View Test Scenarios (5 tests)
β
'[example] Saturday'β
'[example] Wednesday'β
Sundayβ
Fridayβ
Invalid date returns undefined