This code substracts dates:
const moment = require("moment");
const currentDate = new Date();
// #NOTE:
// By midnight, I mean at the end of the current day
const day_to_add = +1 * 24 * 60 * 60 * 1000;
const current_day_at_midnight_date = new Date(
currentDate.setHours(0, 0, 0, 0) + day_to_add
);
const twenty_eight_days_to_remove = 27 * 24 * 60 * 60 * 1000;
const twenty_eight_days_ago_date = new Date(
currentDate - twenty_eight_days_to_remove
);
const formatted_first_day = moment(current_day_at_midnight_date).format(
"YYYY-MM-D"
);
const formatted_last_day = moment(twenty_eight_days_ago_date).format(
"YYYY-MM-D"
);
console.log(
"🚀 ~ file: add_remove_date.js ~ line 17 ~ formatted_first_day",
formatted_first_day
);
console.log(
"🚀 ~ file: add_remove_date.js ~ line 21 ~ formatted_last_day",
formatted_last_day
);
This is the result:
🚀 ~ file: add_remove_date.js ~ line 17 ~ formatted_first_day 2021-12-23
🚀 ~ file: add_remove_date.js ~ line 21 ~ formatted_last_day 2021-11-25
As you can see, it substracted 28 days as expected.
This code adds dates:
const moment = require("moment");
const currentDate = new Date();
// #NOTE:
// By midnight, I mean at the end of the current day
const day_to_add = +1 * 24 * 60 * 60 * 1000;
const current_day_at_midnight_date = new Date(
currentDate.setHours(0, 0, 0, 0) + day_to_add
);
const twenty_eight_days_to_add = 27 * 24 * 60 * 60 * 1000;
const twenty_eight_days_later_date = new Date(
currentDate + twenty_eight_days_to_add
);
const formatted_first_day = moment(current_day_at_midnight_date).format(
"YYYY-MM-D"
);
const formatted_last_day = moment(twenty_eight_days_later_date).format(
"YYYY-MM-D"
);
console.log(
"🚀 ~ file: add_remove_date.js ~ line 17 ~ formatted_first_day",
formatted_first_day
);
console.log(
"🚀 ~ file: add_remove_date.js ~ line 21 ~ formatted_last_day",
formatted_last_day
);
which is basically the same code. I just replaced - with +.
But, it doesn't work. This is the result:
🚀 ~ file: add_remove_date.js ~ line 17 ~ formatted_first_day 2021-12-23
🚀 ~ file: add_remove_date.js ~ line 21 ~ formatted_last_day 2021-12-22
For some weird reason, the result formatted_last_day of the addition is always one day before formatted_first_day.
Any idea what's happening here?
Dates are objects. If you use operators on them, they are coerced to some other type that the operator supports. In JavaScript, strings are the first choice here, then numbers, so...
+ => coerced to string- => coerced to number (- isn't supported with strings)So, date + 1 is equivalent to date.toString() + 1, but date - 1 is equivalent to date.valueOf() - 1.
You probably want to get the timestamp as number in both occasions, which you can do by manually calling .valueOf() on it (date.valueOf() + something) or by using unary plus which also works only with numbers (+date + something).
As commenter Andreas pointed out: While this is the answer to the particular problem and misunderstanding you had, an overall better approach would be to use moment's functions for that, seeing that you already use the library anyway: moment().add(1, "d").startOf("day") for tomorrow 0:00 and moment().add(28, "d") for same time of day in 4 weeks.