millisecondsToSeconds β GTM Variable Template for Date
When to Use This
Section titled βWhen to Use ThisβTime Operations
Measure durations, calculate elapsed time, and format timestamps.
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βMilliseconds to seconds
INPUT
Number of Milliseconds: 1000
OUTPUT
1
30 seconds conversion
INPUT
Number of Milliseconds: 30000
OUTPUT
30
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.
millisecondsToSeconds
Number of Milliseconds
πΎ The number of milliseconds to convert to seconds.
Supported formats:
β Number
β String
Supported formats:
β Number
β 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.
Number of Milliseconds 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.
millisecondsToSeconds()
Related Variables
Section titled βRelated VariablesβSame category: Date
Under the Hood
Section titled βUnder the Hoodβπ View Implementation Code
/*** Converts a number of milliseconds to seconds.** @param {string|number} data.src - The number of milliseconds to convert to seconds.* @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 {number|undefined} The time in seconds, or undefined if input is not a valid number.** @framework ggLowCodeGTMKit*/const makeNumber = require('makeNumber');
const millisecondsToSeconds = function(timeValue) { const timeNum = makeNumber(timeValue); if (timeNum === timeNum) { return timeNum / 1000; } return undefined;};
const safeFunction = fn => typeof fn === 'function' ? fn : x => x;const out = safeFunction(data.out);
// ===============================================================================// millisecondsToSeconds - Direct mode// ===============================================================================const applyCast = (castFn, value) => safeFunction(castFn)(value);const value = applyCast(data.pre, data.src);return out(millisecondsToSeconds(value));// ===============================================================================// millisecondsToSeconds() β Apply Mode// ===============================================================================/*return function(value) { return out(millisecondsToSeconds(value));};*/π§ͺ View Test Scenarios (6 tests)
β
'[example] Milliseconds to seconds'β
'[example] 30 seconds conversion'β
String milliseconds - converts to secondsβ
Partial second milliseconds - converts to decimal secondsβ
Invalid string input - returns undefinedβ
Invalid input - returns undefined