`DateTime.Parse`解析哪个时区的时间戳?

Kar*_*sai 5 .net c# datetime parsing

我正在使用DateTime.Parse方法解析日期中的字符串时间。它会正确解析,并以输入的时间返回我今天的日期。但是我无法确定将日期作为日期的时区。是否考虑当地日期或世界标准时间?

public class Program
{
    public static void Main(string[] args)
    {
        //Your code goes here
        Console.WriteLine(DateTime.Parse("4:00"));
        //prints 09.05.2019 04:00:00 - here did it take 09.05.2019 from local timezone or is it UTC?
    }
}
Run Code Online (Sandbox Code Playgroud)

在参考的官方文档DateTime.Parse,隐含地提及如下:

具有时间但没有日期成分的字符串。除非您调用Parse(String,IFormatProvider,DateTimeStyles)重载并且在styles参数中包括DateTimeStyles.NoCurrentDateDefault,否则该方法假定当前日期,在这种情况下,该方法假定日期为0001年1月1日。

我很困惑选择哪种文化的确切日期?

Son*_*nül 6

都不是,确实是Unspecified

首先,在.NET Framework中,DateTime结构体保存时区信息。它具有Kind反映某些信息的属性Utc,如Local或,Unspecified但它们并不完全指时区。

就您而言,由于您不提供任何其他参数,因此您的结果DateTime将为Unspecified.

文档中声明为:

如果s包含时区信息,则此方法返回 Kind 属性为的 DateTime 值DateTimeKind.Local,并将 s 中的日期和时间转换为本地时间。否则,它不执行时区转换并返回其 Kind 属性为 DateTimeKind.Unspecified 的 DateTime 值。

对于表示部分,当您DateTimeConsole.WriteLine方法中使用时,它会调用WriteLine(object)重载,这会调用来自FormatProvider的current 。 Thread.CurrentThread.CurrentCulture