0

How to add +1 if the shipout from or to day is SUNDAY?

<div class="shipout">
<p id="postage">Postage: <span id="postage-msg">FREE (UK MAINLAND)</span></p>
<span id="shipout-title"></span><span id="shipout-date"></span>
<script>
Date.prototype.addDays = function(days) {
  var dat = new Date(this.valueOf());
  locale = "en-us";
  dat.setDate(dat.getDate() + days);
  var curr_date = dat.getDate();
  var s=["th","st","nd","rd"];
  v= curr_date %100;
  var curr_month = dat.toLocaleString(locale, { month: "long" });
  return curr_date + (s[(v-20)%10]||s[v]||s[0]) + " " + curr_month + " " + "(" + ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'][dat.getDay()] +")";
}

var dat = new Date();

document.getElementById("shipout-title").innerHTML = "Estimated Delivery: "
document.getElementById("shipout-date").innerHTML =  dat.addDays(4) + " - " + dat.addDays(5)

</script>
</div>

As of current Script the Output will be Estimated Delivery: 25th March (Sat) - 26th March (Sun) If today is 21st March Tuesday.

I need the output like Estimated Delivery: 25th March (Sat) - 27th March (Mon) If today is 21st March Tuesday.

Also

As of current Script the Output will be Estimated Delivery: 26th March (Sun) - 27th March (Mon) If today is 22nd March Wednesday.

I need the output like Estimated Delivery: 27th March (Mon) - 28th March (Tue) If today is 22nd March Wednesday.

1

Modified your code to remove Sunday from Days.

    <div class="shipout">
<p id="postage">Postage: <span id="postage-msg">FREE (UK MAINLAND)</span></p>
<span id="shipout-title"></span><span id="shipout-date"></span>
<script>
var flag = false;;
Date.prototype.addDays = function(days) {
  if(flag == true){
    days = days + 1;
    flag = false;
  }
  var dat = new Date(this.valueOf());
  locale = "en-us";
  dat.setDate(24 + days);
  var curr_date = dat.getDate();

//Changes Start
  var day = dat.getDay();
  if(dat.toLocaleString(locale, { weekday: "long" }) == 'Sunday'){
    curr_date = dat.getDate() + 1;
    day = day + 1;
    flag = true;
  }

//Changes End
  var s=["th","st","nd","rd"];
  v= curr_date %100;

  var curr_month = dat.toLocaleString(locale, { month: "long" });
  return curr_date + (s[(v-20)%10]||s[v]||s[0]) + " " + curr_month + " " + "(" + ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'][day] +")";
}

var dat = new Date();

document.getElementById("shipout-title").innerHTML = "Estimated Delivery: "
document.getElementById("shipout-date").innerHTML =  dat.addDays(2) + " - " + dat.addDays(3)

</script>
</div>

I Updated my code. Hope it helps.

Hope it helps.

| improve this answer | |
  • Thanks for the code. but it will be "Estimated Delivery: 27th March (Mon) - 27th March (Mon)" if today is 25th and "document.getElementById("shipout-date").innerHTML = dat.addDays(2) + " - " + dat.addDays(3)" – Doodu Mar 21 '17 at 20:57
  • is showing me 27th and 28th if today's date is 25th. working fine for me. Check attached screen shot in my answer. Clear cache and check it again. Try your code here. w3schools.com/js/tryit.asp?filename=tryjs_date_setdate – Abhishek Panchal Mar 21 '17 at 21:16
  • Posted script and the shared image has different scripts. image has dat.setDate(25 + days); instead of "dat.setDate(dat.getDate() + days);" – Doodu Mar 21 '17 at 22:03
  • I have edited my question. Updated the line "document.getElementById("shipout-date").innerHTML = dat.addDays(4) + " - " + dat.addDays(5)" – Doodu Mar 21 '17 at 22:11
  • Image script is different just because to get 25th date as you requested for 2nd(27 march) and 3rd(28 day) day. – Abhishek Panchal Mar 21 '17 at 22:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.