确定两个日期时间之间的区别,只计算开放时间 [英] Determine the difference between two DateTimes, only counting opening hours
本文介绍了确定两个日期时间之间的区别,只计算开放时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有关在C#中我们的支持软件,我需要确定两个日期时间之间的时间跨度,但我只想开数个小时(即平日从09:00至17:00)。
所以,举例来说,如果第一个日期时间是16:00 15/02/2011,第二个是16/02/2011上午10点,该方法将返回2小时。
任何帮助是极大AP preciated!
解决方案
DateTime的开始= DateTime.Parse(15/02/2011 16:00);
日期时间结束= DateTime.Parse(16/02/2011 10:00);诠释计数= 0;对于(VAR I =启动; I<结束; I = i.AddHours(1))
{
如果(i.DayOfWeek = DayOfWeek.Saturday和放大器;!&安培;!i.DayOfWeek = DayOfWeek.Sunday)
{
如果(i.TimeOfDay.Hours> = 9和;&安培; i.TimeOfDay.Hours< 17)
{
算上++;
}
}
}Console.WriteLine(计数);
For our support software in C#, I need to determine the time span between two DateTimes, but I only want opening hours counted (i.e. weekdays from 09:00 to 17:00).
So, for instance, if the first DateTime is 15/02/2011 16:00 and the second is 16/02/2011 10:00, the method shall return 2 hours.
Any help is greatly appreciated!
解决方案
DateTime start = DateTime.Parse("15/02/2011 16:00");
DateTime end = DateTime.Parse("16/02/2011 10:00");
int count = 0;
for (var i = start; i < end; i = i.AddHours(1))
{
if (i.DayOfWeek != DayOfWeek.Saturday && i.DayOfWeek != DayOfWeek.Sunday)
{
if (i.TimeOfDay.Hours >= 9 && i.TimeOfDay.Hours < 17)
{
count++;
}
}
}
Console.WriteLine(count);
这篇关于确定两个日期时间之间的区别,只计算开放时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文