如何仅从日期时间 C# 中获取时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1026841/
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
How to get only time from date-time C#
提问by
Suppose I have the value 6/22/2009 10:00:00 AM. How do I get only 10:00 Am from this date time.
假设我的值为 6/22/2009 10:00:00 AM。我如何才能从这个日期时间到上午 10:00。
回答by Marc Gravell
From a DateTime
, you can use .TimeOfDay
- but that gives you a TimeSpan
representing the time into the day (10 hours).
从 a DateTime
,您可以使用.TimeOfDay
- 但这可以TimeSpan
代表一天中的时间(10 小时)。
回答by heavyd
You might want to look into the DateTime.ToShortTimeString()method.
您可能需要查看DateTime.ToShortTimeString()方法。
Also, there many other methods and properties on the DateTimeobject that can help you in formating the date or time in any way you like. Just take a look at the documentation.
此外,DateTime对象上还有许多其他方法和属性可以帮助您以您喜欢的任何方式格式化日期或时间。只需看看文档。
回答by Hugoware
If you're looking to compare times, and not the dates, you could just have a standard comparison date, or match to the date you're using, as in...
如果您想比较时间而不是日期,您可以只使用标准的比较日期,或者与您使用的日期相匹配,例如...
DateTime time = DateTime.Parse("6/22/2009 10:00AM");
DateTime compare = DateTime.Parse(time.ToShortDateString() + " 2:00PM");
bool greater = (time > compare);
There may be better ways to to this, but keeps your dates matching.
可能有更好的方法来解决这个问题,但要保持您的日期匹配。
回答by Theofanis Pantelides
You have many options for this:
你有很多选择:
DateTime dt = DateTime.Parse("6/22/2009 07:00:00 AM");
dt.ToString("HH:mm"); // 07:00 // 24 hour clock // hour is always 2 digits
dt.ToString("hh:mm tt"); // 07:00 AM // 12 hour clock // hour is always 2 digits
dt.ToString("H:mm"); // 7:00 // 24 hour clock
dt.ToString("h:mm tt"); // 7:00 AM // 12 hour clock
Helpful Link: DateTime.ToString() Patterns
有用的链接: DateTime.ToString() 模式
回答by tarakaram gopal
There are different ways to do so. You can use DateTime.Now.ToLongTimeString()
which
returns only the time in string format.
有不同的方法可以做到这一点。您可以使用DateTime.Now.ToLongTimeString()
which 仅以字符串格式返回时间。
回答by ProfK
This works for me. I discovered it when I had to work with DateTime.Date to get only the date part.
这对我有用。当我不得不使用 DateTime.Date 来仅获取日期部分时,我发现了它。
var wholeDate = DateTime.Parse("6/22/2009 10:00:00 AM");
var time = wholeDate - wholeDate.Date;
回答by matsmats
You can use ToString("T")
for long time or ToString("t")
for short time.
您可以ToString("T")
长时间或ToString("t")
短时间使用。
回答by panpawel
There is only DateTime type in C# and it consist of both the date and time portion. If you don't care about the Date portion of DateTime, set it to default value like this:
C# 中只有 DateTime 类型,它由日期和时间部分组成。如果您不关心 DateTime 的日期部分,请将其设置为默认值,如下所示:
DateTime myTime = default(DateTime).Add(myDateTime.TimeOfDay)
This way you can be consistent across all versions of .NET, even if Microsoft decides to change the base date to something else than 1/1/0001.
通过这种方式,您可以在所有 .NET 版本中保持一致,即使 Microsoft 决定将基准日期更改为 1/1/0001 以外的其他日期。
回答by Kishan Gupta
You can simply write
你可以简单地写
string time = dateTimeObect.ToString("HH:mm");
回答by Javier Andres Caicedo
Try this:
尝试这个:
TimeSpan TodayTime = DateTime.Now.TimeOfDay;