0

So I'm troubleshooting with some JavaScript dates. I'm working with NodeJS Mongoose and React. I'd like to update all dates in database, but I'd like to do that every weekend and keep hours, don't change them at all.

Let say I have day like 22 January 2020, and during weekend date will update itself to 29 of January and then 5 of February. Everything in database is save like ISODate("2020-01-16T16:27:15.003Z") and I have a code to update those dates whenever I want. I'm having trouble figure out how to body of setDate() should look like to automatically change months and days while keeping the same hour everytime - so 22/01/2020 4:00 PM during weekend will change to 29/01/2020 4:00PM.

I've already tried to use momentjs to handle dates but it doesn't work with my database.

cron.schedule("* * * * * *",async function() {
                const courses = await Course.find({});
                courses.forEach(course => {
                    const newDate = () => {
                        let date = new Date();
                        return date.toISOString();
                    };
                    Course.updateMany({
                        "nextClasses": course.nextClasses === course.startingDate ? course.startingDate :
                            course.nextClasses
                    },{$set: {"nextClasses": newDate()}},(err) => console.log(err))
                });
            }

That's the code responsible for changing dates, for now it changes everything to current date every second ( on purpose, for validation purposes )

Marcin
  • 102
  • 1
  • 2
  • 9
  • Does this answer your question? [How to run crontab job every week on Sunday](https://stackoverflow.com/questions/16717930/how-to-run-crontab-job-every-week-on-sunday) – michaelitoh Jan 22 '20 at 14:42
  • Thanks for replay. No, it doesn't. I know how to use cron to update in desired day/hour, but I don't know how to change day and month keeping year and hour the same in ``setDate()`` method. I don't know how body of ``setDate()`` should look – Marcin Jan 22 '20 at 15:56
  • Have you used Moment library in js? – User123456 Jan 23 '20 at 05:22
  • 1
    @User123456 Yes, I've tried to. Problem solved by solution below – Marcin Jan 23 '20 at 09:09

1 Answers1

2

This would add 7 days to all dates every Sunday.

const addDays = (date, days) => {
    const result = new Date(date);
    result.setDate(result.getDate() + days);
    return result;
};

cron.schedule("0 0 * * 0", async function() {
    const courses = await Course.find({});
    courses.forEach(async (course) => {
        await course.update(
            {
                $set: {
                    nextClasses: addDays(course.nextClasses, 7)
                }
            },
            (err) => console.log(err)
        );
    });
});

Alternate method

cron.schedule("0 0 * * 0", async function() {
    const courses = await Course.find({});
    courses.forEach(async (course) => {
        course.nextClasses.setDate(course.nextClasses.getDate() + 7);
        course.markModified("nextClasses"); /* Mongoose does not track changes made by built-in Date methods */
        await course.save((err) => console.log(err));
    });
});
Brahma Dev
  • 1,955
  • 1
  • 12
  • 18
  • Hi @Brahma-Dev thanks for your answer. How can I manage those months and hours? I mean thank you very much for day changing but what about situation when I hit the end of month? How can I detect, If that day is the last "Tuesday" of month and next Tuesday would be in next month ? I'm trying to figure out how can I change months automatically as well. – Marcin Jan 22 '20 at 21:41
  • Since the OP is storing values as UTC, this should use UTC methods to get and set the date. Otherwise daylight saving settings of the host system may affect the time. – RobG Jan 22 '20 at 22:13
  • @Marcin It will work across months. It's adding 7 days to it, it will change month and year accordingly. – Brahma Dev Jan 23 '20 at 05:03
  • @RobG ISODate is part of MongoDB. Mongoose manages the conversion between Javascript's Date and Mongo's ISODate. – Brahma Dev Jan 23 '20 at 05:07