Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
570 views
in Technique[技术] by (71.8m points)

timezone - Google App Script - UK Format of Date and British Summer Time

I am new to Google App Script (and Javascript) and am having troubles with constructing dates to put into a Google Calendar event. Moreover the handling of British Summer Time is causing a problem.

In the UK dates are written in the format dd/MM/yyyy. So the spreadsheet from which I read the date has them in this format.

07/01/2021 (which means 7th January 2021, not 1st July 2021)

So reading this into Google App Script using

var 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)

Provided the UK is on GMT (half of the year) this works fine when putting in an event. However when in British Summer Time, the event is out by an hour when created. How do I input a timezone or create a date which is handled correctly for GMT or BST. And is there a better way of doing what I have done so far?

Many thanks.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Hi and thanks for your responses.

I have kind of answered my own question. Essentially British Summer Time lasts from the last Sunday in March to the last Sunday in October (some people refer to it as Daylight Saving Time). During this time BST = GMT+1.

So I have calculated these dates for the next 30 years or so and essentially said that one of my event dates lies between them in a given year apply GMT+1. This seems to work. Its a bit "hardwired" for my tastes but is easy to encode.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...