ics icon indicating copy to clipboard operation
ics copied to clipboard

How to use timezone

Open caique0202 opened this issue 6 years ago • 2 comments

How to add the TZID= to the time/date?

Thanks

caique0202 avatar Nov 27 '19 06:11 caique0202

See my solution.

Let's use an example: I have an event that is stored in my database and that has date/time without timezone, but I know it's tied to a location. For example, my event happens in New York City on Feb 14, 2020 from 9 AM to 10 AM. I'm in France and when I open the .ics file in my Outlook Calendar I should see this event from 5 PM to 6 PM (because I'm 6 hours ahead) in my calendar.

To achieve that, I did the below…

First, I installed moment-timezone. Then my code is:

let strStartTime = "2020-02-14 9:00:00";
let strEndTime = "2020-02-14 10:00:00";

// parse with NYC time, and then convert to UTC
let startTime = moment.tz(strStartTime, "America/New_York").utc();
let endTime = moment.tz(strEndTime, "America/New_York").utc();

// convert dates to array
startTime = [ startTime.get('year'), startTime.get('month')+1, startTime.get('date'), startTime.get('hour'), startTime.get('minute') ];
endTime = [ endTime.get('year'), endTime.get('month')+1, endTime.get('date'), endTime.get('hour'), endTime.get('minute') ];

// create event
createEvent({
  title: "Event Title",
  description: "Event Description",
  location: "Room 1, New York City",
  busyStatus: 'BUSY',
  start: startTime,
  startInputType: 'utc', // I define the start time as UTC
  end: endTime,
  endInputType: 'utc', // I define the start time as UTC
}, function(error, value) { […do something…] })

I hope it will help some others!

Aymkdn avatar Feb 14 '20 16:02 Aymkdn

Thanks for the example! It should definitely go into the readme

julientaq avatar May 17 '20 00:05 julientaq