从UTC日期时间转换与C#用户提供的时区 [英] Convert DateTime from UTC to a user provided time zone with C#
问题描述
我说,如果我有保存日期/时间在UTC时区数据库列,我能读懂他们到DateTime对象在用C#编写我的ASP.NET web应用程序。我该如何将它们转换为用户提供的时区?
Say, if I have a database column that holds date/time in UTC time zone, I can read them into DateTime object in my ASP.NET web app written with C#. How do I convert them to a user provided time zone?
推荐答案
这假定时间
有一个 KIND = DateTimeKind.Utc
您可以使用 ConvertTimeFromUtc
:
You could use ConvertTimeFromUtc
:
TimeZoneInfo.ConvertTimeFromUtc(time, userTimeZone);
TimeZoneInfo.ConvertTime(time, TimeZoneInfo.Utc, userTimeZone);
您可能会想看看在 TimeZoneInfo.ConvertTime
该方法的来龙去脉。
You will probably want to check out the MSDN article on TimeZoneInfo.ConvertTime
for the ins and outs of the method.
这可能是值得一读的所有关于MSDN 时区之间转换为好。它比你想象的要复杂得多。
It's probably worth reading all about converting between time zones on MSDN as well. It's more complex than you might think.
这篇关于从UTC日期时间转换与C#用户提供的时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!