I am working with .NET, and I am running into issues with time zones. I have separate date and time components, both as strings. I want to parse the string as Eastern Prevailing Time (EPT). However, my system is using the UTC clock, so I cannot assume it to be the local time while parsing.
What's the way to go about this? Is there a library that can come in handy?
Currently, I am doing the following:
const string DateFormat = "yyyyMMdd";
const string TimeFormat = "HH:mm:ss";
if (DateTime.TryParseExact(date_string, DateFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out var dateOnly))
{
if (DateTime.TryParseExact(time_string, TimeFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out var timeOnly))
{
return dateOnly.Date + timeOnly.TimeOfDay;
}
return dateOnly.Date;
}
The current implementation parses the DateTime
values as UTC, but we know that these are in EPT. If I were to convert the parsed DateTime
to EPT, then the actual time would change along with the time zone. Changing the offset manually doesn't help either, since it does not take into account daylights savings.