Skip to content

isWeekend β€” GTM Variable Template for Logic

VARIABLES β€Ί LOGIC
isWeekend EXTENDED Logic

Determines if a given date string is a weekend (Saturday or Sunday) and returns true or false.


Conditional Logic

Branch logic based on conditions β€” if/else, ternary, boolean gates.

Date & Time

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


Saturday
INPUT
Target Date: 2024-04-27
OUTPUT
true
Wednesday
INPUT
Target Date: 2024-04-24
OUTPUT
false

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


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