Skip to content

reverseDateSlash — GTM Variable Template for Date

VARIABLES › DATE
reverseDateSlash EXTENDED Date

Reverses a date string between "dd/mm/yyyy" and "yyyy/mm/dd" formats.



Examples

Reverse dd/mm/yyyy
INPUT
Date String: 25/12/2024
OUTPUT
2024/12/25
Reverse yyyy/mm/dd
INPUT
Date String: 2025/01/01
OUTPUT
01/01/2025

GTM Configuration

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

reverseDateSlash
Date String
💾 The date string to reverse, using slash separators.

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
reverseDateSlash()


Under the Hood

📜 View Implementation Code
/**
* Reverse a date string between "dd/mm/yyyy" and "yyyy/mm/dd" formats.
* 
* @param {string} data.src - The date string to reverse, using slash separators.
* @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 reversing.
* 
* @returns {string|undefined} The reversed date string, or undefined if the input is invalid.
*
* @framework ggLowCodeGTMKit
*/
const reverseDateSlash = function(date) {
   if (typeof date === 'string') {
       const parts = date.split('/');
       if (parts.length === 3) {
           return parts.reverse().join('/');
       }
   }
   return undefined;
};

const safeFunction = fn => typeof fn === 'function' ? fn : x => x;
const out = safeFunction(data.out);

// ===============================================================================
// reverseDateSlash - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
return out(reverseDateSlash(value));
// ===============================================================================
// reverseDateSlash() – Apply Mode
// ===============================================================================
/*
return function(value) {
  return out(reverseDateSlash(value));
};
*/
🧪 View Test Scenarios (5 tests)
✅ '[example] Reverse dd/mm/yyyy'
✅ '[example] Reverse yyyy/mm/dd'
✅ Valid date with leading zeros - reverses correctly
✅ Invalid format without slashes - returns undefined
✅ Invalid format with wrong number of parts - returns undefined