Google Apps脚本中的时区 [英] Time zones in Google Apps Script

查看:114
本文介绍了Google Apps脚本中的时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将日期对象转换为与Google电子表格中使用的日期的基础值一致的序列号。我的理解是,在Javascript中,日期对象的原始值是1970年1月1日午夜之后(或之前)的毫秒数;在Google Spreadsheets中,日期的原始值在1899年12月30日午夜为0(零),此后每天等于1(例如,7小时等于7除以24)。



我试过这个函数:

  function date2Serial(date){
return(date.getTime()+ 2209161600000)/ 86400000;
// 2209161600000 = 1899-12-30和1970-01-01之间的毫秒数
// 86400000 =一天中的毫秒数
//问题在侧面 - 有没有必要
// .getTime()和.valueOf()之间的区别?
}

这个返回值有点小 - 我认为关符合我与GMT的时区差异。因此,即使电子表格设置和项目属性在我的情况下设置为GMT + 10,似乎getTime()运行在GMT。



例如:

  A1:16/09/2012 06:00:00 
A2:= date2serial(A1)
A3:= VALUE(A1)



返回41167.8333,其中A3返回41168.25。



现在我尝试使用 getTimezoneOffset()方法,它似乎解决了这个问题:

  function date2Serial(date){
return(date.getTime()+ 2209161600000 - (date.getTimezoneOffset()* 60000))/ 86400000;
}

然而这依赖于项目属性时区是正确的,而不是电子表格设置时区(实际上可以设置其中的每一个)。



我的问题:

预期这种行为,并且时区的调整是必要的?或者,getTime()(以及所有其他方法(如getHours(),getMinutes()等))在用户设置的时区而不是GMT / UTC上运行,而调整实际上是解决错误的方法?

解决方案

getTime()返回一个持续时间,而不是时间,因此没有时区。减去参考时刻是1970年1月1日午夜(UTC)。

像getHours(),getMinutes()等方法给出了时间的一部分。结果取决于所使用的日期对象的时区。如果你想独立于日期对象的时区,你应该使用getUTCHours(),getUTCMinutes()等。


I want to convert a date object into a "serial number" that is consistent with the underlying value of dates used in Google Spreadsheets. My understanding is that in Javascript the primitive value of a date object is the number of milliseconds after (or before) midnight on 1st Jan, 1970; in Google Spreadsheets, the primitive value of a date is 0 (zero) for midnight on 30th Dec, 1899, and each day thereafter equals 1 (so, for example, 7 hours is equal to 7 divided by 24).

I tried this function:

function date2Serial(date) {
  return (date.getTime() + 2209161600000) / 86400000;
  // 2209161600000 = milliseconds between 1899-12-30 and 1970-01-01
  // 86400000 = milliseconds in a day
  // question "on the side" - is there any essential difference between
  //  .getTime() and .valueOf()?
}

And this returns a value that is a bit off - and I think the "off" corresponds with my time zone difference from GMT. So even though both the Spreadsheet Settings and Project Properties are set to GMT+10 in my case, it seems that getTime() operates on GMT.

So for example:

A1: 16/09/2012 06:00:00
A2: =date2serial(A1)
A3: =VALUE(A1)

A2 returns 41167.8333, where A3 returns 41168.25.

Now I tried using the getTimezoneOffset() method, which appeared to correct the issue:

function date2Serial(date) {
  return (date.getTime() + 2209161600000 - (date.getTimezoneOffset() * 60000)) / 86400000;
}

However this relies on the Project Properties time zone being correct, as opposed to the Spreadsheet Settings time zone (it is actually possible to set each of these differently).

My question:

Is this behaviour expected, and the "adjustment" for time zone necessary? Or should the getTime() (and all the other methods like getHours(), getMinutes() and so on) operate on the user's set time zone rather than GMT/UTC, and the "adjustment" is actually a workaround for a bug?

解决方案

getTime() returns a time duration, not a moment in time and therefore does not have a time zone. The reference moment to substract is midnight of January 1, 1970 (UTC).

Methods like getHours(), getMinutes() and so on give part of a moment in time. The result is dependent on the time zone of the date object used. If you want to be independent of the date objects' time zone you should use getUTCHours(), getUTCMinutes() and so on.

这篇关于Google Apps脚本中的时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