比如:当前时间到下周二 05:00:00还剩下多少秒?
1 /// <summary> 2 /// 计算距离下一个 周XX XX时XX分XX秒,还剩下多少秒 3 /// </summary> 4 /// <param name="currDateTime">当时间</param> 5 /// <param name="dayWeek">目标星期</param> 6 /// <param name="str_HourMinuteSecond">目标时间,时分秒的字符串格式 "05:00:00"</param> 7 /// <param name="totalSeconds">计算结果:还剩下的总秒数</param> 8 void CalEndTimeSeconds(DateTime currDateTime, DayOfWeek dayWeek, string str_HourMinuteSecond, out int totalSeconds) 9 { 10 totalSeconds = 0; 11 TimeSpan tp = new TimeSpan(); 12 if (TimeSpan.TryParse(str_HourMinuteSecond, out tp)) 13 { 14 int endPointSeconds = (int)tp.TotalSeconds; 15 16 int currDayPassSeconds = (int)currDateTime.TimeOfDay.TotalSeconds; 17 18 //当天还剩下的秒数 19 totalSeconds = currDayPassSeconds <= endPointSeconds ? (endPointSeconds - currDayPassSeconds) : (perDaySeconds - currDayPassSeconds); 20 21 if (currDateTime.DayOfWeek > dayWeek) 22 { 23 int days = 7 - (int)currDateTime.DayOfWeek; 24 totalSeconds += days * perDaySeconds; 25 totalSeconds += endPointSeconds; 26 } 27 else if (currDateTime.DayOfWeek == dayWeek) 28 { 29 totalSeconds += 0; 30 } 31 else 32 { 33 int days = (int)(dayWeek - currDateTime.DayOfWeek) - 1; 34 totalSeconds += days * perDaySeconds; 35 totalSeconds += endPointSeconds; 36 } 37 } 38 }