我如何每两个月做一份工作? [英] How do I run a job every 2 month?

查看:58
本文介绍了我如何每两个月做一份工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须根据日期过滤今天要运行的作业。因此,对于每个计划的日期,我将它与DateTime.Today.Date进行比较,并获取计划在今天运行的作业列表。

如何根据给定的周期时间过滤周期性作业?

示例:

- >如果周期时间为0,则在预定日期每月运行。

- >如果周期时间为2,第一次在预定的一天运行工作,然后每两个月运行一次。



我尝试过的事情:



我必须以这种方式添加周期时间,如果计划日期= 3/7/2016,周期时间= 2,如果计划日期==今天,则运行服务并添加2个月(2016年5月7日)并在该日期运行服务并添加2个月(2016年7月7日)等等。

我这样写,但如果它曾经返回true它没有检查其他条件。这对我的情况不起作用。



var x =预定日期;

if(cycleTime == 0&& x ==今天)

{

返回true;

}

if(cycleTime!= 0&& ; x ==今天)

{

x = x.AddMonths(cycleTime);

if(x ==今天)

{

返回true;

}

}

I have to filter the jobs to be run today based on the date. So, for every scheduled date, I am comparing it to DateTime.Today.Date and getting a list of jobs that are scheduled to run today.
How can I filter the recurring job based on the given cycle time?
Example:
->If cycle time is 0, run every month on the scheduled date.
->If cycle time is 2, for the first time run the job on scheduled day and then run every two months.

What I have tried:

I have to add cycle time in this way, if schedule date = 3/7/2016, cycle time =2, and if scheduled date == today, run service and add 2 months(5/7/2016) and run service on that date and add 2 months(7/7/2016) and so on.
I wrote this way, but if it once returns true it is not checking the other condition. And this is not working for my scenario.

var x = Scheduled date;
if (cycleTime == 0 && x == today)
{
return true;
}
if (cycleTime != 0 && x== today)
{
x = x.AddMonths(cycleTime);
if (x== today)
{
return true;
}
}

推荐答案

if (cycleTime != 0 && x== today)
{
    x = x.AddMonths(cycleTime);
    if (x== today) // x no longer equals today 
    {
        return true;
    }
}



您已将x的值更改为比今天大几个月,所以在第二次测试时它不会等于。为什么你甚至需要第二次测试?


You have changed the value of x to be some number of months greater than today, so on the second test it will not be equal. Why do you even need the second test?


这篇关于我如何每两个月做一份工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