AdWords
5.6K members online now
5.6K members online now
Use AdWords conversion tracking and reporting to measure your results. Have a question about Google Analytics? Post it here, on the Google Analytics Community!
Guide Me
star_border
Reply

How to get current date and time with Adwords Script

Visitor ✭ ✭ ✭
# 1
Visitor ✭ ✭ ✭

Hi,

 

I'm trying to implement the new date script to get current date and time.
However, when I preview the script,
it shows the current date and time in California though I am in Japan.

 

My script is:

 

function main() {
var today = new Date();
Logger.log(today);
}

 

I thought the "new Date" script works on Adwords Script as Java script does,
but it didn't.

 

How do I supposed to do to get current date and time in Japan.

 

Thanks,

Maho

2 Expert replyverified_user
Marked as Best Answer.
Solution
Accepted by AdWiser (Top Contributor)
September 2015

Re: How to get current date and time with Adwords Script

Top Contributor
# 2
Top Contributor

Hi Maho, this should work:

 

function main() {
var today=new Date();

//convert to millseconds

var localTime=today.getTime();

//apply offset between California & Japan in milliseconds (17 hours)

var offsetTime=localTime+61200000;

//Convert to date again
var japanTime=new Date(offsetTime);
Logger.log(japanTime.toLocaleString())
}

 

Jon

 

AdWords Top Contributor Google+ Profile | Partner Profile | AdWords Audits
Marked as Best Answer.
Solution
Accepted by topic author Maho
September 2015

Re: How to get current date and time with Adwords Script

Top Contributor
# 3
Top Contributor

Jon's solution should do the trick, if I remember correctly the time and date for adwords script is California time, regardless of the account settings.

Calin Sandici, AdWords Top Contributor | Find me on: Google+ | Twitter | LinkedIn | myBlog
Was my response helpful? If yes, please mark it as the ‘Best Answer.’ Learn how here.

Re: How to get current date and time with Adwords Script

[ Edited ]
Visitor ✭ ✭ ✭
# 4
Visitor ✭ ✭ ✭

Hi Jon and Calin,

Thank you so much for your help!
I'll try it Smiley Happy

Maho

Re: How to get current date and time with Adwords Script

Visitor ✭ ✭ ✭
# 5
Visitor ✭ ✭ ✭
Japan doesn't observe DST, so it might be better to have an extra step and convert to UTC first.

Re: How to get current date and time with Adwords Script

Visitor ✭ ✭ ✭
# 6
Visitor ✭ ✭ ✭

Very helpful. Thank you.

Re: How to get current date and time with Adwords Script

Visitor ✭ ✭ ✭
# 7
Visitor ✭ ✭ ✭
I had the same question and Jon_Gritton's approach worked fine for me. I later found out another approach that I prefer:

- you can use the utilities class of Google Apps in AdWords script, it contains the method Utilities.formatDate, see https://goo.gl/WbxgZe
- if you use the timezone of your AdWords account, you don't have to bother about calculating the offset: AdWordsApp.currentAccount().getTimeZone()

So your're script could be looking something like this:

function main() {
var today = new Date();
Logger.log(Utilities.formatDate(today, AdWordsApp.currentAccount().getTimeZone(), "yyyy-MM-dd' 'HH:mm:ss' 'zzzz' ('XXX')'"));
}

For formatting options, see http://goo.gl/Lg2O2y