Kon*_*ner 7 timezone datetime google-sheets
我想计算Google表格中不同时区的日期时间之间的时差.
如果我将两个字段格式化为"datetime"而将另一个字段格式化为"duration",我可以使用差异运算符成功计算同一时区内的差异.
示例: A1 = 1/10/2016 10:10:00,B2 = 13/11/2016 15:35:00C2 = =B2-B1.
但是,当我将时区添加到日期时间时,例如A1 = 1/10/2016 10:10:00 GMT+1:00:00,C2显示#VALUE.
我知道我可以自己计算时区差异并从持续时间中减去它,但有没有办法通过直接在datetime字段中指定时区来实现自动化?
使用自定义功能.
Google表格公式和日期格式不包含时区处理程序,因此如果您包含时区符号,则会将该值视为字符串而不是日期时间.
另一种方法是使用Google Apps脚本/ JavaScript编写一个功能,将包含时区的日期时间字符串转换为Google表格日期.
/**
* Converts a string to a date-time value
*
* @param {Thu Apr 23 2015 00:00:00 GMT+0200} dateTimeString
* @customfunction
*/
function stringToDateTime(dateTimeString) {
var output = new Date (dateTimeString);
if(!isNaN(output.valueOf())) {
return output;
} else {
throw new Error('Not a valid date-time');
}
}
Run Code Online (Sandbox Code Playgroud)
A1 = 1/10/2016 10:10:00 GMT+0100
注意:JavaScript Date Object必须支持日期时间格式
=B1-stringToDateTime(A1)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8603 次 |
| 最近记录: |