我知道这看起来很简单。
在Google电子表格中,我有一列在一个时区(GMT)输入时间,另一个列在另一个时区(太平洋时间)自动获取时间。
GMT | PT
----------|------------
5:00 AM | 9:00 PM到目前为止我正在使用
=$C$3-time(8,0,0)问题是,我想改变日光节约的时间公式。
是否有任何可用的函数或脚本可以将夏时制自动计算。
发布于 2016-10-29 23:14:57
简短回答
没有内置函数,但可以构建自定义函数。
示例
/**
* Converts a datetime string to a datetime string in a targe timezone.
*
*@param {"October 29, 2016 1:00 PM CDT"} datetimeString Date, time and timezone.
*@param {"Timezone"} timeZone Target timezone
*@param {"YYYY-MM-dd hh:mm a z"} Datetime format
*@customfunction
*/
function formatDate(datetimeString,timeZone,format) {
var moment = new Date(datetimeString);
if(moment instanceof Date && !isNaN(moment)){
return Utilities.formatDate(moment, timeZone, format)
} else {
throw 'datetimeString can not be parsed as a JavaScript Date object'
}
}注意:
Google脚本中的new Date(string) / Date.parse(string)实现不支持某些时区缩写。
来自https://tc39.es/ecma262/#sec-date-time-string-format
目前还没有国际标准来指定诸如CET、EST等民用时区的缩写,有时甚至对两个非常不同的时区使用相同的缩写。
相关
解释
为了考虑夏时区,要转换的值的输入参数应该包括日期,而不仅仅是一天中的时间。在调用公式之前,可以将默认日期和时区设置为构建datetimeString。
=formatDate("October 29, 2016 "&A2&" GMT","PDT","hh:mm a")对于目标时区,除了三个字母缩写外,我们还可以使用TZ数据库名 (比如America/Los_Angeles ),例如:
=formatDate("October 29, 2016 "&A2&" GMT","America/Los_Angeles","HH:mm")如果datetimeString的时区缩写和TZ名称失败,则使用时间偏移量(即UTC-4)。
另请参阅
参考文献
发布于 2019-03-06 15:58:19
本教程非常有用:https://davidkeen.com/blog/2017/01/time-zone-conversion-in-google-sheets/
Google不具有转换时区数据的内置方式,但是通过使用Moment.js和Google脚本编辑器的功能,我们可以将时区功能添加到任何工作表中。
以下是我复制到我的脚本项目中的文件:
确保您首先添加了moment.js脚本,并将其置于矩-timezone.js脚本之上,因为time-timezone.js取决于它。
然后在您的另一个脚本项目中,您的Code.gs文件可以如下所示:
var DT_FORMAT = 'YYYY-MM-DD HH:mm:ss';
/**
https://stackoverflow.com/questions/34946815/timezone-conversion-in-a-google-spreadsheet/40324587
*/
function toUtc(dateTime, timeZone) {
var from = m.moment.tz(dateTime, DT_FORMAT, timeZone);//https://momentjs.com/timezone/docs/#/using-timezones/parsing-in-zone/
return from.utc().format(DT_FORMAT);
}
/**
https://stackoverflow.com/questions/34946815/timezone-conversion-in-a-google-spreadsheet/40324587
*/
function fromUtc(dateTime, timeZone) {
var from = m.moment.utc(dateTime, DT_FORMAT);//https://momentjs.com/timezone/docs/#/using-timezones/parsing-in-zone/
return from.tz(timeZone).format(DT_FORMAT);
}

https://stackoverflow.com/questions/34946815
复制相似问题