如果在5分钟内运行其他方法,请检查具体时间

Jam*_*son 1 c# datetime

我有一个每30分钟24/7运行一次的程序.但是在凌晨1:30我需要运行原始方法,然后我还需要运行其他方法.

我知道DateTime.Now.TimeOfDay会给我当前的时间.

但我们的自动化程序如果备份可能会从1:30开始运行5分钟,所以如果时间是凌晨1:30到凌晨1:40,请告诉程序运行我的附加方法.

Ini*_*eer 5

您可以确定当前TimeSpan是否在两个指定TimeSpan时间之间.

    TimeSpan currentTime = DateTime.Now.TimeOfDay;

    TimeSpan earliest = new TimeSpan(1, 30, 0); // 1:30 AM
    TimeSpan latest   = new TimeSpan(1, 40, 0); // 1:40 AM

    if (currentTime >= earliest && currentTime <= latest)
    {
        // It's between 1:30 AM and 1:40 AM - Do Something!!
    }
Run Code Online (Sandbox Code Playgroud)