Skip to content

Instantly share code, notes, and snippets.

@magician11
Last active May 1, 2018 17:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save magician11/cc47a7beab074896e11642370b0f5094 to your computer and use it in GitHub Desktop.
Save magician11/cc47a7beab074896e11642370b0f5094 to your computer and use it in GitHub Desktop.
How to get a Node.js function to run once on the last day of each month
const moment = require('moment');
const someAction = () => console.log('Actioning...');
const wait = ms => new Promise((resolve, reject) => setTimeout(resolve, ms));
const startCron = async () => {
while (true) {
if (
moment().date() ===
moment()
.endOf('month')
.date()
) {
someAction();
}
await wait(moment.duration(1, 'day').asMilliseconds());
}
};
console.log('Starting cron service...');
startCron();
@magician11
Copy link
Author

magician11 commented May 1, 2018

For more information on the the moment library, have a look at https://momentjs.com/

A walkthrough of this gist is here https://youtu.be/xOr6KDA-jXw

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment