简体   繁体   English

从日期减去 3 天

[英]Subtracting 3 days from date

I want to calculate a reminderDate by subtracting 3 days from it.我想通过从中减去 3 天来计算reminderDate日期。 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.我尝试过这样的事情,但.getDay().day()似乎都不起作用。 And dt seems to give the date of today, and not the date of exchange .而且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);

Any help is appreciated!任何帮助表示赞赏!

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如果您使用的是 momentjs,则无需切换到本机 Date object,因为您可以使用 momentjs 并且非常简单

Use momentjs day() to help you get the oridinal for a day of week使用 momentjs day()来帮助您获取一周中某一天的序号

0 - Sunday 1 - Monday... .. . 0 - 周日 1 - 周一...... 6 - Saturday 6 - 星期六

To find what date will say "Saturday" come in this week you can do like moment().day("Saturday") .要查找本周会显示“星期六”的日期,您可以执行类似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基于以上这些想法,您可以尝试这个助手 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>

It's not too hard to do this with plain javascript.用普通的 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, eg与其减去 3 天,看看那一天是什么,然后再减去,您可以计算出在第一天减去的天数并进行一次减法,例如

 // Parse string in format dmy to Date function parseDMY(s) { let [d, m, y] = s.split(/\D/); return new Date(y, --m, d); } // s is date in format dmy 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()));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM