开发者

Convert from Local Client System's Time to CST and vice versa

I need to store the datetime in CST timezone regardless of any timezone given.

The Clients 开发者_开发技巧who access the application are from from various time zones, like IST, CST, EST,...

I need to store all the datetime entered by the client in CST timezone to my database. And while retrieving, i need to convert back to there local timezone.

How to achieve it?


It is generally accepted to store all datetime values in your DB in the GMT/UTC format.

For those who want to render the UTC value to a particular time zone for different users of the application, like wilpeck mentioned, it's suggested that you determine the end users locale and:

  • when persisting, store the locale with the UTC date value
  • when reading, render the UTC value to local time with the associated locale value

EDIT:

For example:

You might have a table with a field StartDateTime so to support multiple time zones you might have an additional field StartDateTimeOffset. If the client is in the Indian Standard Time (IST) zone you might have a datetime value of 2009/10/13 14:45 which is 2009/10/13 09:15 in UTC. So you would store the UTC value (2009/10/13 09:15) in the field StartDateTime and store the offset +05:30 in the StartDateTimeOffset field. Now when you read this value back from the DB you can use the offset to convert the UTC datetime value (2009/10/13 09:15) back to the local time of 2009/10/13 14:45.


Something like this would most likely work out for you. I may be have an incorrect value for the time zone id but I think this is close. The timezone stuff is available in .NET 3.5+

DateTime clientDateTime = DateTime.Now;
        DateTime centralDateTime = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(clientDateTime, "Central Time (US & Canada)");

EDIT:

If you can store DateTime in UTC by detecting the client timezone whenever they provide you a date that would be the best option. Then depending on the client timezone you can render the date according to the client's local timezone.


Try like this.

DateTime clientDateTime = DateTime.Now;
DateTime centralDateTime = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(clientDateTime, "Central Standard Time");

Time Zone Id

DateTime currentTime = DateTime.Now;
Console.WriteLine("Current Times:");
Console.WriteLine();
Console.WriteLine("Los Angeles: {0}", 
                  TimeZoneInfo.ConvertTimeBySystemTimeZoneId(currentTime, TimeZoneInfo.Local.Id, "Pacific Standard Time"));
Console.WriteLine("Chicago: {0}", 
                  TimeZoneInfo.ConvertTimeBySystemTimeZoneId(currentTime, TimeZoneInfo.Local.Id, "Central Standard Time"));
Console.WriteLine("New York: {0}", 
                  TimeZoneInfo.ConvertTimeBySystemTimeZoneId(currentTime, TimeZoneInfo.Local.Id, "Eastern Standard Time"));
Console.WriteLine("London: {0}", 
                  TimeZoneInfo.ConvertTimeBySystemTimeZoneId(currentTime, TimeZoneInfo.Local.Id, "GMT Standard Time"));
Console.WriteLine("Moscow: {0}", 
                  TimeZoneInfo.ConvertTimeBySystemTimeZoneId(currentTime, TimeZoneInfo.Local.Id, "Russian Standard Time"));
Console.WriteLine("New Delhi: {0}", 
                  TimeZoneInfo.ConvertTimeBySystemTimeZoneId(currentTime, TimeZoneInfo.Local.Id, "India Standard Time"));
Console.WriteLine("Beijing: {0}", 
                  TimeZoneInfo.ConvertTimeBySystemTimeZoneId(currentTime, TimeZoneInfo.Local.Id, "China Standard Time"));
Console.WriteLine("Tokyo: {0}", 
                  TimeZoneInfo.ConvertTimeBySystemTimeZoneId(currentTime, TimeZoneInfo.Local.Id, "Tokyo Standard Time"));

https://learn.microsoft.com/en-us/dotnet/api/system.timezoneinfo.converttimebysystemtimezoneid?view=netcore-3.1

https://www.c-sharpcorner.com/blogs/how-to-convert-a-datetime-object-into-specific-timezone-in-c-sharp

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