question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't create schedule with 30 or 31 day of every month

See original GitHub issue

I’m trying to create a new job which should run every 30 or 31st day of month. Although cron strings like '10 10 31 * *', '0 10 10 31 * *' or '* 10 10 30 * *' seem to be correct, I get exception Month '1' is limited to '29' days. which pushes me to explicitly specify months that have 30 or 31 days. My proposal is not to reject cron strings that have at least one correct date in future.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
pincheiracommented, Jul 20, 2018

Using the L alias is not supported as of July 20th 2018, but you can always just do it on the 1st day of the month, with the earliest timezone in the world. At least that works for my case.

const earliestTimezone = 'Pacific/Kiritimati';
const firstDayOfTheMonth = '0 0 1 * *'; // at 00:00
const job = new CronJob({
  cronTime: firstDayOfTheMonth,
  onTick: myFunctionToBeRun,
  start: true,
  timeZone: earliestTimezone
});
3reactions
haifahrulcommented, Jul 1, 2019

Maybe you can use the logic here. You can use package momentJS.

var CronJob = require('cron').CronJob;
var moment = require('moment');

new CronJob('0 00 16 28-31 * *, function() {
  if (isLastDayOfMonth()) {
    doSomeStuff()
  }
}, null, true, 'America/Los_Angeles');

function isLastDayOfMonth(){
  const currentDay  =  moment().format('YYYY-MM-DD');
  const lastDayOfCurrentMonth  =  moment(date).endOf('month').format('YYYY-MM-DD');

  if (currentDate === lastDayOfCurrentMonth) {
    return true;
  } else {
    return false;
  }
}

function doSomeStuff(){
  // your task here.
}

The cron will be run every 28 - 31 day, and then check whether current day is a last day of current month.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Create an all-day event - Microsoft Support
Create an event. In the Calendar, select New Appointment. You can also select New Items > All Day Event. Enter a description in...
Read more >
How to set up repeating event in Google Calendar on last day ...
Click on your calendar and you can verify that your repeating event now occurs on the last day of each month. You can...
Read more >
Calendar recurrences on the last day of the month
In Google Calendar, scheduling an appointment on the 31st of every month means it will only appear on those months that have 31...
Read more >
Solved: Scheduled script to run on Last day of every month...
Yes, setting the run date to the 31st at 00:00:00 will run the job on the last day of every month at 00:00:00...
Read more >
Why Don't All Months Have the Same Number of Days?
Unfortunately, the lunar cycle is approximately 29.5 days, which does not divide ... Roman calendar in 46 B.C. to make each month have...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found