I'm trying to create an event for another user which needs to appear on their Calendar (in that user's Timezone).

e.g. Current user's Timezone is Australia/Sydney, I need to create an event for a New Zealand user with timezone Pacific/Auckland.

When you simply create an event below it's getting saved in current user's Timezone.

DateTime eventStart = Datetime.newInstance(2019, 11, 14, 10, 0, 0);
DateTime eventEnd = Datetime.newInstance(2019, 11, 14, 10, 30, 0);

    insert new Event(OwnerId = userIdwithDifferentTimezone,StartDateTime = eventStart, EndDateTime = eventEnd, Subject = subject);

I tried to assign Timezone by converting DateTime to that Timezone and add suffix as below,

DateTime dt = eventStart;
String targetTZ = dt.format('Z',tzName);    // Get TZ offset for target

// Set the timezone
String dt_str = '"'+dt.year()+'-'+dt.month()+'-'+dt.day()+'T'+dt.hour()+':'+dt.minute()+':'+dt.second()+'.000'+targetTZ+'"' ;

eventStart = (Datetime)JSON.deserialize(dt_str, Datetime.class);

But it seems this does not setting the Timezone, when DateTime object is initialized, seems it's assigning current user's Timezone instead.

What is the correct way to set Timezone in Event?

Appreciate any help, thoughts to correct this. Thank you!

  • How is eventStart being initialized? – Glen De Marcos Nov 12 '19 at 2:21
  • Hi @GlenDeMarcos updated question. cheers! – Pasan Eeriyagama Nov 12 '19 at 3:09

It's not the neatest thing, but you could used the format() method of the DateTime class to convert the DateTime to a string with an adjusted timezone, and then convert it back to DateTime. More information about the format() method can be found here.

String timeFormat = 'yyyy-MM-dd HH:mm:ss';
String timeZone = 'Pacific/Auckland';

DateTime convertedStart = DateTime.valueOf(eventStart.format(timeFormat, timeZone));
DateTime convertedEnd = DateTime.valueOf(eventStart.format(timeFormat, timeZone));;

insert new Event(
    OwnerId = userIdwithDifferentTimezone, 
    StartDateTime = convertedStart, 
    EndDateTime = convertedEnd, 
    Subject = subject
  • Hi @Nicholas, Thanks, I tried it before. This also seems to create Event in current User's timezone. At the time when Datetime object is initialized it assigns current user's Timezone. But when you format it to a different timezone string representation seems to be in that user's timezone. and once it's back to Datetime instance using Datetime.ValueOf() or Deserialize like I did, it seems to be in current user's timezone. – Pasan Eeriyagama Nov 12 '19 at 1:58
  • Interesting. I tested it myself and found that it worked as I would have expected it to. I set the start time to be 2019-11-12 07:00:00 in EST, but when I ran the above code it created the event with a start time of 2019-11-12 04:00:00. – Nicholas Savini Nov 12 '19 at 2:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.