-1

I use bootstrap datepicker. When choosing date, I'd like to add the chosen date +3 excluding weekends (and probably some random date too for holidays) and put it in a textbox. So, if the chosen date is Fri, 25th then the textbox value should be Wed, 30th. This is my current script:

$('.date_get .date').datepicker({
    startView           : 0,
    todayBtn            : "linked",
    forceParse          : false,
    autoclose           : true,
    endDate             : "now",
    format              : "dd/mm/yyyy",
    todayHighlight      : true,
    daysOfWeekDisabled  : [0,6]
}).on('changeDate', function (selected) {

    //DATE 2 = DATE 1 + 3 Days
    var tgl = new Date(selected.date.valueOf() + (1000 * 60 * 60 * 24 * 3)); //Chosen date +3 days

    var hr  = tgl.getDate();
    var bln = tgl.getMonth() + 1; //Januari == 0

    if(bln <10)
    bln = "0"+bln;
    else
    bln = bln;

    var thn      = tgl.getFullYear();
    var hari_ini = hr + "/" + bln + "/" + thn;

    $('.date_tgt').val(hari_ini);
});
ashura91
  • 441
  • 4
  • 17

3 Answers3

0

You need to use setDate function to add three days. Try this

var result = new Date(yourDate);
result.setDate(result.getDate() + 3);
Ajinkya
  • 22,324
  • 33
  • 110
  • 161
Yun
  • 173
  • 1
  • 12
0

You can use moment.js to get day of week in javascript. For example:

var selectedDate = moment(new Date(selected.date.valueOf()); //create moment instance
var dayOfWeek = selectedDate.day(); //will return 0-6, represent sunday-saturday

Then you can make algorithm to adjust your need, something like mapping day of week (0-6) to its added value. Like this:

var addedDays = [3, 3, 3, 5, 5, 5, 4]
//example: 
var selectedDate = moment("2017-01-13"); //friday last week
var resultDate = selectedDate.add(addedDays[selectedDate.Day()], 'days');
//resultDate = 13th (friday) + 5 days = 18th (wednesday)
yahyaman
  • 486
  • 3
  • 11
  • What is addedDays? If the choosen date is Sat 14th, then added 5 days it would be Thu 19th. It should be still Wed 18th – ashura91 Jan 17 '17 at 04:58
  • It's the amount to add for day of week. If we use default value from moment.js, sunday = 0, monday = 1, tuesday = 2, and so on. So, I have array `addedDays` with index related to day of week, containing value of days to be added so that it calculate correctly as you want (+3 day exclude weekend). – yahyaman Jan 17 '17 at 07:15
0

If I understand you correctly you can try this one.

  var date1 = new Date("Jan 20 2017 12:00:00 GMT+0800"); 
  var date2 = new Date(); 
  date2.setDate(date1.getDate()+5); //added 5 days
  var answer= dateDifference(date1,date2); 

  console.log(answer);

 function dateDifference(start, end) {

  // Copy date objects so don't modify originals
  var s = new Date(+start);
  var e = new Date(+end);
  var flag = new Date(+start);
  var numberOfWeekEnds = 0; 

  // Set time to midday to avoid dalight saving and browser quirks
  s.setHours(12,0,0,0);
  e.setHours(12,0,0,0);

  // Get the difference in whole days
  var totalDays = Math.round((e - s) / 8.64e7);

  // Get the difference in whole weeks
  var wholeWeeks = totalDays / 7 | 0;

  // Estimate business days as number of whole weeks * 5
  var days = wholeWeeks * 5;

  // If not even number of weeks, calc remaining weekend days
  var lasta
  if (totalDays % 7) {
    s.setDate(s.getDate() + wholeWeeks * 7);

    while (s < e) {
      s.setDate(s.getDate() + 1);

      // modified
      if (s.getDay() != 0 && s.getDay() != 6) {
        flag.setDate(flag.getDate() + 1 +numberOfWeekEnds);
        numberOfWeekEnds = 0
      }
      else{
        numberOfWeekEnds++;
      }
    }
  }
  return flag;
}

Basically what I did is I modified the function from this answer to check whether a day is weekdays or not.

You can play around date1 and date2 to further check.

Community
  • 1
  • 1
ajbee
  • 3,511
  • 3
  • 29
  • 56