0

I have a requirement to convert user time to CST and check user day with the CST day so the first thing is I am converting user Time to CST time by using the below ways

Datetime cstTimeNow      = Datetime.now().format('yyyy-MM-dd HH:mm:ss','America/Chicago');

or

TimeZone tz = TimeZone.getTimeZone('America/Chicago');
integer offsetSeconds = tz.getOffset(dateTime.now());
DateTime cstTimeNow = dateTime.now().addSeconds(offsetSeconds);

both give expected value and now I need to compare the converted CST time and day of the week with a particular day and time, to extract the time and day value I use below.

string cstTimeExtracted = cstTimeNow.format('hh:mm');

it subtracts 5 hours or n number of hours as per user local time and gives incorrect time value and the same way, extracting day also returns incorrect value.

String currentDayOfTheWeek = cstTimeNow.format('EEEE'); 

I somehow managed to get the time correctly using the below but if is there any better ways to extract the time, please do let me know.

String splitTime=cstTimeNow.split(' ')[1]

But finding the Day using the above code still returns an incorrect value for example if CST time is 1 am now and the day is Friday then by using format it subtracts 5 hours from the converted CST time and due to this it changes the day to Thursday.

Could some please suggest a better way to convert user time to CST and then accurately extract the Time and the day of the week.

1 Answer 1

2

DateTime objects have eight components: Year, Month, Day, Hour, Minute, Second, Millisecond, and TimeZone. When you call System.now(), you get a DateTime that is already GMT. When you call DateTime.format, it uses the specified time zone to render the date as that time zone.

When you run:

DateTime cstTimeNow = dateTime.now().addSeconds(offsetSeconds);

You've created a new GMT object that is offsetSeconds different, but still in the GMT time zone, not the CST time zone. This behavior is fairly unintuitive until you think about it really hard. It ends up being off by five hours, because it's GMT-based, which is then offset (again) by calling DateTime.format. You're actually changing by 10 hours total.

You can't willingly create a DateTime object that contains any other time zone other than the user's current time zone or GMT, which is why you need the TimeZone class to come up with the conversion values. You need to shift your mentality from thinking about working in a specific time zone to effectively "always working in GMT."

So, instead of checking to see if it's between 8AM and 5PM CST, for example, you think of it as 2PM to 11PM GMT and use those calculations instead. You can use the TimeZone offset data to calculate the time from CST (or any time zone) to GMT, and then do all your calculations in GMT. If you need to extract the local time back out for display purposes, use DateTime.format() to get the correct value.

1
  • Thank you so much, this is a very useful information.
    – gs650x
    Mar 21, 2021 at 9:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .