简体   繁体   English

在Google App脚本中设置当前时间

[英]Format current time in Google App Scripts

How can you set the current time with google app scripts? 如何使用Google App脚本设置当前时间? 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. 我现在想将此时间设置为上午11:56。 I'm using the Date documentation . 我正在使用Date 文档 I've tried using toLocaleTimeString() from javascript but it is not defined in app scripts 我尝试从JavaScript使用toLocaleTimeString() ,但未在应用脚本中定义

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) 要格式化日期,您需要的是Utilities.formatDate()函数,该函数在此处记录: 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. 用适当的值替换“您的日期”和“您的时区”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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