Cron expression that runs for every 2 years - cron

I need to run a particular script starting from Jan 1st of 2019 at 12 AM and next it should run on Jan 1st 2021 at 12 AM, then on Jan 1st 2023 at 12 AM and soon.

You can create and/or check the cron expression format with http://www.cronmaker.com/
0 0 0 1 1 ? 2019/2
This should work. It will run the task on
1. Tuesday, January 1, 2019 12:00 AM
2. Friday, January 1, 2021 12:00 AM
3. Sunday, January 1, 2023 12:00 AM
4. Wednesday, January 1, 2025 12:00 AM
5. Friday, January 1, 2027 12:00 AM

Related

How to calculate days since 1900 generated by excel date?

Excel is converting the date 03/11/2021 to value 44535, which seems to be days since 1900. I´m trying to figure out a way to calculate this using my own golang libs. Does anyone have this kind of problem?
Thank you a lot for your help
A not so fency workaround for this problem would be addDays
d := time.Date(1900, 1, 1, 0, 0, 0, 0, time.UTC)
fmt.Println(d) // 1900-01-01 00:00:00 +0000 UTC
d2 := d.AddDate(0, 0, 44503)
fmt.Println(d2) // 2021-11-05 00:00:00 +0000 UTC
Would print: 05/11/2021 witch is 2 days more than what we desire.
Here we can see the same using JavaScript:
date = new Date(1900, 0, 1)
// Mon Jan 01 1900 00:00:00 GMT-0338 (Amazon Standard Time)
date.setDate(date.getDate() + 44503)
// Fri Nov 05 2021 00:00:00 GMT-0400 (Amazon Standard Time)
After some research about this 2 days I found this in a comment by #chux-reinstate-monica:
If you choose to use MS Excel to check your work note 2 things: 1) Jan 1, 1900 is day 1 (not the number of days since Jan 1, 1900) and 2) according to Excel Feb 29, 1900 exists(a bug in their code they refuse to fix.)
So we can substract 2 days from that to have: 03/11/2021.

Cron Expression for every second Monday of the month (for Hangfire)

I am trying to create recurring job in hangfire that runs, once a month at the second Monday, something like this:
1. Monday, May 14, 2018 8:00 AM
2. Monday, June 11, 2018 8:0 AM
3. Monday, July 9, 2018 8:00 AM
4. Monday, August 13, 2018 8:00 AM
5. Monday, September 10, 2018 8:00 AM
I have found this answer in stackoverflow, but since this is not a standard cron for scheduling hangifre jobs I can not use it.
My question is can I make an expression like this using the format
* * * * * (min hour day/month month day/week)
The following command seems to work for me.
0 8 ? * MON#2
Assuming that you want this job to execute at 8 AM the second Monday of each month, the # character allows you to specify the "nth" day of any given month. We use the ? character in the day/month row since we are fine with any numeric day as long as it is the second Monday.
Read more about special characters here: http://www.quartz-scheduler.org/documentation/quartz-2.2.2/tutorials/crontrigger.html#special-characters
Below are cron for three different time for every 2nd Monday, Look into the pattern and make change in time as per your need day
For each second Monday of the month at 00:00 hrs, try below:
0 0 0 ? 1/1 MON#2 *
For each second Monday of the month at 10:30 hrs, try below:
0 30 10 ? 1/1 MON#2 *
For each second Monday of the month at 13:30 hrs, try below:
0 30 13 ? 1/1 MON#2 *
Here you go.
0 0 12 ? 1/1 MON#2 *
minute hour day month dayofweek command
0 0 8-14 * 2 /path/here
This will run a job every second tuesday of the month at midnight.
8-14 limits the occurance of tuesday to the second week in the month.
1-7 first week
8-14 second week
15-21 third week
22-28 forth week
29-31 fifth week

how to create a cron expression for every 2 weeks

Here is a cron expression that I tried: 0 0 0 */14 * ?. It creates the following schedule:
Start Time:
Friday, September 8, 2017 1:25 AM
Next Times:
Friday, September 15, 2017, 12:00 AM
Friday, September 29, 2017, 12:00 AM
Sunday, October 1, 2017, 12:00 AM
Sunday, October 15, 2017, 12:00 AM
Sunday, October 29, 2017, 12:00 AM
This expression is working for every 2 weeks in every month, but my requirement is it has to run for every 2 weeks. I mean after executing September 29th, the next date should be October 13, but it schedules for October 1.
There is no direct cron expression for every 2 weeks. I use the following cron expression, which is similar to 2 weeks, but not exactly for 2 weeks.
cron expression for every 2 weeks on the 1st and the 15th of every month at 1:30 AM:
30 1 1,15 * *
Friday every two weeks:
0 0 * * Fri [ $(expr $(date +%W) \% 2) -eq 1 ] && /path/to/command
Found on:
https://cron.help/every-2-weeks-on-friday
30 7 1-7,14-21 * 1
“At 07:30 on every day-of-month from 1 through 7 and every day-of-month from 14 through 21 and on Monday.”
You need to specify a start day. Otherwise it's will always reset with the 1st day of the month.
So this expression "0 0 0 23/14 OCT ? 2017" is every 2 weeks starting on October 23rd 2017
The crontab manual on my Ubuntu 18 says:
Note: The day of a command's execution can be specified by two fields — day of month, and day of week. If both fields are restricted (i.e., aren't *), the command will be run when either field matches the current time. For example,
30 4 1,15 * 5 would cause a command to be run at 4:30 am on the 1st and 15th of each month, plus every Friday. One can, however, achieve the desired result
by adding a test to the command (see the last example in EXAMPLE CRON FILE below).
and the mentioned example is:
# Run on every second Saturday of the month
0 4 8-14 * * test $(date +\%u) -eq 6 && echo "2nd Saturday"

