TimeSpan MorningBegin = TimeSpan.Parse(wtime.MorningBegin);//8:30
TimeSpan MonringEnd = TimeSpan.Parse(wtime.MonringEnd);//12:00
TimeSpan AfternoonBegin = TimeSpan.Parse(wtime.AfternoonBegin);2:00
TimeSpan AfternoonEnd = TimeSpan.Parse(wtime.AfternoonEnd);6:30
= TimeSpan.Parse(BeginDate.ToLongTimeString());//开始时间
TimeSpan begin
TimeSpan end = TimeSpan.Parse(EndDate.ToLongTimeString());//结束时间
要求算出:开始时间 到 结束时间 在 8:30-12:00 2:00-6:30 这2个区间的时间
开始时间和结束时间可以任意输入
如:7:20-9:00 那么有效时间为30分钟
10:00 -3:00 有效时间为3小时
求个简单的方法计算
/// <summary>
/// 计算滞留时间
/// </summary>
/// <param name="startDateTime"></param>
/// <param name="endDateTime"></param>
/// <returns></returns>
public string GetStagnateTime(DateTime startDateTime, DateTime endDateTime)
{
DateTime startDateTime1 = startDateTime, endDateTime1 = endDateTime;
string m_strstartWeek = startDateTime.DayOfWeek.ToString();
string m_strendWeek = endDateTime.DayOfWeek.ToString();
string workTime1 = " 08:30:00", workTime2 = " 12:00:00", workTime3 = " 13:30:00", workTime4 = " 17:30:00";//workTime5 = " 23:59:59";
if (m_strstartWeek == "Sunday")
startDateTime = DateTime.Parse(startDateTime.AddDays(1).Date.ToShortDateString() + workTime1);
if (m_strendWeek == "Sunday")
endDateTime = DateTime.Parse(endDateTime.AddDays(-1).Date.ToShortDateString() + workTime4);
if (startDateTime.Hour >= 0 && startDateTime.TimeOfDay.TotalHours < 8.50000)
startDateTime = DateTime.Parse(startDateTime.Date.ToShortDateString() + workTime1);
if (startDateTime.Hour >= 12 && startDateTime.TimeOfDay.TotalHours < 13.5000)
startDateTime = DateTime.Parse(startDateTime.Date.ToShortDateString() + workTime3);
if (startDateTime.TimeOfDay.TotalHours >= 17.50 && startDateTime.TimeOfDay.TotalHours <= 23.9999999)
{
if (startDateTime.Date.AddDays(1).DayOfWeek.ToString() == "Sunday")
startDateTime = DateTime.Parse(startDateTime.Date.AddDays(2).ToShortDateString() + workTime1);
else
startDateTime = DateTime.Parse(startDateTime.Date.AddDays(1).ToShortDateString() + workTime1);
}
if (endDateTime.Hour >= 12 && endDateTime.TimeOfDay.TotalHours <= 13.5000)
endDateTime = DateTime.Parse(endDateTime.Date.ToShortDateString() + workTime2);
if (endDateTime.TimeOfDay.TotalHours >= 17.50 && endDateTime.TimeOfDay.TotalHours <= 23.9999999)
if (endDateTime.Date.AddDays(1).DayOfWeek.ToString() == "Sunday")
endDateTime = DateTime.Parse(endDateTime.Date.AddDays(2).ToShortDateString() + workTime1);
else
endDateTime = DateTime.Parse(endDateTime.Date.AddDays(1).ToShortDateString() + workTime1);
if (endDateTime.Hour >= 0 && endDateTime.TimeOfDay.TotalHours < 8.50000)
endDateTime = DateTime.Parse(endDateTime.Date.ToShortDateString() + workTime1);
TimeSpan midTime = endDateTime - startDateTime;
TimeSpan midTime1 = endDateTime1 - startDateTime1;
Int64 Datemini = Int64.Parse(Math.Round(midTime.TotalMinutes, 0).ToString());
if (midTime.Days < 0)
return "0分钟";
else if (midTime.Days == 0)
{
if ((startDateTime.TimeOfDay.TotalHours >= 8.5000 && endDateTime.TimeOfDay.TotalHours <= 12.000) || (startDateTime.TimeOfDay.TotalHours >= 13.5000 && endDateTime.TimeOfDay.TotalHours <= 17.5000))
{
if (Datemini > 60)
return (Datemini / 60).ToString() + "小时" + (Datemini % 60).ToString() + "分钟";
else
return Datemini.ToString() + "分钟";
}
if (startDateTime.TimeOfDay.TotalHours <= 12.000 && endDateTime.TimeOfDay.TotalHours <= 17.5000)
{
Datemini = Datemini - 90;
if (Datemini > 60)
return (Datemini / 60).ToString() + "小时" + (Datemini % 60).ToString() + "分钟";
else
return Datemini.ToString() + "分钟";
}
}
else
{
int daysBetween = midTime1.Days;
int ValidationDays = 0;
for (int i = 1; i <= daysBetween; i++)
{
if (i % 7 != 0)
ValidationDays++;
}
if ((startDateTime.TimeOfDay.TotalHours >= 8.5000 && endDateTime.TimeOfDay.TotalHours <= 12.000) || (startDateTime.TimeOfDay.TotalHours >= 13.5000 && endDateTime.TimeOfDay.TotalHours <= 17.5000))
{
Datemini = Datemini + Int64.Parse((450 * ValidationDays).ToString());
}
if (startDateTime.TimeOfDay.TotalHours <= 12.000 && endDateTime.TimeOfDay.TotalHours <= 17.5000)
Datemini = (Datemini - 90) + Int64.Parse((450 * ValidationDays).ToString());
return (Datemini / 60).ToString() + "小时";
}
return "";
}
这个我以前做过,以上是我自己写的,和你的差不多就是了,我是计算滞留时间