C# - 两个日期时间之间的持续时间(以分钟为单位)
我需要确定两个日期时间之间的持续时间(以分钟为单位)。
然而,有一个细微的差别:
- 排除周末
- 只计算分钟数 上午 7:00 至晚上 7:00 之间。 例如:<代码>[09/30/2010 6:39:00 下午] - [09/30/2010 7:39:00 下午] = 21 分钟
我只是很难想出一个合适的方法来做到这一点,如果有人能提出建议,我将不胜感激。
谢谢。
编辑:
我最终选择了 dtb 的解决方案。只有一种特殊情况需要处理:如果结束时间在晚上 7:00 之后,则计算从上午 7:00 到实际结束时间的分钟数。
这就是我修改它的方法:
var minutes = from day in start.DaysInRangeUntil(end)
where !day.IsWeekendDay()
let st = Helpers.Max(day.AddHours(7), start)
let en = (day.DayOfYear == end.DayOfYear ?
end :
Helpers.Min(day.AddHours(19), end)
)
select (en - st).TotalMinutes;
再次感谢您的帮助。
I need to determine duration between two DateTimes in minutes.
However, there is a slight twist:
- exclude weekends
- only count minutes
which are between 7:00AM and 7:00PM.
for example:[09/30/2010 6:39:00
PM] - [09/30/2010 7:39:00 PM] = 21
Minutes
I'm just having a hard time coming up with a decent way to do it and would appreciate if anyone can suggest something.
Thanks.
Edit:
I ended up going with dtb's solution. There is only one special case which needed to be taken care of: if end time is after 7:00PM, count the minutes from 7:00AM to the actual end time.
This is how I modified it:
var minutes = from day in start.DaysInRangeUntil(end)
where !day.IsWeekendDay()
let st = Helpers.Max(day.AddHours(7), start)
let en = (day.DayOfYear == end.DayOfYear ?
end :
Helpers.Min(day.AddHours(19), end)
)
select (en - st).TotalMinutes;
Again, thanks for the help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
当然,您可以使用 LINQ:(
注意:您可能需要检查许多我完全忽略的极端情况。)
辅助方法:
You can, of course, use LINQ:
(Note: You probably need to check for a lot of corner cases which I completely ignored.)
Helper methods:
记下您的开始时间,计算到当天结束(即晚上 7 点)为止的分钟数。
然后从第二天早上 7 点开始,计算到最后一天的天数(不包括进入结束日的任何时间)。
计算已经过去了多少个(如果有)周末。 (每个周末将天数减少 2)。
从那里做一些简单的数学计算,得到天数的总分钟数。
添加最后一天的额外时间和开始日的额外时间。
Take your start time, get the amount of minutes to the end of that day (Ie the 7pm).
Then from the 7am the next day, count the amount of days to the final day (excluding any time into the end day).
Calculate how many (If any) weekends have passed. (For every weekends reduce the count of days by 2).
Do some simple math from there to get the total minutes for the count of days.
Add the extra time on the final day and the start days extra time.
尝试以下 DiffRange 函数。
Try the following DiffRange function.
我确信我错过了一些东西。
I'm sure there's something I missed.
这是一个很难回答的问题。对于基本的、简单的方法,我编写了以下代码:
我知道这在编程上看起来并不美观,但我不知道在开始和结束之间迭代几天和几小时是否有好处。
It was a pretty hard question. For a basic, in a straightforward approach, I have written the below code:
I know this not seem programmatically beautiful but I don't know if it is good to iterate through days and hours between start and end.
我的实现:)这个想法是快速计算总周数,并日复一日地走剩下的一周......
My implementation :) The idea is to quickly compute the total weeks, and walk the remaining week day by day...
使用TimeSpan.TotalMinutes,减去非工作日,减去多余的时间。
Use TimeSpan.TotalMinutes, subtract non-business days, subtract superfluous hours.
我不会编写任何代码,但是有了 DateTime,您就可以知道星期几,因此您知道有多少个周末在您的范围内,因此您可以知道一个周末有多少分钟。
所以这不会那么难......当然必须有一个最佳的单行解决方案......但我认为你可以解决这个问题。
我忘了提及,您还知道晚上 7:00 到早上 7:00 之间的分钟数,因此您所要做的就是在得到的时差中减去正确的分钟数。
I won't write any code, but having a DateTime you can tell the Day of the week, thus you know how many weekends are in your range, so you can tell how many minutes are in a weekend.
So it wouldn't be so hard... of course there must be an optimal one line solution... but i think you can get around with this one.
I forgot to mention that you also know the minutes btween 7:00 pm and 7:00 am so all you have to do is substract the right amount of minutes to the time difference you get.