简体   繁体   中英

Format current time in Google App Scripts

How can you set the current time with google app scripts? I'm having a tough time finding the Time documentation.

I'm able to get the time in milliseconds using.

(new Date()).getTime()

I'd like to now format this time as 11:56 AM. I'm using the Date documentation . I've tried using toLocaleTimeString() from javascript but it is not defined in app scripts

For formatting a date, what you want is the Utilities.formatDate() function, documented here: https://developers.google.com/apps-script/reference/utilities/utilities#formatDate(Date,String,String)

Usage to get the time only would be:

Utilities.formatDate(YOUR DATE,YOUR TIMEZONE, 'HH:mm')

replacing YOUR DATE and YOUR TIMEZONE with the appropriate values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM