getDayNameFromDateString — GTM Variable Template for Date
getDayNameFromDateString CORE Date
Returns the day name from an ISO 8601 date string (YYYY-MM-DD).
When to Use This
Date Formatting
Format and transform date values into human-readable or machine-readable strings.
Date & Time
Calculate durations, differences, and time-based operations on date values.
Examples
ISO date to Saturday
INPUT
Date String (YYYY-MM-DD): 2024-11-02
OUTPUT
Saturday
Invalid format returns undefined
INPUT
Date String (YYYY-MM-DD): 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.
getDayNameFromDateString
Date String (YYYY-MM-DD)
💾 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 (YYYY-MM-DD) 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.
getDayNameFromDateString()
Related Variables
Same category: Date
Under the Hood
📜 View Implementation Code
/**
* Returns the day name (e.g., "Monday") 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 {string|undefined} The name of the day (e.g., "Friday"), or undefined if input is invalid.
*
* @framework ggLowCodeGTMKit
*/
const Math = require('Math');
const makeNumber = require('makeNumber');
const getDayNameFromDateString = 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;
const days = ["Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"];
return days[h];
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// getDayNameFromDateString - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
return out(getDayNameFromDateString(value));
// ===============================================================================
// getDayNameFromDateString() – Apply Mode
// ===============================================================================
/*
return function(value) {
return out(getDayNameFromDateString(value));
};
*/🧪 View Test Scenarios (5 tests)
✅ '[example] ISO date to Saturday'
✅ Valid ISO date Monday - returns Monday
✅ Valid ISO date with January - handles month adjustment correctly
✅ '[example] Invalid format returns undefined'
✅ Invalid string - returns undefined