function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
VivekShindeVivekShinde 

Issue with datetime to date conversion when daylight saving

I'm facing a weird issue when a datetime is converted to date between midnight and 1 AM ET (Eastern Time). We're facing the issue after the day light saving which is post 12th March 2017.

We're fetching the current date and time using system.now() method. The datetime is returned in GMT timezone.

If the current datetime is 3/21/2017 12.15 AM, then the GMT returned by the system.now() mehtod is 4.15 AM. 4 hours are added in the time since, ET is 4 hours behind GMT.

Now, we're making use of the DateTime class's instance method, format('EEE') to get the current day. For the stated date in the example, the day comes out to be Tuesday. Based on this day, we've some calculations which determine the number of days to be subtracted from the datetime instance.

Suppose, it is determined that we want to subtract 2 days and get the date. After subtracting 2 days from the datetime 3/21/2017 12.15 AM, we're getting datetime as 3/19/17 12.15 AM.

We want the date component from this datetime, hence are making use of the DateTime class' instace method date() to fetch the date. But, the datetime is converted to date, it is returning 3/18/2017 instead of 3/19/2017. The date() method returns date in local timezone and I believe that when the datetime is converted to date, the date is getting calculated by subtracting 5 hours instead of 4 hours to get the date in local timezone which is ET.

Any help regarding understanding the reason behind this will be much obiliged. Thanks in advance!
sfdcsushilsfdcsushil
Vivek, what do you have in your user profile? There are two options over there - you can chose Eastern Standard Time or Eastern Daylight Time. 
VivekShindeVivekShinde
Thanks Sushil for replying. It is customer community user whose time zone is set as "America/Indiana/Indianapolis" while registering a new community user. The timezone was GMT-5 before daylight saving. Now the timezone which is shown on the user record is GMT-4 after the daylight saving effective after 12th March 2017.
sfdcsushilsfdcsushil
Vivek, I am sorry i did not get chance to look at the issue , were you able to find the fix. If yes, please you may post it here and mark that as answer. 
VivekShindeVivekShinde
Sushil, I was doing all these calculations for getting the start of the week's date. To fulfil the requirement, we're now using toStartOfWeek method of Date class. The issue was not resolved from the logic which was implmented earlier.