Timezone Issue

477 views
Skip to first unread message

ECohen

unread,
Aug 23, 2023, 4:59:48 PM8/23/23
to Google Ads Scripts Forum
Hello,

Recently, we had our Google Ads MCC account switched from Pacific Time to Eastern Time. To make a long story short, I believe I am experiencing some sort of issue with my ad scripts as a result because time doesn't seem to be actually matching up. Here is what I have to illustrate the problem. I have this quick script that just outputs the time zone and current time to the log file:

var currentTime = new Date();
Logger.log('The time zone is: ' +  AdsApp.currentAccount().getTimeZone());
Logger.log('The current time is: ' + currentTime);

Here is the result - notice that the log time doesn't match up with the "system" time somehow. The log time is 4:47:52pm, but the output says that it is 13:47:52 (GMT-700). I am pretty sure they should match up, but they don't.

8/23/2023 4:47:52 PM 
The time zone is: America/New_York
8/23/2023 4:47:52 PM 
The current time is: Wed Aug 23 2023 13:47:52 GMT-0700 (GMT-07:00)

Any ideas on what could be causing this would be greatly appreciated. I suspect it's some kind of bug, and it's throwing off the data I am gathering with my script.

Google Ads Scripts Forum Advisor

unread,
Aug 23, 2023, 6:33:00 PM8/23/23
to adwords...@googlegroups.com

Hi,

Thank you for reaching out to the Google Ads Scripts team.

Based on our documentation (https://developers.google.com/google-ads/scripts-legacy/docs/features/dates#common_pitfalls), when directly logging a date object using Logger.log(), it is rendered using a default format and timezone. The default timezone is America/Los_Angeles (Pacific time), regardless of the timezone associated with the Google Ads account. If you would like to render the date object as a string using a custom format and timezone for logging or other purposes, always use Utilities.formatDate(date, timeZone, format).

For additional context, the America/New_York timezone is 3 hours ahead of America/Los_Angeles so it appears that the result you're getting is expected behavior. Let us know if you have further clarification.

This message is in relation to case "ref:_00D1U1174p._5004Q2o7mIv:ref"

Thanks,
 
Google Logo Google Ads Scripts Team


Nils Rooijmans

unread,
Aug 24, 2023, 3:17:58 AM8/24/23
to Google Ads Scripts Forum
When you create a new Date object in Javascript, it uses the local time of the computer it's running on. In the case of Google Ads Scripts this is the Google servers that run our scripts, and these are in the America/Los_Angeles (Pacific time) zone.

If you want to create a Date object with the time in the timezone of your Google Ads account, you need to to use ' AdsApp.currentAccount().getTimeZone()' .

Here's an example to explain and show the difference:

function main() {

  // this logs the current time in the timezone of Google's server (America/Los_Angeles, Pacific time)

  var currentTime = new Date();
  Logger.log('The current timeat Google server is: ' + currentTime);  
 
  // and this this logs the time using your Google Ads account timezone
  var currentTimeAccount = getAccountCurrentDateTime();
  Logger.log('The current account time is: ' + currentTimeAccount);
}

//this function shows how to get the time in the timezone of the google ads account  
function getAccountCurrentDateTime() {
  Logger.log('The account time zone is: ' +  AdsApp.currentAccount().getTimeZone());
  return new Date(Utilities.formatDate(new Date(), AdsApp.currentAccount().getTimeZone(), "MMM dd,yyyy HH:mm:ss"));
}



Hope this helps,

Nils Rooijmans
https://nilsrooijmans.com
See my Google Ads Scripts FAQ to avoid the same mistakes I made: https://nilsrooijmans.com/google-ads-scripts-faq/

Google Ads Scripts Forum Advisor

unread,
Aug 24, 2023, 7:52:28 AM8/24/23
to adwords...@googlegroups.com

Hi All,

 

@Nils - Thank you for your insights on this.

 

You can try and follow Nils' approach and let us know how it goes on your end. If you encounter any errors or have any clarifications, just let us know and provide your Google Ads account ID as well as the name of the script in the account so we're able to further investigate.

ECohen

unread,
Aug 24, 2023, 8:58:20 AM8/24/23
to Google Ads Scripts Forum
Thank you so much for this clarification. @nils your code really made it clear for me! That was a huge help. Problem solved.
Reply all
Reply to author
Forward
0 new messages