C# 将 DateTimeKind 应用于 DateTime 对象



鉴于DateTime.Kind是一个get-only属性,我们如何将DateTimeKind分配给由DateTime.Parse((诞生的DateTime对象?

var dte = DateTime.Parse("01/01/2018 10:10:00");
Console.WriteLine("{0} | Kind: {1}", dte, dte.Kind.ToString());
// PRINTS: 1/1/2018 10:10:00 AM | Kind: Unspecified
dte = dte.ToLocalTime();
Console.WriteLine("{0} | Kind: {1}", dte, dte.Kind.ToString());
// PRINTS: 1/1/2018 6:10:00 PM | Kind: Local
// OK, the "Kind" is changed to "Local" which is what I'm trying to achieve 
// however it also converted the time!!!

我的问题是如何在不使用 dte 的情况下将 UTC 或本地"种类"应用于对象 dte。ToUniversal(( 或 dte。ToLocal((,因为这两种方法都转换(或更改(时间

您应该能够使用解析的日期时间和采用 DateTimeKind 的构造函数,使用所需的 DateTimeKind 创建新的 DateTime,如下所示:
msdn.microsoft.com/en-us/library/t882fzc6(v=vs.110(.aspx

最新更新