Home > Back-end >  Timestamp on Google Tag Manager
Timestamp on Google Tag Manager

Time:08-04

I need a little help to create a variable on google tag manager that takes me the timestamp of when an event is triggered.

Now I have implemented this custom javascript but it takes me as the timezone that of the country of the user who will trigger the event. On the other hand, I would like timezone always 02.00, can someone teach me which part of the code I need to modify to have my timezone always set?

Thank you

function() 
{

  var now = new Date();
  var tzo = -now.getTimezoneOffset();
  var dif = tzo >= 0 ? ' ' : '-';
  var pad = function(num) {
    var norm = Math.abs(Math.floor(num));
    return (norm < 10 ? '0' : '')   norm;
};
  return now.getHours()
      ':'   pad(now.getMinutes()) 
      ':'   pad(now.getSeconds())
      '.'   pad(now.getMilliseconds())
      dif   pad(tzo / 60) 
      ':'   pad(tzo % 60);
}

CodePudding user response:

This should be ample:

function qwe() {
  var d = new Date();
  nd = new Date((d.getTime()   (d.getTimezoneOffset() * 60000))   (3600000 * 2));
  var tzo = 120;
  var dif = tzo >= 0 ? ' ' : '-';
  var pad = function (num) {
    var norm = Math.abs(Math.floor(num));
    return (norm < 10 ? '0' : '')   norm;
  };
  return nd.getHours()
      ':'   pad(nd.getMinutes())
      ':'   pad(nd.getSeconds())
      '.'   pad(nd.getMilliseconds())
      dif   pad(tzo / 60)
      ':'   pad(tzo % 60);
}

CodePudding user response:

Try to see this answer to "How to ignore user's time zone and force Date() use specific time zone": How to ignore user's time zone and force Date() use specific time zone

It says:

A Date object's underlying value is actually in UTC. To prove this, notice that if you type new Date(0) you'll see something like: Wed Dec 31 1969 16:00:00 GMT-0800 (PST). 0 is treated as 0 in GMT, but .toString() method shows the local time.

Big note, UTC stands for Universal time code. The current time right now in 2 different places is the same UTC, but the output can be formatted differently.

What we need here is some formatting

var _date = new Date(1270544790922); 
// outputs > "Tue Apr 06 2010 02:06:30 GMT-0700 (PDT)", for me
_date.toLocaleString('fi-FI', { timeZone: 'Europe/Helsinki' });
// outputs > "6.4.2010 klo 12.06.30"
_date.toLocaleString('en-US', { timeZone: 'Europe/Helsinki' });
// outputs > "4/6/2010, 12:06:30 PM"

This works but.... you can't really use any of the other date methods for your purposes since they describe the user's timezone. What you want is a date object that's related to the Helsinki timezone. Your options at this point are to use some 3rd party library (I recommend this), or hack-up the date object so you can use most of it's methods.

Option 1 - a 3rd party like moment-timezone

moment(1270544790922).tz('Europe/Helsinki').format('YYYY-MM-DD HH:mm:ss')
// outputs > 2010-04-06 12:06:30
moment(1270544790922).tz('Europe/Helsinki').hour()
// outputs > 12

This looks a lot more elegant than what we're about to do next.

Option 2 - Hack up the date object

var currentHelsinkiHoursOffset = 2; // sometimes it is 3
var date = new Date(1270544790922);
var helsenkiOffset = currentHelsinkiHoursOffset*60*60000;
var userOffset = _date.getTimezoneOffset()*60000; // [min*60000 = ms]
var helsenkiTime = new Date(date.getTime()  helsenkiOffset   userOffset);
// Outputs > Tue Apr 06 2010 12:06:30 GMT-0700 (PDT)

It still thinks it's GMT-0700 (PDT), but if you don't stare too hard you may be able to mistake that for a date object that's useful for your purposes.

I conveniently skipped a part. You need to be able to define currentHelsinkiOffset. If you can use date.getTimezoneOffset() on the server side, or just use some if statements to describe when the time zone changes will occur, that should solve your problem.

Conclusion - I think especially for this purpose you should use a date library like moment-timezone.

  • Related