Subtracting 3 days from date

557 Views Asked by At

I want to calculate a reminderDate by subtracting 3 days from it. However, if the resulting date

  • falls on a Saturday or Sunday, it should give the date of the Friday
  • falls on a Friday, it should give the date of Thursday

For example

Exchange Date                             ReminderDate

18.06.2020                -3 days =       15.06.2020 --> OK, because Monday
17.06.2020                -3 days =       14.06.2020 --> Sunday, must be changed to 12.06.2020 
16.06.2020                -3 days =       13.06.2020 --> Saturday, must be changed to 12.06.2020 
15.06.2020                -3 days =       11.06.2020 --> Friday, must be changed to 11.06.2020

I tried something like this, but neither .getDay() nor .day() seem to work. And dt seems to give the date of today, and not the date of exchange.

var exchange = NWF$("#" + varAustauschtermin).val(); // date like 18.06.2020
console.log("Exchange: " + exchange);
var reminderDate = moment(exchange, "DD.MM.YYYY").format("DD.MM.YYYY");
var dt = new Date(reminderDate);
// var reminderDate = moment(exchange, "DD.MM.YYYY").subtract(3, 'days').format("DD.MM.YYYY");

// console.log("reminderDate.day(): " + reminderDate.day()); 
// console.log("reminderDate.getDay(): " + reminderDate.getDay());

if(dt.getDay() == 6) { // Saturday
    console.log("Saturday");
    reminderDate = moment(exchange, "DD.MM.YYYY").subtract(1, 'days').format("DD.MM.YYYY");
} else if (dt.getDay() == 0) { // Sunday
    console.log("Sunday");
    reminderDate = moment(exchange, "DD.MM.YYYY").subtract(2, 'days').format("DD.MM.YYYY");
} else if (dt.getDay() == 5) { // Friday
    console.log("Friday");
    reminderDate = moment(exchange, "DD.MM.YYYY").subtract(1, 'days').format("DD.MM.YYYY");
} else {
    console.log("Weekday");
    reminderDate = moment(exchange, "DD.MM.YYYY").subtract(3, 'days').format("DD.MM.YYYY");
}
console.log("Reminder Date: " + reminderDate);

Any help is appreciated!

2

There are 2 best solutions below

0
On BEST ANSWER

If you are using momentjs then there is no need to switch to native Date object because everything you can do with momentjs and with much simplicity

Use momentjs day() to help you get the oridinal for a day of week

0 - Sunday 1 - Monday ... .. . 6 - Saturday

To find what date will say "Saturday" come in this week you can do like moment().day("Saturday"). Then there is subtract which you are already using to rewind dates by given days.

Building on these above ideas you can try this helper function

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.0/moment.min.js"></script>

<script type="text/javascript">
  function dateShift(d) {
    //days numbering are 0(Sunday) to 6(Saturday)

    d.subtract(3, 'days')
    
    //Is SUNDAY?
    if (d.day() == 0) {
      //adjust it to friday
      var upComingFri = d.day('Friday'); // date of friday in which THIS sunday is
      return upComingFri.subtract(7, 'days'); 
      //but we want to rewind as you want to stay in same week as the original date provided
    }

    //Is SATURDAY?
    if (d.day() == 6) {
      //adjust it to friday
      var friday = d.day('Friday'); //sat is in same week 
      return friday;
    }

    //Is FRIDAY?
    if (d.day() == 5) {
      //adjust it to thursday
      var thursday = d.day('Thursday');
      return thursday;
    }

    return d;
  }


  t1 = moment('18.06.2020', "DD.MM.YYYY");
  r1 = dateShift(t1);
  console.log(r1.format("DD.MM.YYYY"))

  t2 = moment('17.06.2020', "DD.MM.YYYY");
  r2 = dateShift(t2);
  console.log(r2.format("DD.MM.YYYY"))

  t3 = moment('16.06.2020', "DD.MM.YYYY");
  r3 = dateShift(t3);
  console.log(r3.format("DD.MM.YYYY"))

  t4 = moment('15.06.2020', "DD.MM.YYYY");
  r4 = dateShift(t4);
  console.log(r4.format("DD.MM.YYYY"))
</script>

0
On

It's not too hard to do this with plain javascript. Instead of subtracting 3 days, seeing what the day is, then subtracting again, you can work out the days to subtract given the initial day and do one subtraction, e.g.

// Parse string in format d.m.y to Date
function parseDMY(s) {
  let [d, m, y] = s.split(/\D/);
  return new Date(y, --m, d);
}

// s is date in format d.m.y
function adjustDate(s) {
  let d = parseDMY(s);
  // Subtract 4 days from Mon and Tue, 5 from Wed, 3 otherwise
  d.setDate(d.getDate() - ([,4,4,5][d.getDay()] || 3));
  return d;
}

['29.03.2020','28.03.2020','27.03.2020','26.03.2020',
 '01.04.2020','31.03.2020','30.03.2020'
].forEach(s => console.log(parseDMY(s).toDateString() + ' -> ' + 
                           adjustDate(s).toDateString()));