C# DateTime:当我只使用“时间”时使用什么“日期”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/372601/
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-04 00:57:14  来源:igfitidea点击:

C# DateTime: What "date" to use when I'm using just the "time"?

c#datetimetime

提问by Ian Boyd

I'm using a DateTimein C# to display times. What date portion does everyone use when constructing a time?

DateTime在 C# 中使用 a来显示时间。每个人在构建时间时都使用哪个日期部分?

E.g. the following is not valid because there is no zero-th month or zero-th day:

例如以下是无效的,因为没有第零个月或第零天:

// 4:37:58 PM
DateTime time = new DateTime(0, 0, 0, 16, 47, 58);

Do I use COM's zero date?

我是否使用 COM 的零日期?

// 4:37:58 PM
DateTime time = new DateTime(1899, 12, 30, 16, 47, 58);

Or perhaps SQL Server's?

或者也许是 SQL Server 的?

//4:37:58 PM
DateTime time = new DateTime(1900, 1, 1, 16, 47, 58);

I realize it's arbitrary, since I'll be ignoring the date portions in code, but it would still be nice to be able to use:

我意识到这是任意的,因为我将忽略代码中的日期部分,但是能够使用仍然很好:

DateTime duration = time2 - time1;


Answer

回答

I think I like MinValue

我想我喜欢MinValue

 DateTime time = DateTime.MinValue.Date.Add(new TimeSpan(16, 47, 58));


Note:I can't use a TimeSpan, because that doesn't store times of the day. And the reason I know that is because there's no way to display its contents as a time.

注意:我不能使用 a TimeSpan,因为它不存储一天中的时间。我知道的原因是因为没有办法将其内容显示为时间。

Which is to say that TimeSpanrecords a span of time, not a time of day, e.g.:

也就是说,TimeSpan记录时间跨度,而不是一天中时间,例如:

TimeSpan t = new TimeSpan(16, 47, 58);
t.ToString();

returns a span of time in the format hours:minutes:seconds, e.g.:

小时分钟的格式返回时间跨度,例如:

16:47:58

rather than a time:

而不是时间:

4:47:58 PM    (United States)
04:47:58 nm   (South Africa)
4:47:58.MD    (Albania)
16:47:58      (Algeria)
04:47:58 ?    (Bahrain)
PM 4:47:58    (Singapore)
下午 04:47:58  (Taiwan)
04:47:58 PM   (Belize)
4:47:58 p.m.  (New Zealand)
4:47:58 μμ    (Greece)
16.47.58      (Italy)
?? 4:47:58   (Korea)
04:47:58 ?.?  (Iran)
??? 04:47:58   (India)
04:47:58 p.m. (Argentina)
etc

In other words, there is a difference between a timespan, and a time. And also realize that TimeSpandoesn't provide a mechanism to convert a span of time into a time of day - and there is a reason for that.

换句话说,时间跨度和时间是有区别的。并且还意识到这TimeSpan并没有提供将时间跨度转换为一天中的时间的机制 - 这是有原因的。

采纳答案by Joachim Kerschbaumer

what about DateTime.MinValue?

DateTime.MinValue 怎么样?

回答by Marc Gravell

How about DateTime.Now.TimeOfDay, and use the TimeSpan?

怎么样DateTime.Now.TimeOfDay,使用TimeSpan

Re "because that doesn't store times of the day." - well, it does if you think of a TimeSpanas the time since midnight.

重新“因为它不存储一天中的时间。” - 好吧,如果您将 aTimeSpan视为自午夜以来的时间,则确实如此。

A "duration", for example, screams TimeSpan.

例如,“持续时间”会尖叫TimeSpan

回答by Joe

Given that DateTime.TimeOfDay returns a TimeSpan, I'd use that.

鉴于 DateTime.TimeOfDay 返回一个 TimeSpan,我会使用它。

Why can't you use a TimeSpan? I don't understand your comment that it doesn't store times of day.

