# 时间计算问题（急）

0

`  　　　　　　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());//结束时间`

10:00 -3：00 有效时间为3小时

xmao-xmao | 初学一级 | 园豆：69

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

jowo | 老鸟四级 |园豆：2834 | 2010-08-25 09:39

jowo | 园豆：2834 (老鸟四级) | 2010-08-25 09:40

您需要登录以后才能回答，未注册用户请先注册