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 )