Google脚本日期显示错误的“小时” [英] Google Script Date Showing wrong "hours"
问题描述
我觉得我似乎已经遍历了几乎所有有关JS和GAS中日期的文档。特别是关于DST问题。因为直到DST更改,我才没有出现这样的问题。我想我缺少一些简单的东西。问题是,尽管将所有设置弄乱了,但从格式实用程序输出的日期上的小时数仍然不正确。下面是调试器的屏幕截图和我正在运行的代码的摘录。
日期值是我关注的值。它将在脚本的第53行中创建。
I feel as if I have poured over nearly all of the documentation regarding dates in JS and GAS. Especially concerning DST issues. Since I have not had a problem like this until the DST change. I think I am missing something simple. The problem is that despite messing with all of the settings the hours on my date output from the format utility are still incorrect. Below is a screen shot of the debugger and a snippet of the code i am running. The value 'date' is the one that I am concerned with. It is created within the script on line 53.
var date = new Date(); //line 53
date = Utilities.formatDate(date, "Chicago/America", "MM/dd/yyyy HH:mm:ss zzzz"); //line 61
我住在KC。 (Central Time或UTC-06:00或其他)。
I am based in KC. (Central Time or UTC-06:00 or whatever).
我感觉这对我来说将是一个面目全非的时刻,但是哦!
感谢您的所有帮助!
I have a feeling this is going to be a face palm moment for me but oh well! Thank you for any and all help!
推荐答案
我通常没有任何问题的简单用法是:
what I usually use without any issue is simply that :
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
在您的示例代码中两次:一次作为字符串结果,一次作为同一行中的日期对象...
note that you were using date
2 times in your example code :once as a string result and once as a date object in the same line...
在评论后进行编辑:
假设日期戳在A2中,以下代码应返回正确的时间。
assuming for example that the datestamp is in A2, the following code should return the right time.
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");
这篇关于Google脚本日期显示错误的“小时”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!