Reputation: 3198
The script timezone is set to Etc/GMT
I am accepting a date (using a datetime-local yyyy-MM-ddTHH:mm) from user to invoke a trigger function.
var at = moment(mydate); // say user sets mydate (local IST) as 2017-06-21T14:31
ScriptApp.newTrigger('onClock').timeBased().at(new Date(at)).inTimezone(CalendarApp.getDefaultCalendar().getTimeZone()).create();
this will invoke the trigger at 2017-06-21 20:01 local time IST
How do I make a code to invoke trigger at 2017-06-21T14:31 local time?
Upvotes: 1
Views: 501
Reputation: 31300
The inTimezone(timezone)
method only accepts valid "long format" JODA time zones.
You are already getting the default calendar time zone:
CalendarApp.getDefaultCalendar().getTimeZone()
which returns the "long format" JODA type time zone, and that is what the inTimezone()
method needs as it's parameter.
So, you don't need to get the 2017-06-21T14:31
information from the user, UNLESS their default calendar time zone is NOT the same as their local time. If your situation, is that there is a difference between the default calendar time zone, and their local time on their computer; and you want to account for that difference, then you do need to get the local time zone.
If you really do need to get a time zone that is different from the default calendar time zone, then either the user needs to select a time zone, or you can try to get it some other way.
In your question, you stated that the time you got is: 2017-06-21T14:31
That local time 2017-06-21T14:31
doesn't have time zone information in it. If it had the offset, then you could use the offset to work back to the "long format" time zone.
You could have a drop down list for the user to choose from, with values that are valid "long format" JODA time zones.
<select>
<option value="Etc/GMT+12">GMT +12</option>
<option value="Etc/GMT+11">GMT +11</option>
</select>
If you really do need to get the users local time zone from client side code, see the following Stack Overflow answer:
Stack Overflow answer - Getting the client's timezone in JavaScript
var offset = new Date().getTimezoneOffset();
console.log(offset);
Then you will need to convert the offset to a value "long format" time zone. You can do that by having a list "hard coded" into your code, and do a "look up" or construct a "Etc/GMT+##" time zone with code. The offset is the opposite of the GMT number. A standard offset of minus 12 is +12 GMT
-12:00 --- Etc/GMT+12
Upvotes: 2