我是 Google App Script(和 Javascript)的新手,并且在构建日期以放入 Google 日历事件时遇到了麻烦。此外,英国夏令时的处理也造成了问题。
在英国,日期的格式为 dd/MM/yyyy。因此,我从中读取日期的电子表格采用这种格式。
07/01/2021(即 2021 年 1 月 7 日,而不是 2021 年 7 月 1 日)
因此使用
将其读入 Google App Scriptvar data = sheet.getDataRange().getValues()
// The first part of the code gets the date from the spreadsheet and converts to MM/dd/yyyy format
timeofevent = data[0][0]
var newtime = Utilities.formatDate(new Date(timeofevent), calendar.getTimeZone(), "MM/dd/yyyy")
var newstart = newtime + " 15:00:00 GMT" // my event always runs from 3pm til 4pm on given date
var newend = newtime + " 16:00:00 GMT"
// Then I create a date object of these times and pass to create event with options
var nstart = new Date(newstart)
var nend = new Date(newend)
calendar.createEvent("Test",nstart, nend, options)
如果英国处于格林威治标准时间(半年),这在举办活动时效果很好。但是,在英国夏令时,事件在创建后一小时后结束。如何输入时区或创建为 GMT 或 BST 正确处理的日期。有没有更好的方法来做我迄今为止所做的事情?
非常感谢。
答案 0 :(得分:0)
您好,感谢您的回复。
我已经回答了我自己的问题。基本上英国夏令时从 3 月的最后一个星期日持续到 10 月的最后一个星期日(有些人将其称为夏令时)。在此期间 BST = GMT+1。
所以我计算了未来 30 年左右的这些日期,基本上是说我的一个事件日期在给定年份中位于它们之间,应用 GMT+1。这似乎有效。根据我的口味,它有点“硬连线”,但很容易编码。