为什么不能使用 TimeSpan?我不明白你的评论,它不存储一天中的时间。

回答by Greg Hurlman

A TimeSpan most certainly can store the time of the day - you just have to treat the value as the amount of time elapsed since midnight, basically the same way we read a clock.

TimeSpan 肯定可以存储一天中的时间 - 您只需将该值视为自午夜以来经过的时间量,基本上与我们读取时钟的方式相同。

回答by Joel Coehoorn

I recommend DateTime.MinValue

我建议 DateTime.MinValue

回答by Neil

To display a TimeSpan formatted with local culture, simply add it to a date like DateTime.Today. Something like this:

要显示使用本地文化格式化的 TimeSpan,只需将其添加到 DateTime.Today 之类的日期即可。像这样的东西:

(DateTime.Today + timeSpan).ToString();

(DateTime.Today + timeSpan).ToString();

Since your value really doesn't represent a date, you're better off storing it as a TimeSpan until the time comes to display it.

由于您的值确实不代表日期,因此最好将其存储为 TimeSpan,直到需要显示它为止。

回答by Oliver Friedrich

You can just create a new DateTime with a string literal.

您可以使用字符串文字创建一个新的 DateTime。

String literal for time:

时间的字符串文字:

DateTime t = new DateTime("01:00:30");

String literal for date:

日期的字符串文字:

DateTime t = new DateTime("01/05/2008"); // english format
DateTime t = new DateTime("05.01.2008"); // german format

For a DateTime with date and time values:

对于具有日期和时间值的 DateTime:

DateTime t = new DateTime("01/05/2008T01:00:30");

In most cases, when creating a DateTime, i set it to DateTime.Now, if it is not actually set to anything else. If you instantiate an DateTime manually, you should beware of the DateTimeKind set correctly, otherwise this could lead to surprises.

在大多数情况下,当创建 DateTime 时,我将它设置为 DateTime.Now,如果它实际上没有设置为其他任何内容。如果您手动实例化 DateTime,则应注意正确设置的 DateTimeKind,否则可能会导致意外。

回答by Greg Beech

Personally I'd create a custom Timestructthat contains a DateTimeinstance, and which has similar properties, constructors etc. but doesn't expose days/months/etc. Just make all your public accessors pass through to the contained instance. Then you can simply have the epoch as a private static readonly DateTimefield and it doesn't matter what value you choose as it's all neatly contained within your custom struct. In the rest of your code can simply write:

我个人会创建一个Timestruct包含DateTime实例的自定义,它具有类似的属性、构造函数等,但不公开天/月/等。只需让您的所有公共访问器都传递到包含的实例。然后你可以简单地将纪元作为一个private static readonly DateTime字段,你选择什么值并不重要,因为它都整齐地包含在你的自定义结构中。在你的代码的其余部分可以简单地写:

var time = new Time(16, 47, 58);

回答by TheSoftwareJedi

Use a TimeSpan, and make it UTC if you have TimeZone issues.

如果您有 TimeZone 问题,请使用 TimeSpan,并将其设为 UTC。

回答by Andrei R?nea

May I suggest that in some cases a custom struct could do? It could have an Int32 backing value (there are 86 milion milliseconds in a day; this would fit in an Int32).

我可以建议在某些情况下自定义结构可以吗?它可以有一个 Int32 支持值(一天有 8600 万毫秒;这适合 Int32)。

There could be get-only properties :

可能有 get-only 属性:

Hours Minutes Seconds Milliseconds

小时 分钟 秒 毫秒

You could also overload operators such as +, - and so on. Implement IEquatable, IComparable and whatever may be the case. Overload Equals, == . Overload and override ToString.

您还可以重载 +、- 等运算符。实现 IEquatable、IComparable 和任何可能的情况。重载等于,==。重载和覆盖 ToString。

You could also provide more methods to construct from a DateTime or append to a datetime and so on.

您还可以提供更多方法来从 DateTime 构造或附加到 datetime 等等。