I am having some difficulties with timezones... I have a guest site user who is in GMT time. So, when apex is running, they have GMT+0 timezone. The salesforce user that sends them the link to the site does include a desired timezone, however. For this example, it is 'America/Chicago'.

Now, I grab available hours for the salesforce user that sent the link for the external user to use in the form of '8:00 AM' for example. I convert that to a time string like '1901-01-01 08:00:00' and then do DateTime.valueOf('1901-01-01 08:00:00'), and it becomes 8:00 am on that date due to being in GMT time.

Then, when I come back to my javascript (this occurs in a remote action method that I get this info), I get the datetime of Jan 1, 1901 08:00 GMT-0600, which is central standard time... but we are currently in central daylight time. So all the other dates I am making and dealing with are in CDT while these are in CST...

And, in the controller, when I do Timezone.getTimeZone('America/Chicago') and then to tz.getOffset(availableHours) (where availableHours is the 8:00 am time from above that I made), I get -6 hours instead... It seems to be just defaulting to central standard time.

This becomes a huge annoyance when I pass date times in both CDT, and the available hours, which are in CST to another remote function, then trying to deal with getting them to the same timezone...

Does anyone have any ideas or advice on creating a date time string (like 1901-01-01 08:00:00), then applying the correct daylight savings time timezone offset to it when using a timezone like 'America/Chicago' instead of using standard time?

If I could stop the -6 hour offset from the beginning when I first make those datetimes before they go to my VF page and make it the correct -5 hour offset, I believe my problem will be solved.

  • getOffset is DST aware; make sure you're using the correct date as well as time, and you'll get the correct value. – sfdcfox Sep 21 '17 at 17:50
  • Well, when putting DateTime.valuOf('1901-01-01 08:00:00') then trying to get the timezone offset with an America/Chicago timezone, it gives -6 hour offset. If I get the offset of the current time with DateTime.now(), it gives me the right -5 hour offset – Tyler Dahle Sep 21 '17 at 18:29
  • That's correct. You need to use the correct date in order to get the correct offset. – sfdcfox Sep 21 '17 at 18:33
  • Ahh, duh haha. I see the issue now... I was hardcoding that dummy date because all I wanted was the time, the date didn't actually matter. But the better way to go would be to construct a date-time string by getting today's date instead. – Tyler Dahle Sep 21 '17 at 18:44
  • Yeah, that's probably the best way to go. It's certainly the approach I would take. – sfdcfox Sep 21 '17 at 18:45

Your Answer

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

Browse other questions tagged or ask your own question.