C# 日期时间未指定种类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16442484/
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
DateTime Unspecified Kind
提问by Zaheer Ahmed
On msdn it is defined for Unspecified Kind as:
在 msdn 上,它为 Unspecified Kind 定义为:
So if Kind is unspecified DateTime is UTC, but on the same page (given example):
因此,如果 Kind 未指定 DateTime 是 UTC,但在同一页面上(给定示例):
class Sample
{
public static void Main()
{
DateTime saveNow = DateTime.Now;
myDt = DateTime.SpecifyKind(saveNow, DateTimeKind.Unspecified);
Display("Unspecified: .....", myDt);
}
public static string datePatt = @"M/d/yyyy hh:mm:ss tt";
public static void Display(string title, DateTime inputDt)
{
DateTime dispDt = inputDt;
string dtString;
dtString = dispDt.ToString(datePatt);
Console.WriteLine("{0} {1}, Kind = {2}", title, dtString, dispDt.Kind);
dispDt = inputDt.ToLocalTime();
dtString = dispDt.ToString(datePatt);
Console.WriteLine(" ToLocalTime: {0}, Kind = {1}", dtString, dispDt.Kind);
dispDt = inputDt.ToUniversalTime();
dtString = dispDt.ToString(datePatt);
Console.WriteLine(" ToUniversalTime: {0}, Kind = {1}", dtString, dispDt.Kind);
Console.WriteLine();
}
}
}
giving the output as:
给出输出为:
Unspecified: ..... 5/6/2005 02:34:42 PM, Kind = Unspecified
ToLocalTime: 5/6/2005 07:34:42 AM, Kind = Local
ToUniversalTime: 5/6/2005 09:34:42 PM, Kind = Utc
未指定:..... 5/6/2005 02:34:42 PM,种类 = 未指定
ToLocalTime:5/6/2005 07:34:42 AM,种类 = 本地
ToUniversalTime:5/6/2005 09:34:42 PM,种类 = Utc
So, issue I have with this is, that if Unspecified is Utc then why Utc to Utc conversion change the datetime object value?
所以,我遇到的问题是,如果 Unspecified 是 Utc 那么为什么 Utc 到 Utc 的转换会更改日期时间对象值?
采纳答案by Jon Skeet
No, Unspecified and UTC are very different - the page you're quoting from is from ToLocalTime. The point is that if you call ToLocalTimeusing an "unspecified" DateTime, then the value will be treated as ifit were in UTC.
不,未指定和 UTC 是非常不同的 - 您引用的页面来自ToLocalTime. 问题的关键是,如果你调用ToLocalTime使用“未指定” DateTime,则该值将被视为仿佛它是在UTC。
Likewise if you call ToUniversalTimeusing an "unspecified" DateTime, then the value will be treated as ifit were in the system local time zone.
同样,如果你调用ToUniversalTime使用“未指定” DateTime,则该值将被视为好像它是系统中的本地时区。
Frankly this sort of thing is why I dislike DateTimerather a lot. If I were you, I'd use Noda Timeinstead, which separates the concepts out into different types entirely, removing a lot of the confusion. (There are more types to know about, but each one represents a single concept.) I'm clearly biased though...
坦率地说这样的事情是为什么我不喜欢DateTime,而很多。如果我是你,我会改用Noda Time,它将概念完全分成不同的类型,消除了很多混乱。(有更多类型需要了解,但每一种都代表一个概念。)虽然我显然有偏见......
回答by helios456
The DateTimeKind.Unspecified is useful in cases where you don't want the time to be converted to another local time.
DateTimeKind.Unspecified 在您不希望将时间转换为另一个本地时间的情况下很有用。
Take for example a server application which displays the current time for the server in a client application. If you do not specify DateTimeKind.Unspecified on the server and the current time is retrieved through a WCF call, then when .ToString is called in the client application, it will automatically be converted to the local time zone if they are different.
以服务器应用程序为例,它在客户端应用程序中显示服务器的当前时间。如果在服务器上没有指定 DateTimeKind.Unspecified 并且通过 WCF 调用获取当前时间,那么在客户端应用程序中调用 .ToString 时,如果它们不同,它将自动转换为本地时区。


