4

I want to create a Calendar event using the same timezone as the calendar.

I already have the year, month and day as numbers, in separate variables. How can I construct a Date object using these values, in a specific timezone?

var day = 31;
var month = 11;  // Month is zero-based
var year = 2014;
var timezone = calendar.getTimeZone();

// How to add the timezone here?
var date = new Date(year, month, day, 12, 0, 0);

Essentially, I ask this because of the documentation:

If no time zone is specified, the time values are interpreted in the context of the script's time zone, which may be different than the calendar's time zone.

Thus I wish to know how to correctly specify the timezone.


Relevant blog post (although it doesn't answer my question):

  • Demystifying Time Zones in Apps Script - part 1 and part 2
KyleMit
  • 30,350
  • 66
  • 462
  • 664
Denilson Sá Maia
  • 47,466
  • 33
  • 109
  • 111

3 Answers3

3

You can get the timezone of the script and then load it into the parameters.

var timeZone = Session.getScriptTimeZone();
var timeStamp = Utilities.formatDate(new Date(), timeZone, "dd-MM-yyyy | HH:mm:ss");

Now you can use the timeStamp in your code.

Neo
  • 755
  • 1
  • 10
  • 24
  • Note: the script time zone can be changed. It's isn't just random for which server you happen to be hitting! https://developers.google.com/apps-script/reference/base/session?hl=en#getScriptTimeZone() – Simon_Weaver Dec 22 '20 at 23:51
  • 1
    Although that points you in the wrong direction. You can change it in `appsscript.json` under `timeZone` https://developers.google.com/apps-script/manifest – Simon_Weaver Dec 22 '20 at 23:55
  • 1
    @Simon_Weaver thank you - this is the best answer to the question. – planetClaire Mar 14 '21 at 00:54
0

From the documentation you referred to, about the method createEvent,, they show an example that creates an event on July 20 , 1969 , they create the date like this:

new Date('July 20, 1969 20:00:00 UTC')

You could do it like that, replace UTC with the time zone you want. Use the official time zone name instead of GMT+X so the daylight saving will be automatically calculated. (Didn't test recently but it used to work - I have no computer right now to test this before posting, sorry )

You can get the calendar tz in the script using cal.getTimeZone where cal is a calendar object.

I assumed that you wanted to keep your script settings in a tz different from the tz of the calendar? Otherwise it is obviously simpler to set all tz on the same value.

Serge insas
  • 45,904
  • 7
  • 105
  • 131
  • So, I need to convert my numeric month to English, then concatenate everything into a string. This can probably work, although I kinda expected some kind of API to set/change the timezone directly, without resorting to string concatenation and parsing. I'll test later. – Denilson Sá Maia Aug 08 '14 at 19:53
  • Just a question: do you want to be able to enter time in local time value or create events at some absolute time as needed for example to settle an appointment between 2 people in different tz ? In other words how do you want it to work? Create an event at 12:00 in Tokyo for example or create an event at whatever hour it will be in Tokyo when its 12:00 here...? Edit: did you see that post? http://stackoverflow.com/questions/439630/how-do-you-create-a-javascript-date-object-with-a-set-timezone-without-using-a-s?rq=1 – Serge insas Aug 08 '14 at 20:11
  • Essentially, I want to have some control over the timezone, to make sure the event I create is in the correct timezone. (but, right now, a single event fora single person) – Denilson Sá Maia Aug 11 '14 at 04:13
-1

Not sure if this is what you're looking for.

The code below takes into consideration the calendar timezone and the Session / Script timezone, and adjusts the offset.

var day = 31;
var month = 11;  // Month is zero-based
var year = 2014;
var timezone = calendar.getTimeZone();

// How to add the timezone here?
var date = new Date(year, month, day, 12, 0, 0);

// get the timezones of the calendar and the session
var calOffset = Utilities.formatDate(new Date(), timezone, "Z");
var scriptOffset = Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "Z");

// remove the 0s
var re = /0/gi;
calOffset = parseInt(calOffset.replace(re,''));
scriptOffset = parseInt(scriptOffset.replace(re,''));

var offsetDif =   calOffset - scriptOffset;
var date = new Date();
// set the offset difference between the Session / Script and the calendar
date.setHours(date.getHours() +offsetDif);
KyleMit
  • 30,350
  • 66
  • 462
  • 664
Cyrus Loree
  • 837
  • 6
  • 7
  • This gives wrong results because when you remove the zeros form offset values, the ```offsetDif``` is calculated incorrectly – PriyankaJ Dec 01 '22 at 07:14