从日期中减去 3 天

Subtracting 3 days from date

我想通过减去 3 天来计算 reminderDate。但是,如果结果日期

例如

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

我试过类似的方法,但 .getDay().day() 似乎都不起作用。 dt 似乎给出了今天的日期,而不是 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);

感谢任何帮助!

如果您使用的是 momentjs,则无需切换到本机 Date 对象,因为您可以使用 momentjs 做任何事情,而且非常简单

使用 momentjs day() 帮助您获取一周中某一天的序号

0 - 周日 1 - 星期一 ... .. . 6 - 星期六

要查找本周的“星期六”日期,您可以像 moment().day("Saturday") 那样操作。 然后是 subtract,您已经在使用它来按给定日期倒回日期。

基于以上这些想法,您可以尝试这个辅助函数

<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>

用普通 javascript 做到这一点并不难。与其减去 3 天,看看今天是多少天,然后再减去,您可以计算出要减去的天数,给定初始日期并进行一次减法,例如

// 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()));