Apps脚本formatDate使用用户的时区而不是GMT

Apps脚本formatDate使用用户的时区而不是GMT

本文介绍了Google Apps脚本formatDate使用用户的时区而不是GMT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  sheet.getRange(1,1)这是一行代码,用于设置单元格中的当前日期和时间。 .setValue(new Date()); 

然后我有一行根据单元格的值创建一个格式化的日期:

  var addedDate = sheet.getRange(1,1).getValue(); 
var addedTime = Utilities.formatDate(addedDate,GMT,hh:mm a);

生成的加入时间似乎在GMT时区,我需要它在用户的时间区。它通常是美国东部时间(-0500),但如果我简单地从时间减去5小时,那么这不包括夏令时(-0400)。



我在这里查看了formatDate的文档:



链接到官方Java SimpleDateFormat类文档:



...但我找不到有效的时区列表来替换GMT。

解决方案

您可以像这样直接获取电子表格时区:

  var addedDate = sheet.getRange(1,1).getValue(); 
var addedTime = Utilities.formatDate(addedDate,SpreadsheetApp.getActive()。getSpreadsheetTimeZone(),hh:mm a);

请参阅 p

但Google Apps Script使用这些格式if你想自己选择它:




I have a line that sets the current date and time in a cell:

sheet.getRange(1,1).setValue(new Date());

Then I have a line that creates a formatted date based on that cell's value:

var addedDate = sheet.getRange(1,1).getValue();
var addedTime = Utilities.formatDate(addedDate, "GMT", "hh:mm a");

The resulting addedTime seems to be in the GMT time zone and I need it to be in the user's time zone. It will usually be US Eastern Time (-0500), but if I simply subtract 5 hours from the time then that doesn't account for daylight saving time (-0400).

I checked the documentation for formatDate here: https://developers.google.com/apps-script/reference/utilities/utilities#formatDate(Date,String,String)

which links to the official Java SimpleDateFormat class documentation here: http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html

... but I can't find a list of valid time zones there to replace the GMT with.

解决方案

You can directly get the spreadsheet Time Zone like this :

  var addedDate = sheet.getRange(1,1).getValue();
  var addedTime = Utilities.formatDate(addedDate, SpreadsheetApp.getActive().getSpreadsheetTimeZone(), "hh:mm a");

See doc here

but Google Apps Script uses these formats if you want to choose it yourself :

http://joda-time.sourceforge.net/timezones.html

这篇关于Google Apps脚本formatDate使用用户的时区而不是GMT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 10:59