C# 将 UTC 日期时间转换为本地日期时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12937968/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 01:00:58  来源:igfitidea点击:

Converting UTC DateTime to local DateTime

c#datetimeutc

提问by Shane Courtrille

I have the following ASP.Net MVC Controller method:

我有以下 ASP.Net MVC 控制器方法:

public ActionResult DoSomething(DateTime utcDate)
{
   var localTime = utcDate.ToLocalTime();
}

The problem is that localTime will have the exact same value as utcDate. I assume this is because utcDate doesn't know it has a UTC value. So my question is how can I convert utcDate (which I KNOW is UTC) into local?

问题是 localTime 将具有与 utcDate 完全相同的值。我认为这是因为 utcDate 不知道它具有 UTC 值。所以我的问题是如何将 utcDate (我知道它是 UTC)转换为本地?

采纳答案by Rawling

If you knowthe DateTimecontains a UTC value, you can use the following:

如果你知道DateTime包含UTC值,可以使用以下

DateTime iKnowThisIsUtc = whatever;
DateTime runtimeKnowsThisIsUtc = DateTime.SpecifyKind(
    iKnowThisIsUtc,
    DateTimeKind.Utc);
DateTime localVersion = runtimeKnowsThisIsUtc.ToLocalTime();

For example, in my current application, I create timestamps in my database with SQL's utcnow, but when I read them into my C# application the Kindproeprty is always Unknown. I created a wrapper function to read the timestamp, deliberately set its Kindto Utc, and then convert it to local time - essentially as above.

例如,在我当前的应用程序中,我使用 SQL 在我的数据库中创建时间戳utcnow,但是当我将它们读入我的 C# 应用程序时,Kind属性总是Unknown. 我创建了一个包装函数来读取时间戳,特意将其设置KindUtc,然后将其转换为本地时间 - 基本上如上所述。

Note that DateTime.ToLocalTime()only doesn't affect the value if one (or both) of the following holds:

请注意,DateTime.ToLocalTime()只有在以下一项(或两项)成立时才不会影响该值:

  • The DateTime's Kindproperty is DateTimeKind.Local
  • Your local timezone is such that "no change" is the correct conversion
  • DateTimeKind属性DateTimeKind.Local
  • 您当地的时区是“无变化”是正确的转换

I think we can assume the second point isn't true. Thus it seems that iKnowThisIsUtc's Kindproperty is set to Localalready. You need to figure out why whatever is supplying you with these DateTimes thinks they are local.

我想我们可以假设第二点是不正确的。因此,似乎iKnowThisIsUtcKind属性已设置为Local。您需要弄清楚为什么向您提供这些DateTimes 的任何东西都认为它们是本地的。

回答by Robert H

Try the following:

请尝试以下操作:

public static DateTime UtcToPacific(DateTime dateTime)
        {
            return TimeZoneInfo.ConvertTimeFromUtc(dateTime, TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time"));
        }

Obviously change Pacific to your local timezone.

显然将太平洋更改为您当地的时区。

If you want to find out what time zones are present on your machine see the following link: http://msdn.microsoft.com/en-us/library/bb397781.aspx

如果您想了解您的机器上存在哪些时区,请查看以下链接:http: //msdn.microsoft.com/en-us/library/bb397781.aspx

回答by Yann Olaf

or this:

或这个:

var localTime = new DateTimeOffset(utcDate,TimeSpan.FromHours(0))
                    .ToLocalTime().DateTime;

回答by tranceporter

as described by MSDN, you need the Kind property of your Date to be UTC, for the ToLocalTime() to work (and convert the date to local time. http://msdn.microsoft.com/en-us/library/system.datetime.tolocaltime.aspx

如 MSDN 所述,您需要 Date 的 Kind 属性为 UTC,以便 ToLocalTime() 工作(并将日期转换为本地时间。http://msdn.microsoft.com/en-us/library/system .datetime.tolocaltime.aspx

Try this:

尝试这个:

utcDate = DateTime.SpecifyKind(utcDate, DateTimeKind.Utc);
var localTime = utcDate.ToLocalTime();