basicDateToExtended — GTM Variable Template for Time
basicDateToExtended EXTENDED Time
Converts a basic ISO date (YYYYMMDD) to extended format (YYYY-MM-DD).
Examples
Basic to extended ISO date
INPUT
Compact Date: 20241225
OUTPUT
2024-12-25
Invalid returns undefined
INPUT
Compact Date: 2024122
OUTPUT
undefined
GTM Configuration
This is what you'll see when you open this variable in Google Tag Manager. Hover the icons for details.
basicDateToExtended
Compact Date
💾 The compact date string in "yyyymmdd" 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.
Compact Date number
💡 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.
basicDateToExtended()
Related Variables
Same category: Time
Under the Hood
📜 View Implementation Code
/**
* Convert a basic ISO 8601 date (yyyymmdd) to extended format (yyyy-mm-dd).
*
* @param {string} data.src - The compact date string in "yyyymmdd" 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 conversion.
*
* @returns {string|undefined} The extended date string in "yyyy-mm-dd" format, or undefined if input is invalid.
*
* @framework ggLowCodeGTMKit
*/
const basicDateToExtended = function(date) {
if (typeof date !== 'string' || date.length !== 8) { return undefined; }
const year = date.slice(0, 4);
const month = date.slice(4, 6);
const day = date.slice(6, 8);
return year + "-" + month + "-" + day;
};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);
// ===============================================================================
// basicDateToExtended - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
return out(basicDateToExtended(value));
// ===============================================================================
// basicDateToExtended() – Apply Mode
// ===============================================================================
/*
return function(value) {
return out(basicDateToExtended(value));
};
*/🧪 View Test Scenarios (5 tests)
✅ '[example] Basic to extended ISO date'
✅ Valid date with January first - converts correctly
✅ Valid date end of year - converts correctly
✅ '[example] Invalid returns undefined'
✅ Non-string input - returns undefined