basicDateToExtended β GTM Variable Template for Date
basicDateToExtended EXTENDED Date
Converts a basic ISO date (YYYYMMDD) to extended format (YYYY-MM-DD).
When to Use This
Section titled βWhen to Use ThisβDate Formatting
Format and transform date values into human-readable or machine-readable strings.
Type Conversion
Safely convert between data types β strings, numbers, booleans, arrays, objects.
Date & Time
Calculate durations, differences, and time-based operations on date values.
Examples
Section titled βExamplesβBasic to extended ISO date
INPUT
Compact Date: 20241225
OUTPUT
2024-12-25
Invalid returns undefined
INPUT
Compact Date: 2024122
OUTPUT
undefined
Live Sandbox
Section titled βLive Sandboxβ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
Section titled βRelated VariablesβSame category: Date
Under the Hood
Section titled β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