C#_計算目前時間到指定的周X、指定的時間X 還有多少秒
阿新 • • 發佈:2019-04-28
times nds als 距離 mes 目前 當前時間 spa min
比如:當前時間到下周二 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 intendPointSeconds = (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 }
C#_計算目前時間到指定的周X、指定的時間X 還有多少秒