Skip to content

furiber/gtm-current-time-variable

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 

Repository files navigation

GTM Current Date/Time Variable

A Google Tag Manager (GTM) client-side variable template that returns the current date and time with timezone support and flexible formatting options.

Features

  • 39 Timezone Options: Covers major cities and timezones worldwide
  • 20 Output Formats: From ISO 8601 to human-readable formats
  • Custom Format Patterns: Create your own date/time format
  • Multi-Locale Support: 13 languages for localized output
  • Lightweight: Pure sandboxed JavaScript (no external dependencies)

Installation

Option 1: Import from Community Template Gallery

  1. In Google Tag Manager, go to Templates > Variable Templates
  2. Click Search Gallery
  3. Search for "Current Date/Time Variable"
  4. Click Add to workspace

Option 2: Manual Import

  1. Download the template.tpl file from this repository
  2. In Google Tag Manager, go to Templates > Variable Templates
  3. Click New
  4. Click the three-dot menu (top right) > Import
  5. Select the downloaded template.tpl file
  6. Click Save

Usage

Creating a Variable

  1. Go to Variables > User-Defined Variables
  2. Click New
  3. Choose Current Date/Time Variable from the template list
  4. Configure the options:
    • Timezone: Select the timezone for the output
    • Output Format: Choose the desired format
    • Locale: Select language for localized text (day/month names)
  5. Save your variable

Configuration Options

Timezone

Select from 39 timezone options:

Region Timezones
Local Browser's local timezone
UTC Coordinated Universal Time
Americas New York, Chicago, Denver, Los Angeles, Anchorage, Honolulu, Phoenix, Toronto, Vancouver, Mexico City, Sao Paulo, Buenos Aires
Europe London, Paris, Berlin, Amsterdam, Madrid, Rome, Zurich, Stockholm, Moscow, Istanbul
Asia Dubai, Kolkata, Bangkok, Singapore, Hong Kong, Shanghai, Tokyo, Seoul
Oceania Sydney, Melbourne, Perth, Auckland
Africa Cairo, Johannesburg

Output Format

Choose from 20 built-in formats:

Format Example Output
ISO 8601 2024-01-15T14:30:00
ISO 8601 with Timezone 2024-01-15T14:30:00-05:00
Date Only 2024-01-15
Time Only 14:30:00
Time 12-hour 2:30:00 PM
Date and Time 2024-01-15 14:30:00
Date and Time 12-hour 2024-01-15 2:30:00 PM
Readable January 15, 2024 2:30 PM
Unix Timestamp (ms) 1705329000000
Unix Timestamp (sec) 1705329000
Day of Week Monday
Day of Week Short Mon
Month Name January
Year 2024
Month 01
Day 15
Hour (24h) 14
Hour (12h) 2
Minute 30
Custom Format User-defined pattern

Custom Format Patterns

When using "Custom Format", you can create your own pattern using these tokens:

Token Description Example
YYYY 4-digit year 2024
MM 2-digit month 01
DD 2-digit day 15
HH 2-digit hour (24h) 14
hh 2-digit hour (12h) 02
mm 2-digit minute 30
ss 2-digit second 45
A AM/PM PM
dddd Full day name Monday
ddd Short day name Mon
MMMM Full month name January
MMM Short month name Jan

Custom Format Examples:

Pattern Output
YYYY/MM/DD 2024/01/15
DD-MM-YYYY 15-01-2024
dddd, MMMM DD, YYYY Monday, January 15, 2024
hh:mm A 02:30 PM
YYYY-MM-DD HH:mm:ss 2024-01-15 14:30:45

Locale

Available locales for localized text output:

  • English (US) - en-US
  • English (UK) - en-GB
  • German - de-DE
  • French - fr-FR
  • Spanish - es-ES
  • Italian - it-IT
  • Portuguese (Brazil) - pt-BR
  • Dutch - nl-NL
  • Japanese - ja-JP
  • Chinese (Simplified) - zh-CN
  • Korean - ko-KR
  • Russian - ru-RU
  • Arabic - ar-SA

Example Use Cases

1. Track Time-Based User Behavior

Create a variable with the "hour" format to track which hours users are most active:

Timezone: Local
Output Format: Hour (24h)

Use in a Custom Dimension in Google Analytics 4.

2. Dayparting for Ad Campaigns

Create a variable to segment users by time of day:

Timezone: America/New_York
Output Format: Hour (24h)

Then create a lookup table variable to categorize:

  • 6-12: Morning
  • 12-18: Afternoon
  • 18-22: Evening
  • 22-6: Night

3. Log Timestamps in Data Layer

Push ISO timestamps to your data layer:

Timezone: UTC
Output Format: ISO 8601 with Timezone

4. Display Local Date on Website

Use with Custom HTML tag to show formatted date:

Timezone: Local
Output Format: Readable
Locale: (match your site's language)

5. Weekend vs Weekday Analysis

Timezone: Local
Output Format: Day of Week

Use in triggers or custom dimensions to compare behavior.

Technical Notes

  • Uses pure GTM sandboxed JavaScript APIs (getTimestampMillis, makeNumber, makeString, Math)
  • Calculates date components using mathematical algorithms (proleptic Gregorian calendar)
  • Fixed timezone offsets are used (standard time) - DST is not automatically handled
  • The variable is evaluated each time it's referenced (not cached)
  • Works in all browsers without requiring additional permissions

DST Considerations

This template uses fixed UTC offsets for each timezone, representing standard time. During daylight saving time periods, the displayed time may be off by one hour for timezones that observe DST. For applications requiring precise DST handling, consider:

  • Using UTC timezone (which doesn't observe DST)
  • Manually adjusting the offset during DST periods
  • Using the "Local" timezone option which respects the browser's settings

Permissions

This template requires minimal permissions:

  • Logging (debug only): For development and debugging purposes

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the Apache License 2.0 - see the LICENSE file for details.

Support

If you encounter any issues or have feature requests, please open an issue on GitHub.

About

Current time in GTM in a variable

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages