从日期时间获取小时和分钟 [英] Get Hours and Minutes from Datetime
问题描述
退房时间:
2013年3月8日15:00:00.000
Out Time : 2013-03-08 15:00:00.000
在时间:
2013年3月8日11:21 :03.290
In Time : 2013-03-08 11:21:03.290
我需要的时间和分分别从上述同日,当(输出时间 - 在时间)
I need to get Hours and Minutes separately for same date from above, when (Out Time - In Time).
我怎样才能做到这一点?
How can I do that ?
推荐答案
我想你也许只是想:
TimeSpan difference = outTime - inTime;
int hours = (int) difference.TotalHours;
int minutes = difference.Minutes;
注意分
会给你刚分钟(从未超过59),而 TotalHours
(接近零截断)会给你的总小时数,这可能会超过23,如果时间更,比相隔一天
Note that Minutes
will give you "just the minutes (never more than 59)" whereas TotalHours
(truncated towards zero) will give you "the total number of hours" which might be more than 23 if the times are more than a day apart.
您也应该考虑你想要做的,如果该值为负什么 - 无论是考虑它,或者明确地通过验证反对排除这种可能性
You should also consider what you want to do if the values are negative - either consider it, or explicitly rule it out by validating against it.
这篇关于从日期时间获取小时和分钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!