# What happens on day 31

**URL:** <https://discuss.hangfire.io/t/what-happens-on-day-31/3779>\
**Category:** question\
**Tags:** recurring\
**Created:** [September 11, 2017, 10:01am UTC](https://discuss.hangfire.io/t/what-happens-on-day-31/3779 "2017-09-11T10:01:53Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rui\_Queiros](https://avatars.discourse-cdn.com/v4/letter/r/54ee81/32.png) [@Rui\_Queiros](https://discuss.hangfire.io/u/Rui_Queiros)\
**Post date:** [September 11, 2017, 10:01am UTC](https://discuss.hangfire.io/t/what-happens-on-day-31/3779/1 "2017-09-11T10:01:53Z")

</div>

Hello

What happens in the months like February and April who does not have 31 days if I set a Recurrjing Job to run every 31 which whould mean the last day of the month? Or for example day 30, which also does not exist in february for instance?

---

<div class="post-metadata">

**Author:** ![anon95177454](https://avatars.discourse-cdn.com/v4/letter/a/45deac/32.png) [@anon95177454](https://discuss.hangfire.io/u/anon95177454)\
**Post date:** [September 12, 2017, 7:18am UTC](https://discuss.hangfire.io/t/what-happens-on-day-31/3779/2 "2017-09-12T07:18:20Z")

</div>

It should run on the last day of that mothn

---

<div class="post-metadata">

**Author:** ![Rui\_Queiros](https://avatars.discourse-cdn.com/v4/letter/r/54ee81/32.png) [@Rui\_Queiros](https://discuss.hangfire.io/u/Rui_Queiros)\
**Post date:** [September 12, 2017, 10:42am UTC](https://discuss.hangfire.io/t/what-happens-on-day-31/3779/3 "2017-09-12T10:42:38Z")

</div>

![](https://europe1.discourse-cdn.com/flex017/uploads/hangfire/original/2X/b/b47ad7b2a2ff8788db55fe44df3eef0096a9ff16.jpg)

You sure about that? I’m not enterily sure that is what will happen.

---

<div class="post-metadata">

**Author:** ![anon95177454](https://avatars.discourse-cdn.com/v4/letter/a/45deac/32.png) [@anon95177454](https://discuss.hangfire.io/u/anon95177454)\
**Post date:** [September 12, 2017, 11:03am UTC](https://discuss.hangfire.io/t/what-happens-on-day-31/3779/4 "2017-09-12T11:03:27Z")

</div>

I was wrong 31 will always match just 31st day of month. If month has no 31st so it won’t run.  
However you can use L (which stands for LAST)  
0 0 L \* \* will run on every last day of month

> **[Cron | CRON expression](https://en.wikipedia.org/wiki/Cron#CRON_expression)**
>
> A CRON expression is a string comprising five or six fields separated by white space that represents a set of times, normally as a schedule to execute some routine. Comments begin with a comment mark #, and must be on a line by themselves.

---

<div class="post-metadata">

**Author:** ![Rui\_Queiros](https://avatars.discourse-cdn.com/v4/letter/r/54ee81/32.png) [@Rui\_Queiros](https://discuss.hangfire.io/u/Rui_Queiros)\
**Post date:** [September 12, 2017, 11:12am UTC](https://discuss.hangfire.io/t/what-happens-on-day-31/3779/5 "2017-09-12T11:12:25Z")

</div>

It still does not remove the February problem, L for 31 will work, but 30 won’t exist in February also, so If I say that 30 will run in the end of the month, sometimes it will run on 31 instead of 30 which would make it run in the wrong day in 7 months.

Basically February ruins everything. Day 29 will also be a problem because it may or it may not exist 😅

By the way just tested with L and I got:`'L' is not a valid [Day] crontab field value. It must be a numeric value between 1 and 31 (all inclusive).`

---

<div class="post-metadata">

**Author:** ![taiko](https://avatars.discourse-cdn.com/v4/letter/t/8baadc/32.png) [@taiko](https://discuss.hangfire.io/u/taiko)\
**Post date:** [September 13, 2017, 3:02am UTC](https://discuss.hangfire.io/t/what-happens-on-day-31/3779/6 "2017-09-13T03:02:49Z")

</div>

Hi, Rui. I just tested ir with the ‘L’ CRON keyword and it is in fact as you said. It’s not valid.

But you get get around it by just getting the day number yourself and just concatenate the string. For instance:

`var day = DateTime.DaysInMonth(DateTime.UtcNow.Year,2).ToString();`

I tested it with minute incrementation only as it would take too long to wait the end of each month to test it. 🙂 So, this was it:

```
    private static string LigacaoBD = @"*Your Data Source*";
    private static int number = 0;
    private static string id = ""; 

    var server = new BackgroundJobServer();
    BackgroundJob.Enqueue(() => test());

    public string Increment()
    {
        number++;
        cron = "*/" + number + " * * * *";
        return cron;
    }

    public void test()
    {

        using (var connection = JobStorage.Current.GetConnection())
        {
            foreach (var recurringJob in connection.GetRecurringJobs())
            {
                recurringJob.Cron = Increment();
            }
        }

        RecurringJob.AddOrUpdate(() => test(), cron);
    }

```

So, bascially, you have to “manually” look for the cron of the recurring job and order a change (otherwise it won’t update) and send it to a string. And then just use that string as the cron.

I tested it with minute incrementation, but you can replace that with the line I showed at the beginning of the about, about the days in month.

Hope it helped. 🙂

---

<div class="post-metadata">

**Author:** ![burningice](https://dub1.discourse-cdn.com/flex017/user_avatar/discuss.hangfire.io/burningice/32/53_2.png) [@burningice](https://discuss.hangfire.io/u/burningice)\
**Post date:** [September 13, 2017, 8:34am UTC](https://discuss.hangfire.io/t/what-happens-on-day-31/3779/7 "2017-09-13T08:34:59Z")

</div>

Try reading this thread, it has a ton of different approaches to the dilemma. The easiest being running the job on the first day of the month instead and substraction a day in your code.

> <https://stackoverflow.com/questions/6139189/cron-job-to-run-on-the-last-day-of-the-month>

---

<div class="post-metadata">

**Author:** ![taiko](https://avatars.discourse-cdn.com/v4/letter/t/8baadc/32.png) [@taiko](https://discuss.hangfire.io/u/taiko)\
**Post date:** [September 13, 2017, 2:11pm UTC](https://discuss.hangfire.io/t/what-happens-on-day-31/3779/8 "2017-09-13T14:11:04Z")

</div>

Yes, it can definitely be easier to do this of the first day rather than the last. But it depends on Rui’s problem. If it’s not affected he might be better off just doing it on the first day. If it is, than there are other solutions. I posted mine above. It’s an example and he can adapt it to his own needs. 🙂

---

<div class="post-metadata">

**Author:** ![Rui\_Queiros](https://avatars.discourse-cdn.com/v4/letter/r/54ee81/32.png) [@Rui\_Queiros](https://discuss.hangfire.io/u/Rui_Queiros)\
**Post date:** [September 13, 2017, 2:17pm UTC](https://discuss.hangfire.io/t/what-happens-on-day-31/3779/9 "2017-09-13T14:17:40Z")

</div>

I will read everything, but I can’t delay, I can advance and that is what I am currently doing right now, 29,30 and 31 are all being processed in 28. But I will read all your suggestions.

Thank you all

---

<div class="post-metadata">

**Author:** ![taiko](https://avatars.discourse-cdn.com/v4/letter/t/8baadc/32.png) [@taiko](https://discuss.hangfire.io/u/taiko)\
**Post date:** [September 14, 2017, 3:57pm UTC](https://discuss.hangfire.io/t/what-happens-on-day-31/3779/10 "2017-09-14T15:57:48Z")

</div>

Please do, and give feedback on your choice. 🙂

---

<div class="post-metadata">

**Author:** ![pieceofsummer](https://dub1.discourse-cdn.com/flex017/user_avatar/discuss.hangfire.io/pieceofsummer/32/785_2.png) [@pieceofsummer](https://discuss.hangfire.io/u/pieceofsummer)\
**Post date:** [September 14, 2017, 9:05pm UTC](https://discuss.hangfire.io/t/what-happens-on-day-31/3779/11 "2017-09-14T21:05:22Z")

</div>

Extended cron syntax (with `L`) will be supported in Hangfire 1.7.0.

For now, you may schedule your job on 28,29,30,31 days of every month, and add an extra check in your job method:

```auto
var today = DateTime.Today;
if (today.Day != DateTime.DaysInMonth(today.Year, today.Month)) return;

```

You may also checkout a dev branch of Hangfire and build it yourself. It should already support extended cron syntax.
