Home > other >  I have to update a variable at the start of every month in javaScript
I have to update a variable at the start of every month in javaScript

Time:07-28

For Example: On the 1st of August 2022 at 12:00 AM the variable should increase by 1. The variable is stored in MongoDB, this is for a mern stack project. How should I go about this?

CodePudding user response:

I would suggest to use some cron jobs, which can update what you want in given time. e.g. package: https://www.npmjs.com/package/node-cron

Set it to 0 12 1 * * which represents invoking your function 12:00 AM every first day of month

CodePudding user response:

I wouldn't increase the value by one on the first of each month. I instead would use a field containing the startDate as a reference and use that one to calculate the number of months between the startDate and the current date.

You can use that compute result directly or store it in an additional field.

You actually always need to use a reference date. Even if you increment it every month because you need to check if your increment script was actually executed.

  • Related