Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

248
Views
Javascript: Substracting dates works and adding them does not work

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?

about 4 years ago ยท Santiago Trujillo
1 answers
Answer question

0

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.

about 4 years ago ยท Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
ยฉ 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!