-1

I need a counter that counts values: Days, e.g. 1, when ordering by 10:30 am, the change should be for 2 days after that time. Of course, the counter must also ignore the weekends.

So, buy on Friday at 14/10/2022 10:00, the date of dispatch should be on 17/10/2022. However, when ordering 14/10/2022 12:00, the shipping date should be on 18/10/2022.

it would be good to count down the time for submission to 10:30

function setShipDate () {
    // element to write date to  
    var shipdate = document.getElementById("date");
    // Set start date to today - end date in 2 days
    var startDate = new Date();
    var endDate = "", noOfDaysToAdd = 1, count = 0;
    // Map numeric month to name  
    var month = new Array();
      month[0] = "01";
      month[1] = "02";
      month[2] = "03";
      month[3] = "04";
      month[4] = "05";
      month[5] = "06";
      month[6] = "07";
      month[7] = "08";
      month[8] = "09";
      month[9] = "10";
      month[10] = "11";
      month[11] = "12";
    while(count < noOfDaysToAdd){
        // add a day to the end date each loop
        endDate = new Date(startDate.setDate(startDate.getDate() + 1));
        // only count when not a weekend
        if(endDate.getDay() != 0 && endDate.getDay() != 6){
           count++;
        }
    }
    // update shipdate HTML
    shipdate.innerHTML= endDate.getDate()  + '.' + month[endDate.getMonth()] + '.'  + endDate.getFullYear();
}
setShipDate();
Scheduled shipment: <span id = "date">

Surely it is possible to simplify this code + add this extra function mentioned above.

<today date> // today's date
<text id = "time"> // how much time I have left for the order with shipment:
<text id = "date"> // the end date according to the schedule until 10:30
Michael M.
  • 10,486
  • 9
  • 18
  • 34
Mateusz
  • 11
  • 3
  • Your question is pretty much a duplicate of this: [How to add days to Date?](https://stackoverflow.com/questions/563406/how-to-add-days-to-date). The only difference is the weekend thing, but `Date.getDay()` solves that. Friday is `5`. Of course, adding 2 days if it's a friday is probably incorrect - you also need to check if the next monday is a holiday. – Tibrogargan Oct 15 '22 at 08:39
  • Don't add requirements in comments, modify your question. You're asking for multiple things in the question - it's not focused and probably won't have much value for other readers. This is not a code writing service. – Tibrogargan Oct 15 '22 at 09:20

1 Answers1

1
var shipdate = document.getElementById("date");

let newDate = new Date();

// GET ORDER TIME
let orderHour = newDate.getHours();
let orderMinute = newDate.getMinutes();
let orderWeekDay = newDate.getDay();

// default delivery time
let deliverAfterDays = 1;

// delivery after weenkend
if (orderWeekDay == 5) {
    deliverAfterDays = 3;
}

// Do deliveriy according to tomorrow's slot
if ((orderHour*60+orderMinute) > 630) {
    deliverAfterDays++;
}

let deliveryDate = new Date(newDate.setDate(newDate.getDate() + deliverAfterDays));

const prefix0 = (n) => {
    if (n <= 9) {
        return "0"+n;
    }
    return n.toString();
}

shipdate.innerHTML= deliveryDate.getDate()  + '.' + prefix0(deliveryDate.getMonth()) + '.'  + deliveryDate.getFullYea();

I think this will solve your problem

Sunil Prajapati
  • 330
  • 2
  • 6