Skip to content

Instantly share code, notes, and snippets.

@magician11

magician11/nodejs-cron.js

Last active May 1, 2018
Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

@magician11 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
You can’t perform that action at this time.