我想计算Google表格中不同时区的日期时间之间的时差。
如果我将两个字段的格式设置为“日期时间”,将另一个字段的格式设置为“持续时间”,则可以使用差异运算符在同一时区内成功计算出差异。
示例: A1 = 1/10/2016 10:10:00
,B2 = 13/11/2016 15:35:00
C2 = =B2-B1
。
但是,当我将时区添加到日期时间时,例如A1 = 1/10/2016 10:10:00 GMT+1:00:00
,C2显示#VALUE
。
我知道我可以自己计算时区差并从持续时间中减去时差,但是有没有办法通过直接在datetime字段中指定时区来自动做到这一点?
最佳答案
简短答案
使用custom function。
解释
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');
}
}
A1 =
1/10/2016 10:10:00 GMT+0100
注意:JavaScript Date Object必须支持日期时间格式
=B1-stringToDateTime(A1)
引用
关于Google表格中带有时区的日期时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39719851/