Get specific day of week of the upcoming Canada day on Linux

How to use cal command to add the calendar of next July to the end of the file, for example, myfile, and what day of the week the upcoming Canada Day fall on?
So far I just have this command:
cal July 2017 >> myfile
I feel like I am not doing it correct and I don't know which command to use, to find the day of the week for specific date.
Use this command:
cal 7 2017 >> file
The output is:
July 2017
Su Mo Tu We Th Fr Sa
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
You can find out day of week of a particular day with the GNU date command:
date -d"2017-07-01" # what day of week is Canada Day this year?
=> Sat Jul 1 00:00:00 UTC 2017
If you just want the week day, then:
date -d"2017-07-01" +%A
=> Saturday
You can check more about these commands with man cal or man date.
On a Mac, you could do this:
date -j -vJulm -v1d -v2017y +%A
See more on this post: date command on Mac OS

Excel:Next year period

How to get next year period based on current month and year, for example:
Jan 2014 - Dec 2014
Feb 2014 - Jan 2015
Mar 2014 - Feb 2015
Apr 2014 - Mar 2015
May 2014 - Apr 2015
Jun 2014 - May 2015
Jul 2014 - Jun 2015
Aug 2014 - Jul 2015
Sep 2014 - Aug 2015
Oct 2014 - Sep 2015
Nov 2014 - Oct 2015
Dec 2014 - Nov 2015
Next period
Jan 2015 - Dec 2015
Feb 2015 - Jan 2016
etc.
I have tried with the following formula:
=UPPER(TEXT(NOW();"MMM")) &" "& TEXT(NOW();"YY")-1
It works fine for Jan 2014 but can't figure out how to get Dec 2014; Feb 2014 - Jan 2015 and so on?
I think you need the EOMonth formula.
=EOMONTH(NOW(),-13) +1 and =EOMONTH(NOW(),-2) +1 should give give you JAN 2014 to DEC 2014
from the MS Excel documentation
Microsoft Excel stores dates as sequential serial numbers so they can
be used in calculations. By default, January 1, 1900 is serial number
1, and January 1, 2008 is serial number 39448 because it is 39,448
days after January 1, 1900.
To get the text formatting you are after, I would suggest that you stick with formatting the cell/column as #Makyen has suggested. Having said that this is the formula that you can use to format the text.
=UPPER(TEXT(EOMONTH(NOW(),-13) +1, "MMM YY"))
Assuming that the date (as a date serial number) for which you desire to find the year period is in cell A1, the following should provide the next year period starting from that day:
=EOMONTH(A1,11) +DAY(A1) -1
Examples:
Input Output
1/18/2014 1/17/2015
2/18/2014 2/17/2015
3/18/2014 3/17/2015
4/18/2014 4/17/2015
5/18/2014 5/17/2015
6/18/2014 6/17/2015
7/18/2014 7/17/2015
8/18/2014 8/17/2015
9/18/2014 9/17/2015
10/18/2014 10/17/2015
11/18/2014 11/17/2015
12/18/2014 12/17/2015
1/18/2015 1/17/2016
2/18/2015 2/17/2016
3/18/2015 3/17/2016
4/18/2015 4/17/2016
5/18/2015 5/17/2016
6/18/2015 6/17/2016
7/18/2015 7/17/2016
8/18/2015 8/17/2016
9/18/2015 9/17/2016
10/18/2015 10/17/2016
11/18/2015 11/17/2016
12/18/2015 12/17/2016
1/18/2016 1/17/2017
If you want the year period to start from the current day:
=EOMONTH(NOW(),11) + DAY(NOW()) -1
If you want the year period to start from the first day of the current month:
=EOMONTH(EOMONTH(NOW(),-1) + 1,11)
or
=EOMONTH(NOW() - DAY(NOW()) + 1,11)
The EOMONTH() function:
EOMONTH(start_date,months)
Returns the serial number for the last day of the month that is the
indicated number of months before or after start_date. Use EOMONTH to
calculate maturity dates or due dates that fall on the last day of the
month.
If this function is not available, and returns the #NAME? error,
install and load the Analysis ToolPak add-in.

Resources