Skip to content

Latest commit

 

History

History
32 lines (28 loc) · 1.82 KB

local-timezone-timestamp.md

File metadata and controls

32 lines (28 loc) · 1.82 KB

Create a Timestamp in a fixed timezone (independend of the user's local timezone)

This is useful, if you want to send timestamps in a fixed timezone, for example of your websites location, ignoring the timezone of the user.

1. Create Custom HTML Tag

This Tag makes sure, the dayjs javascript library is loaded. Make sure this Tag always fires before executing the javascript in the second step. Code:

<script src="https://cdn.jsdelivr.net/npm/dayjs@latest/dayjs.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/dayjs@latest/plugin/utc.js"></script>
<script src="https://cdn.jsdelivr.net/npm/dayjs@latest/plugin/timezone.js"></script>

1. Create timestamp

You can either use a Custom Javascript Variable, Custom HTML Tag or any other means where you can write javascript. If you use a Custom HTML Tag, you can combine step 1 and 2 into one Tag. Here's a code example:

dayjs.extend(dayjs_plugin_utc);
dayjs.extend(dayjs_plugin_timezone);
let localTime = dayjs.tz(dayjs.utc(), "Europe/Zurich");
dataLayer.push({
  "event":         "timeStamp",
  "unixTimestamp": localTime,                               // Unix Timestamp is always GMT/UTC, a local Unix timestamp doesn't exist
  "localTime":     localTime.format(),                      // local time as ISO8601 format
  "localTimeCust": localTime.format("YYYY-MM-DD_HH-mm-ss"), // local time with your own format definition
  "utcTime":       dayjs.utc().format(),                    // GMT/UTC time as ISO8601 format
  "utcTimeCust":   dayjs.utc().format("YYYY-MM-DD_HH-mm-ss")// GMT/UTC time with your own format definition
});