Skip to content

Date Variables

Date Variables

CoreCategory30 Variables

Time is a segmentation axis, a compliance requirement, and an attribution signal — all at once. Fire tags only during business hours. Send Meta CAPI the Unix timestamp it demands. Calculate lead time from form to purchase. Convert dates between every system that speaks a different format. Date is the category that makes time behave — 30 sandbox-safe variable templates for dayparting, timestamps, format conversion, and duration math. No Custom JavaScript.

30 variables100% sandbox-safeWeb + ServerMIT licensed

Every GTM container handles time somewhere. Some tags need to fire only at specific times of day. Some destinations require Unix timestamps in a specific unit. Some dataLayer values arrive as dates in the wrong format. Some attribution reports need duration math between events. These are separate problems with the same substrate — and almost every implementation solves them with one-off Custom JavaScript that nobody maintains.

The Date category covers four jobs:

  • Dayparting and time-based triggers. Fire tags only during business hours, only on weekends, only in specific hour windows. Currently solved with new Date().getHours() in Custom JavaScript — code that lives in one place but whose rules need to change seasonally.
  • Timestamps for destination compliance. Meta CAPI requires event_time in Unix seconds. Some server endpoints want milliseconds. Event deduplication between pixel and CAPI needs the same timestamp in both tags.
  • Duration math. Lead time from form to purchase, days since first visit, time between engagement and conversion. Feeds attribution analysis and can be sent as a Smart Bidding signal.
  • Format and unit conversion. CRM pushes “25/12/2024”, data warehouse expects “2024-12-25”, internal system uses “20241225”. Engagement time in milliseconds, GA4 wants seconds.

All four jobs have clean named-template solutions. No Custom JavaScript required for any of them.

Dates arrive in the dataLayer — and from DOM scraping, cookies, and third-party systems — in every format except the one you want:

Format A

ISO extended

”2024-12-25”
Format B

ISO basic (compact)

“20241225”
Format C

European slash

”25/12/2024”
Format D

US slash

”12/25/2024”
Format E

Unix milliseconds

1735084800000
Format F

Unix seconds

1735084800

Every one of these shows up in real containers — often more than one simultaneously, because different parts of the stack (backend, CRM, analytics) standardize on different conventions. The Date category doesn’t care which you start with; there’s a variable to move between any pair.

Twenty-nine variables, grouped by the job they do. The Business time group is the hero — variables that turn timestamps into segmentation, attribution, and compliance signals.

Service-focused campaigns often should run only when someone is available to answer the phone. B2B lead generation during working hours, e-commerce chat triggers when support is live, remarketing tags that match when a physical store is open.

Recipe — Business-hours trigger condition
Variable: isBusinessHours · Direct mode · Use as trigger condition
FieldValue
Start hour (24h)9
End hour (24h)18
Business daysMonday-Friday
Timezonebrowser (or specify e.g. Europe/Paris)
true between 9:00 and 18:00 local time on weekdays; false otherwise

Add the variable as a trigger condition with isBusinessHours equals true to gate tag firing. To fire a different tag outside business hours (e.g., a “contact us tomorrow” notice), use isBusinessHours equals false on a parallel trigger.

Timezone gotcha. By default, isBusinessHours uses the browser timezone, which means a user in Tokyo hitting a French business’s site during Paris business hours will see “closed.” If your business operates in a specific timezone and you want that perspective globally, set the timezone parameter explicitly.

Weekend promotions, weekday-only B2B campaigns, Tuesday-specific flash sales — day-of-week targeting is one of the most common segmentation patterns in marketing.

Recipe — Weekend-only tag trigger
Variable: isWeekend · Direct mode · Use as trigger condition
FieldValue
Weekend daysSaturday, Sunday (configurable for Middle East markets where Friday-Saturday)
Timezonebrowser or explicit
true on weekend days; false on weekdays

For specific-day targeting, use getDayIndexFromDateString and compare to the numeric index (0 = Sunday, 1 = Monday, through 6 = Saturday). Example trigger condition: getDayIndexFromDateString equals 2 fires only on Tuesdays.

Hour-of-day segmentation goes deeper than business hours — you might want a GA4 custom dimension tracking “morning / afternoon / evening / night” visitor behavior, or a specific tag that fires only during prime-time ad slots (18:00-22:00).

Recipe — Classify current hour into daypart
Variables: getHourOfDay (Apply) → classifyByThreshold (Direct)
FieldValue
getHourOfDay outputInteger 0-23
classifyByThreshold thresholds[6, 12, 18, 22]
classifyByThreshold labels[“night”, “morning”, “afternoon”, “evening”, “late_night”]
14 (2pm) becomes “afternoon”, ready as GA4 custom dimension value

The classified daypart value can feed a GA4 custom dimension, a Google Ads conversion parameter, or a trigger condition. Pair with isWeekend for a combined daypart × weekday/weekend matrix.

Lead time — the duration between two events for the same user — is an attribution signal that most containers don’t capture. The form submission that converts within 24 hours is a hotter lead than the one that converts after 14 days.

