getDayIndexFromDateString — GTM Variable Template for Date
getDayIndexFromDateString EXTENDED Date
Returns the index of the day of the week (0 = Sunday, 6 = Saturday) based on an ISO 8601 date string.
Examples
Saturday returns 6
INPUT
Date String: 2024-11-02
OUTPUT
0
Monday returns 1
INPUT
Date String: 2024-12-23
OUTPUT
2
Invalid format returns undefined
INPUT
Date String: 25/12/2024
OUTPUT
undefined
GTM Configuration
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
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
🔗 Result Handling — Chain Variables
Chain apply-mode variables to the output. Each variable receives the result of the previous one.
getDayIndexFromDateString()
Related Variables
Same category: Date
Under the Hood
📜 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;
return h;
};
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