c# - 使用C#将DateTime从UTC转换为用户提供的时区

标签 c# asp.net datetime timezone

说,如果我有一个以UTC时区保存日期/时间的数据库列,则可以将它们读入用C#编写的ASP.NET Web应用程序的DateTime对象中。如何将它们转换为用户提供的时区?

最佳答案

假设time具有Kind = DateTimeKind.Utc

您可以使用ConvertTimeFromUtc

TimeZoneInfo.ConvertTimeFromUtc(time, userTimeZone);


TimeZoneInfo.ConvertTime

TimeZoneInfo.ConvertTime(time, TimeZoneInfo.Utc, userTimeZone);


您可能想查看TimeZoneInfo.ConvertTime上有关MSDN文章的详细信息。

也可能值得阅读有关converting between time zones on MSDN的全部内容。它比您想象的要复杂。

关于c# - 使用C#将DateTime从UTC转换为用户提供的时区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11623472/

相关文章:

javascript - 一旦获得焦点,将 RadioButton 值设置为 true,然后点击 asp :RadioButton 中的 Enter 按钮

php - 从 MySQL 中检索每日和每月总计以用于图表

C# DateTime.ToString "o"格式在 Azure 上返回不同的字符串

javascript - 选择从 JavaScript 填充的控件返回空值

c# - 我应该如何强制加载引用的程序集?

c# - HttpClient 抛出 System.ArgumentException : 'windows-1251' is not a supported encoding name

c# 在事务中检索插入的 ID

c# - <identity impersonate ="true"/>的正确使用

javascript - 检查现在是否距前 3 小时以内

c# - Serilog WriteTo.File() 在控制台应用程序中有效,但在 .net Core 3 Worker Service 中无效