Google脚本日期显示错误的“小时”

时间:2013-03-13 20:30:00

标签: javascript google-apps-script dst timezone-offset

我觉得好像我已经倾倒了几乎所有关于JS和GAS日期的文档。特别是关于DST问题。因为在DST改变之前我没有这样的问题。我想我错过了一些简单的事情。问题是尽管搞乱了所有设置,但格式实用程序输出日期的时间仍然不正确。下面是调试器的屏幕截图和我正在运行的代码片段。 值'date'是我关注的值。它在第53行的脚本中创建。

var date = new Date(); //line 53
date = Utilities.formatDate(date, "Chicago/America", "MM/dd/yyyy HH:mm:ss zzzz"); //line 61

我的总部设在KC。 (中部时间或UTC-06:00或其他)。

我有一种感觉,这对我来说是一个面孔的时刻,但哦,好吧! 感谢您的帮助!

Debugging output

1 个答案:

答案 0 :(得分:3)

我通常使用的没有任何问题只是:

  var FUS1=new Date(dateObject).toString().substr(25,6)+":00";// get the timeZone part of the string coming from the date you are working on (an event)
  dateString = Utilities.formatDate(dateObject, FUS1, "MM/dd/yyyy HH:mm:ss");// use it in formatDate and it will have the right value for every season...

请注意,您在示例代码中使用date次2次:一次作为字符串结果,一次作为同一行中的日期对象...

编辑以下评论:

假设例如日期戳在A2中,以下代码应返回正确的时间。

  var date = SpreadsheetApp.getActiveSheet().getRange('A2').getValue();
  var FUS1=new Date(date).toString().substr(25,6)+":00";
  dateString = Utilities.formatDate(date, FUS1, "MM/dd/yyyy HH:mm:ss");