How to use UTC to preserve the integrity of time values when the users want the app to present time in their time zone?

131 views Asked by At

Our company has customers in different time-zones, so our app has to store times in a common standard although app users need to see the time using their time zone.

Whatever local zone it is when time being presented to the user, the value of the time they have edited using their timezone has to be converted back to UTC for storage so that UTC time can be presented to other users using their time zone, and then the time the other users have edited using their time zone has to be converted back to UTC for storage, and so on.

Is there a straight forward way to do that? Didn't see answers in How to convert DateTime in Specific timezone

e.g., The time value picked in my datetime-picker is 2018-08-22 13:30:00 my local time.

1

There are 1 answers

11
Jenna Leaf On

For these time zone conversions to preserve the integrity, these 2 conversions HAVE TO HAPPEN TOGETHER (work in pair) in each web/app session AND use the same TimeZoneInfo.Local in both conversion directions as parameter so to preserve the time-value integrity (avoid discrepancies) !

These TWO have to work in pair to preserve the time integrity :

 //DateTime dtUTC = (DateTime)sqlrdr["editTimeUTC"];
 //From storage to the user
 DateTime dtHERE = TimeZoneInfo.ConvertTime(dtUTC, TimeZoneInfo.Utc, TimeZoneInfo.Local);
 //a new dtHERE after editing to be stored away in UTC 
 dtUTC = TimeZoneInfo.ConvertTime(dtHERE, TimeZoneInfo.Local, TimeZoneInfo.Utc);