...
1 /// <summary> 2 /// 判斷程式是否在設置運行時間內 3 /// </summary> 4 /// <param name="startTime">開始執行時間</param> 5 /// <param name="overTime">執行結束時間</param> 6 /// <returns>true:在執行時間內,false:在執行時間外</returns> 7 private bool IsRunTime(string startTime, string overTime) 8 { 9 var flag = false; 10 11 var beginTime = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " " + startTime); 12 var endTime = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " " + overTime); 13 14 if (beginTime <= endTime) // 當天 15 { 16 flag = beginTime <= DateTime.Now && DateTime.Now <= endTime; 17 } 18 else // 跨天 19 { 20 var zeroTime = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd") + " 00:00"); 21 flag = (beginTime <= DateTime.Now && DateTime.Now <= zeroTime.AddDays(1)) || (zeroTime <= DateTime.Now && DateTime.Now <= endTime); 22 } 23 24 return flag; 25 }