Recipe — Lead time from form submission to purchase
Variable: calculateLeadTime · Direct or Apply mode
FieldValue
Start timestamp{{Cookie - lead_form_timestamp}}
End timestamp{{Apply - currentTimestamp}}
Output unitdays (or hours, minutes)
If the form was submitted 3.5 days ago, returns 3.5 (or 84 in hours)

The result is a number you can send as a custom parameter on the conversion event. Advanced: use the lead-time value as an input to a scoring pipeline where shorter lead time increases the conversion value sent to Smart Bidding.

calculateLeadTimeApplyscaleToRangeApplyGoogle Ads conversion value

”Days since first visit,” “days until campaign end,” “days since last purchase for reactivation” — all variations of the same calculation.

Recipe — Days since first visit
Variable: calculateDaysDifference · Direct or Apply mode
FieldValue
Start date{{Cookie - first_visit_date}}
End date{{Apply - currentTimestamp}}
Number of days between the two dates

Bucket the result with classifyByThreshold to produce tiered segments: “new” (0-7 days), “returning” (8-30), “dormant” (31+).

Meta’s Conversions API requires event_time in Unix seconds on every event. Omit it and the event fails. Send it in milliseconds by mistake and the timestamp resolves to the year 55000-something — Meta accepts the event but its attribution is broken.

Source — Meta Conversions API

”A Unix timestamp in seconds indicating when the actual event occurred. You must send this date in GMT time zone.”

developers.facebook.com — Conversions API parameters
Recipe — event_time for Meta CAPI
Variables: currentTimestamp (Apply) → millisecondsToSeconds (Direct)
FieldValue
currentTimestamp outputUnix ms — 1735084800000
millisecondsToSeconds outputUnix s — 1735084800
Use the final variable as event_time in the Meta CAPI tag

For event deduplication between the browser pixel and CAPI, both tags must send the same event_time. Reference the same variable from both, and pair with an event_id (typically from randomInteger).

Convert date formats between CRM and warehouse

Section titled “Convert date formats between CRM and warehouse”

Multi-system tracking runs into a universal problem: CRM sends “25/12/2024”, data warehouse expects “2024-12-25”, analytics system stores “20241225”.

Recipe — European slash date to ISO extended
Variables: reverseDateSlash (Apply) → replace (Direct, slashes to dashes)
FieldValue
Input”25/12/2024” (DD/MM/YYYY)
After reverseDateSlash”2024/12/25”
After replace /→-“2024-12-25”
ISO-compliant date string ready for a warehouse or BigQuery export

For the simplest format swap — ISO basic to extended — basicDateToExtended is the single-variable answer: “20241225” becomes “2024-12-25” directly. Use extendedDateToBasic for the reverse.

Convert engagement milliseconds to GA4-ready units

Section titled “Convert engagement milliseconds to GA4-ready units”

Browser APIs return durations in milliseconds. performance.now(), custom timing marks, video playback duration. GA4’s engagement time parameters expect seconds.

Recipe — Convert raw engagement ms to seconds for GA4
Variable: millisecondsToSeconds · Direct or Apply mode
FieldValue
Input{{DL - engagement_ms}} — e.g. 45000
45 (seconds) ready for the GA4 event parameter

The unit-conversion pair set (millisecondsToSeconds, millisecondsToMinutes, millisecondsToHours, millisecondsToDays and their reverses) covers every direction. Chain toFixedNumber downstream for specific decimal precision.

Reports read better with month and day names than numbers. “December” in a GA4 custom dimension is more useful than 12.

Recipe — Month name for GA4 custom dimension
Variable: getMonthName · Direct mode
FieldValue
Sourcecurrent date (default) or configurable input date
Localeen-US, fr-FR, de-DE, etc.
“December” in English, “décembre” in French, “Dezember” in German

For charts with limited horizontal space, use getMonthNameShort (“Dec”) and getDayNameShort (“Mon”).

Convert Unix timestamps back to readable dates

Section titled “Convert Unix timestamps back to readable dates”

Cookies and backend systems frequently store timestamps as Unix numbers — but for display in GA4 custom dimensions you need a readable date.

Recipe — Cookie timestamp to readable date
Variable: unixTimestampToDate · Direct or Apply mode
FieldValue
Input{{Cookie - first_visit_ts}} — Unix seconds or ms
Output formatYYYY-MM-DD (ISO) or configurable
1735084800 becomes “2024-12-25”

For just the day-of-week component, use timestampToDayIndex — a fast direct path that avoids the full date conversion when you only need 0-6.

Campaign end dates, subscription renewal calculations, “next available date” logic — all need date arithmetic. addDays handles forward and backward moves (negative values go back in time).

Recipe — Calculate campaign end date from start date
Variable: addDays · Direct or Apply mode
FieldValue
Source date{{DL - campaign_start_date}}
Days to add30
“2024-12-01” + 30 days = “2024-12-31”

