Making Sure the Date/Time Zone is Correct

Posted on September 23, 2014 in

We write a lot of AdWords scripts, and a recent one I was writing was for a client in Texas (Central Time Zone). The ads were going to stop displaying at a certain time, but I noticed in writing the script, the date generated for the current time was always Pacific (PDT).

After a little digging, I came to understand that even though the AdWords account time was Central, dates generated in an AdWords script are based off of the server time where the script is running. At that time, my scripts were running in a data center on Pacific time. Since data centers can move, and it’s possible the script can execute in a data center in a different time zone, it’s better to rely on your accounts time instead. Especially if you are creating a script that performs some function at a specific time, like turning on and off ads, changing copy within ads, etc.

The following are two example methods I used in order to make sure all dates were set in the same time zone. This first one just returns the current correct account time (not the server time at Google).

/**
 * Make sure when getting a date object, we're basing it off the account time, not the data center server time
 */ 
function getAccountCurrentDateTime() {
  return new Date(Utilities.formatDate(new Date(), AdWordsApp.currentAccount().getTimeZone(), "MMM dd,yyyy HH:mm:ss"));
}

Or use a method like this if you want to make sure a date object your working with matches the correct time for the account.

/**
 * The time will convert to whatever server/data center the script is running on. Make sure the time is set to the AW account time
 * @param {Date} date
 */
function setProperTimeZone(date) {
  return new Date(Utilities.formatDate(date, AdWordsApp.currentAccount().getTimeZone(), "MMM dd,yyyy HH:mm:ss"));
}

I hope these are helpful!

By Nathan Byloff

Nathan is the CTO for RankHammer. His area of expertise is technical SEO and everything to do with data - collection, analysis, etc. He is driven by automating any reporting task that has to be done more than once.
  • rwong

    I tried your script. It returns the wrong datetime.
    1. AdWordsApp.currentAccount().getTimeZone() returned ‘America/Chicago’ which is not one of the time zone value accepted by Utilities.formatDate()
    2. new Date(Utilities.formatDate(new Date(), AdWordsApp.currentAccount().getTimeZone(), “MMM dd,yyyy HH:mm:ss”)) returns a date time in ‘PST’ because the date format string ( “MMM dd,yyyy HH:mm:ss”) did not specify a time zone.

  • rwong When in AdWords and you click the gear in the upper right corner and check the time zone, what does it say? Then, in the same account and you run an AdWords script and execute Logger.log(getAccountCurrentDateTime()); what happens?

  • rwong

    nbyloff rwong

    This is the output from the console.
    Fri Sep 26 23:42:35 GMT-07:00 2014

  • rwong nbyloff rwong That looks OK to me? I am in CST too, so I ran a test just to ease your mind. Here was the entire statement:

    Logger.log(getAccountCurrentDateTime());
      Logger.log(new Date());
     The output from that statement was:
    Fri Sep 26 23:48:04 GMT-07:00 2014
    Fri Sep 26 21:48:05 GMT-07:00 2014

    It is almost midnight here, which the first statement corresponds with, and Pacific time is 2 hours behind, where the typical data center I’ve been hitting the past week lines up with. I am looking at your date object, and it seems to signify it’s getting the account time zone you’re looking for (CST), so can you run the two statements above and show me the difference between them? Are you in CST so it won’t matter or can you show the difference?

  • rwong

    nbyloff rwong

    Thank you for helping me with this at this time of the night. Here are my results

    Fri Sep 26 23:59:25 GMT-07:00 2014 Fri Sep 26 21:59:25 GMT-07:00 2014
    I was expecting GMT-05:00 from Logger.log(getAccountCurrentDateTime())

  • rwong nbyloff OK, so what time are you expecting? You said your client’s time zone is CST. Currently, it’s 00:08:00 GMT -7:00 2014 (sorry for the delayed response). So if you are looking for your client’s time zone, which is CST, why do you feel the date you posted: 
    Fri Sep 26 23:59:25 GMT-07:00 2014

    Is incorrect? Because that first time stamp you pasted in your comments, is correct for CST. I am not sure where your problem lies?

  • rwong

    nbyloff rwong

    I was expecting
    Fri Sep 26 21:59:25 GMT-07:00 2014 (PST time zone) 

    converted by getAccountCurrentDateTime() to

    Fri Sep 26 23:59:25 GMT-05:00 2014 (CST time zone)

    I think I got it now. The timestamp without the time-offset is in CST time. My script was using the whole date object which is in PST timezone for date calculation. That is where I got off tracked in my script. Thank you for your help.

  • rwong nbyloff Ah excellent. Glad you got it working. 🙂

  • WouterNieuwerth

    I would just like to say thanks! I was struggling with a time difference between a date/time that I imported from an XML feed and the current date/time in the Adwords account and this helped a lot!

    So once more, THANKS!

  • WouterNieuwerth You’re welcome; glad you found it useful. Making sure the time is synced between two data sources can be a pain sometimes. Feedback is always appreciated.