visual-studio C# - 仅使用小时:分钟:秒将字符串解析为 DateTime
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4260511/
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
C# - Parsing a string to DateTime with just the hours:minutes:seconds
提问by Niv
I'm receiving the hours, minutes, and seconds as an integer user input.
我收到小时、分钟和秒作为整数用户输入。
Now, when I try and use DateTime.Parse and DateTime.TryParse AND Convert.ToDateTime, they all return the correct hours/minutes/seconds; however, it also returns the date (year, month, day and so on.)
现在,当我尝试使用 DateTime.Parse 和 DateTime.TryParse AND Convert.ToDateTime 时,它们都返回正确的小时/分钟/秒;但是,它还返回日期(年、月、日等)。
string a = this.hours.ToString() + ":" + this.minutes.ToString() + ":" + this.seconds.ToString();
this.alarm = new DateTime();
this.alarm = DateTime.ParseExact(a, "H:m:s", null);
Let's say that hours is 05, minutes is 21 and seconds is 50. "a" would be 05:21:50. I would expect "alarm" to be 05:21:50. However, it actually is (may be):
假设小时为 05,分钟为 21,秒为 50。“a”将是 05:21:50。我希望“警报”是 05:21:50。然而,它实际上是(可能是):
11/23/10 05:21:50
Is there any way to make it work right? Thanks in advance.
有没有办法让它正常工作?提前致谢。
回答by Justin Niessner
A DateTime instance will always include some Date information.
DateTime 实例将始终包含一些 Date 信息。
If you're trying to represent just the time of day, you should use TimeSpan(with the TimeSpan representing the span since midnight...or the time of day. You'll also notice that the DateTime.TimeOfDay property does the same thing).
如果您只想表示一天中的时间,则应该使用TimeSpan(TimeSpan 表示从午夜开始的跨度...或一天中的时间。您还会注意到 DateTime.TimeOfDay 属性执行相同的操作)。
The code would be similar:
代码将是类似的:
var timeOfDay = TimeSpan.ParseExact(a, "H:m:s", null);
回答by steinar
The DateTime always holds time anddate. If you want a class that only saves the time, you can create your own structto handle that. But in the general case, there's no need to as you can simply ignore the date part of the DateTime struct, e.g.:
DateTime 始终保存时间和日期。如果您想要一个只节省时间的类,您可以创建自己的类struct来处理它。但在一般情况下,没有必要,因为您可以简单地忽略 DateTime 结构的日期部分,例如:
string a = "05:21:50";
DateTime alarm = new DateTime();
alarm = DateTime.ParseExact(a, "H:m:s", null);
Console.WriteLine(alarm.ToString("H:m:s"));
Console.WriteLine(alarm.Hour);
Console.WriteLine(alarm.Minute);
Console.WriteLine(alarm.Second);
回答by driis
You cannot do that, because DateTime, by design, represents a Date anda time.
您不能这样做,因为 DateTime 在设计上表示日期和时间。
There is no type in the .NET Framework designed to represent a time of day, but the closest would be TimeSpan. Consider wrapping it in your own TimeOfDay type, if Time-of-day semantics are important to you.
.NET Framework 中没有设计用于表示一天中的时间的类型,但最接近的是 TimeSpan。如果 Time-of-day 语义对您很重要,请考虑将其包装在您自己的 TimeOfDay 类型中。
回答by ?yvind Br?then
As the other have mentioned here, a DateTimeobject will always contain a Datepart, but the easiest is just to ignore it. Then, when printing it out, you can use the following syntax:
正如另一个在这里提到的,一个DateTime对象总是包含一个Date部分,但最简单的就是忽略它。然后,在打印出来时,您可以使用以下语法:
alarm.ToShortTimeString();

