Skip to content

getDayIndexFromDateString β€” GTM Variable Template for Date

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

Returns the ISO 8601 day of the week (1 = Monday, 7 = Sunday) from a date string.



Saturday returns 6
INPUT
Date String: 2024-11-02
OUTPUT
6
Monday returns 1
INPUT
Date String: 2024-12-23
OUTPUT
1
Invalid format returns undefined
INPUT
Date String: 25/12/2024
OUTPUT
undefined

This is what you'll see when you open this variable in Google Tag Manager. Hover the icons for details.

getDayIndexFromDateString
Date String
πŸ’Ύ A date string 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.
Date String string
πŸ’‘ Type any text to see the result update live
🎯 Using special value β€” click input to type instead
Test with:
Falsy
Truthy
getDayIndexFromDateString()


πŸ“œ View Implementation Code
/**
* Returns the day index corresponding to an ISO 8601 date string using Zeller's Congruence algorithm.
*
* @param {string} data.src - A date string in YYYY-MM-DD format.
* @param {Function|string} [data.out] - Optional output handler: function to transform result or string with format.
*
* Direct-mode specific parameters:
* @param {Function} [data.pre] - Optional pre-processor function to transform src before calculation.
*
* @returns {number|undefined} The index of the day, or undefined if input is invalid.
*
* @framework ggLowCodeGTMKit
*/
const Math = require('Math');
const makeNumber = require('makeNumber');
const getDayIndexFromDateString = function(dateStr) {
const reDateFormatISO8601 = "(\\d\\d\\d\\d)-(\\d\\d)-(\\d\\d)";
const dateMatchParts = dateStr.match(reDateFormatISO8601);
if (dateMatchParts === null) { return undefined; }
let year = makeNumber(dateMatchParts[1]);
let month = makeNumber(dateMatchParts[2]);
let day = makeNumber(dateMatchParts[3]);
// Zeller's Congruence algorithm
if (month < 3) {
month = month + 12;
year = year - 1;
}
const q = day;
const m = month;
const k = year % 100;
const j = Math.floor(year / 100);
const h = (q + Math.floor((13 * (m + 1)) / 5) + k + Math.floor(k / 4) + Math.floor(j / 4) + 5 * j) % 7;
// Convert Zeller (0=Sat) to ISO 8601 (1=Mon, 7=Sun)
return ((h + 5) % 7) + 1;
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// getDayIndexFromDateString - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
return out(getDayIndexFromDateString(value));
// ===============================================================================
// getDayIndexFromDateString() – Apply Mode
// ===============================================================================
/*
return function(value) {
return out(getDayIndexFromDateString(value));
};
*/
πŸ§ͺ View Test Scenarios (5 tests)
βœ… '[example] Saturday returns 6'
βœ… '[example] Monday returns 1'
βœ… Valid ISO date with January - handles month adjustment correctly
βœ… '[example] Invalid format returns undefined'
βœ… Invalid string - returns undefined