Getting the wrong date from the date function

1,779 views
Skip to first unread message

Merijn P.

unread,
Jan 11, 2017, 2:55:03 AM1/11/17
to AdWords Scripts Forum
Hi, I am getting the wrong day returned when i run my script in the morning. Just now it returns 10/01/2017 instead of 11/01/2017. Could this be because of a bug or a wrong timezone setting? My timezone in Adwords is set correctly.

Here is the code:

DateToday() {
  var today = new Date();
  var dd = today.getDate();
  var mm = today.getMonth()+1;
  var yyyy = today.getFullYear();
  
  if(dd<10) {
    dd='0'+dd
  } 
  
  if(mm<10) {
    mm='0'+mm
  } 
  
  today = dd+'/'+mm+'/'+yyyy;
  return today;
}


Paul Justine de Honor

unread,
Jan 11, 2017, 4:02:05 AM1/11/17
to AdWords Scripts Forum
Hi Merijn,

Could you provide the CID and the name of the script (reply privately to author) so we can further investigate?

Thanks,
Paul Justine De Honor
AdWords Scripts Team

Stephen Howe

unread,
Jan 11, 2017, 5:44:31 AM1/11/17
to AdWords Scripts Forum
Hey Merijn,

You can check what timezone the script is running in by logging an instance of the date class:

var today = new Date();
Logger.log(today);
// Mine prints out
Wed Jan 11 02:40:14 GMT-08:00 2017

This timezone is not the same as the one set by my account (GMT+4). If I recall correctly, offsetting timezones in JS is fiddly and requires some date formatting.
Hope this helps.

Stephen

Merijn P.

unread,
Jan 11, 2017, 10:15:19 AM1/11/17
to AdWords Scripts Forum
Thanks for your reply! It seems to indeed be the case that my time setting was GMT-8, while that is not my timezone. I fixed the issue by changing .getDate() to .getUTCDate() like you can see below. Thanks a lot!

DateToday() {
 var today = new Date();
 var dd = today.getUTCDate();

Stephen Howe

unread,
Jan 11, 2017, 10:47:32 AM1/11/17
to AdWords Scripts Forum
Great! Glad you found a simple fix.

Paul Justine de Honor

unread,
Jan 11, 2017, 8:54:13 PM1/11/17
to AdWords Scripts Forum
Thank you Stephen!

Hi Merijn,

Glad to hear that you solved the issue. Please let us know if you have other concerns and we will be happy to assist you.

Thanks,
Paul Justine De Honor
AdWords Scripts Team

On Wednesday, January 11, 2017 at 3:55:03 PM UTC+8, Merijn P. wrote:
Reply all
Reply to author
Forward
0 new messages