Skip to content

hoursToMilliseconds — GTM Variable Template for Time

VARIABLES › TIME
hoursToMilliseconds EXTENDED Time

Converts hours to milliseconds.


Examples

Hours to milliseconds
INPUT
Number of Hours: 2
OUTPUT
7200000
Zero returns 0
INPUT
Number of Hours: 0
OUTPUT
0

GTM Configuration

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

hoursToMilliseconds
Number of Hours
💾 The number of hours to convert to milliseconds.

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 Hours number
💡 Type any text to see the result update live
🎯 Using special value — click input to type instead
Test with:
Falsy
Truthy
hoursToMilliseconds()


Under the Hood

📜 View Implementation Code
/**
* Converts a number of hours to milliseconds.
* 
* @param {string|number} data.src - The number of hours to convert to milliseconds.
* @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 milliseconds, or undefined if input is not a valid number.
*
* @framework ggLowCodeGTMKit
*/
const makeNumber = require('makeNumber');

const hoursToMilliseconds = function(timeValue) {
   const timeNum = makeNumber(timeValue);
   if (timeNum === timeNum) {
       return timeNum * 1000 * 60 * 60;
   }
   return undefined;
};

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

// ===============================================================================
// hoursToMilliseconds - Direct mode
// ===============================================================================
const applyCast = (castFn, value) => safeFunction(castFn)(value);
const value = applyCast(data.pre, data.src);
return out(hoursToMilliseconds(value));

// ===============================================================================
// hoursToMilliseconds() – Apply Mode
// ===============================================================================
/*
return function(value) {
  return out(hoursToMilliseconds(value));
};
*/
🧪 View Test Scenarios (5 tests)
✅ '[example] Hours to milliseconds'
✅ String number of hours - converts to milliseconds
✅ Decimal hours - converts fractional hours correctly
✅ Invalid string input - returns undefined
✅ '[example] Zero returns 0'