你的位置:首页 > ASP.net教程

[ASP.net教程]Windows服务定时运行,判断当前时间是否在配置时间段内


 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     }