Home > Net >  Javascript: Substracting dates works and adding them does not work
Javascript: Substracting dates works and adding them does not work

Time:12-23

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(
  "           
  • Related