首页 新闻 会员 周边 捐助

时间计算问题(急)

0
悬赏园豆:10 [已解决问题] 解决于 2010-08-25 12:50

 

      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 begin
= TimeSpan.Parse(BeginDate.ToLongTimeString());//开始时间
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小时

 

求个简单的方法计算

 

xmao-xmao的主页 xmao-xmao | 初学一级 | 园豆:69
提问于:2010-08-24 17:45
< >
分享
最佳答案
0

/// <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 "";
}

 

这个我以前做过,以上是我自己写的,和你的差不多就是了,我是计算滞留时间

收获园豆:10
jowo | 老鸟四级 |园豆:2834 | 2010-08-25 09:39
改下参数就可以用了
jowo | 园豆:2834 (老鸟四级) | 2010-08-25 09:40
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册