For “7 days ago from now,” use addDays with input currentTimestamp and days -7. Combine with calculateDaysDifference to build recency/frequency segmentation entirely in the container.

Four chains that cover most real-world date pipelines:

Meta CAPI event_time

Current time in Unix seconds, ready for the CAPI event_time parameter.

currentTimestampApplymillisecondsToSecondsDirect

Dayparting for GA4 custom dimension

Current hour classified into a named daypart, ready as a dimension value.

getHourOfDayApplyclassifyByThresholdDirect

Lead-time conversion value

Lead time converted to a Smart Bidding signal where shorter = higher conversion value.

calculateLeadTimeApplyclampApplyscaleToRangeDirect

CRM-to-warehouse date normalization

European slash date converted to ISO extended format for a data warehouse export.

reverseDateSlashApplyreplaceDirect

Every tutorial on date handling in GTM eventually arrives at a variation of the same pattern:

function() {
var now = new Date();
var hour = now.getHours();
var day = now.getDay();
if (day === 0 || day === 6) return false;
if (hour < 9 || hour >= 18) return false;
return true;
}

Or worse:

function() {
var parts = {{DLV - date_str}}.split(’/’);
var iso = parts[2] + ’-’ + parts[1] + ’-’ + parts[0];
return iso;
}

Works until it doesn’t. The first breaks silently in timezone edge cases. The second breaks the day someone pushes “12-25-2024” with dashes instead of slashes. The variable name is cjs - biz_hrs and nobody remembers whether it accounts for holidays.

Named templates replace the whole pattern:

  • Intent in the name. isBusinessHours means exactly one thing. cjs - biz_hrs means whatever the code inside happens to do.
  • Configuration lives in the UI. Changing business hours for daylight savings means editing a variable config, not code.
  • Format handling is centralized. The day someone discovers reverseDateSlash doesn’t handle a new CRM format, you fix it once and every container importing it picks up the fix.
  • Server-side portability. sGTM’s stricter sandbox accepts named templates that ad-hoc Custom JavaScript wouldn’t.
GoalVariableMode
Fire tag only during business hoursisBusinessHoursDirect
Fire tag only on weekends / weekdaysisWeekendDirect
Extract current hour (0-23)getHourOfDayDirect / Apply
Lead time between two eventscalculateLeadTimeDirect / Apply
Days between two datescalculateDaysDifferenceDirect / Apply
Current Unix timestamp (ms)currentTimestampDirect / Apply
Add or subtract days from a dateaddDaysDirect / Apply
Full / short day namegetDayName / getDayNameShortDirect
Day name from date string inputgetDayNameFromDateStringApply
Numeric day index (0 = Sunday)getDayIndexFromDateString / timestampToDayIndexApply
Full / short month namegetMonthName / getMonthNameShortDirect
ISO basic ↔ extended (YYYYMMDD ↔ YYYY-MM-DD)basicDateToExtended / extendedDateToBasicApply
Swap date component orderreverseDateSlash / reverseDateCompact*Apply
Date ↔ Unix timestampdateToUnixTimestamp / unixTimestampToDateApply
ms ↔ secondsmillisecondsToSeconds / secondsToMillisecondsApply
ms ↔ minutes / hours / daysmillisecondsToMinutes, etc.Apply

How do I fire a GTM tag only during business hours?

Use isBusinessHours as a trigger condition. Configure the start hour, end hour, business days, and timezone. The variable returns true when the current time falls within the defined window, false otherwise.

How do I fire a tag only on weekdays or weekends?

isWeekend returns a boolean. Use as a trigger condition with equals true for weekend-only or equals false for weekday-only tags. For specific days, use getDayIndexFromDateString and compare to the numeric index (0 = Sunday, 1 = Monday, etc.).

How do I calculate lead time between a form submission and a purchase?

Store the form submission timestamp in a cookie. On the purchase event, read the stored timestamp and pass it with currentTimestamp to calculateLeadTime. Returns the duration in days (or configurable units). Send as a custom parameter on the conversion event.

How do I add a Unix timestamp to a Meta CAPI event?

Meta CAPI requires event_time in Unix seconds. Use currentTimestamp to get milliseconds, then chain millisecondsToSeconds. For event deduplication between the browser pixel and CAPI, reference the same variable in both tags.

How do I convert date formats between systems?

reverseDateSlash, reverseDateCompactDay1st, and reverseDateCompactYear1st handle common format swaps. basicDateToExtended converts YYYYMMDD to YYYY-MM-DD; extendedDateToBasic does the reverse. Chain them to normalize dates between CRM, warehouse, and analytics.

How do I extract the day of week?

getDayName returns a full name like “Monday”. getDayNameShort returns “Mon”. getDayIndexFromDateString returns the numeric index (0 = Sunday through 6 = Saturday) for comparison logic. Use the FromDateString variants when parsing from a date string input.

Are these variables sandbox-safe in server-side GTM?

Yes. All templates run inside GTM’s sandboxed JavaScript environment using only permissioned APIs. They work in web and server containers.