Google Apps Script formatDate using user#39;s time zone instead of GMT(Google Apps 脚本 formatDate 使用用户的时区而不是 GMT)
问题描述
我有一行在单元格中设置当前日期和时间:
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");
生成的 addedTime 似乎在 GMT 时区,我需要它在用户的时区.它通常是美国东部时间 (-0500),但如果我只是从时间中减去 5 小时,那么这并没有考虑夏令时 (-0400).
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).
我在这里查看了 formatDate 的文档:https://developers.google.com/apps-script/reference/utilities/utilities#formatDate(Date,String,String)
I checked the documentation for formatDate here: https://developers.google.com/apps-script/reference/utilities/utilities#formatDate(Date,String,String)
此处链接到官方 Java SimpleDateFormat 类文档:http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
which links to the official Java SimpleDateFormat class documentation here: http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
...但我无法找到有效时区列表来替换 GMT.
... 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");
在此处查看文档
但如果您想自己选择,Google Apps 脚本会使用这些格式:
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Google Apps 脚本 formatDate 使用用户的时区而不是 GMT
 
				
         
 
            
        基础教程推荐
- 每次设置弹出窗口的焦点 2022-01-01
- 什么是不使用 jQuery 的经验技术原因? 2022-01-01
- WatchKit 支持 html 吗?有没有像 UIWebview 这样的控制器? 2022-01-01
- Node.js 有没有好的索引/搜索引擎? 2022-01-01
- 如何使用sencha Touch2在单页中显示列表和其他标签 2022-01-01
- 如何在特定日期之前获取消息? 2022-01-01
- 为什么我在 Vue.js 中得到 ERR_CONNECTION_TIMED_OUT? 2022-01-01
- 如何使用 CSS 显示和隐藏 div? 2022-01-01
- Javascript 在多个元素上单击事件侦听器并获取目标 2022-01-01
- jQuery File Upload - 如何识别所有文件何时上传 2022-01-01
 
    	 
    	 
    	 
    	 
    	 
    	 
    	 
    	 
						 
						 
						 
						 
						 
				 
				 
				 
				