Google Script Date Showing wrong “hours”

别等时光非礼了梦想. 提交于 2019-12-11 15:12:10

问题


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

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!


回答1:


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...

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...

Edit following your comment :

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");


来源:https://stackoverflow.com/questions/15395848/google-script-date-showing-wrong-hours

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!