kuchar kuchar - 4 months ago 24
jQuery Question

Bootstrap datepicker - Pick 5 days without weeks

Hello i want to make datepicker with auto select five days without weeks.
If user pick for example 27.07.2016 for the start date i want to select 5 days from it 28.07, 29.07, 01.08, 02.08, 03.08
If it is posible i want to display in input 27.08.2016 - 03.08.2016

So far i did something like this:
Demo example
http://jsfiddle.net/kuchar/hrzakorh/#&togetherjs=t3vznRU6eM

var date = new Date();
var today = new Date(date.getFullYear(), date.getMonth(), date.getDate());
var todaytime = new Date().toLocaleTimeString('pl-PL', {
hour12: false,
hour: "numeric",
minute: "numeric"
});
if (todaytime <= '12:00:00'){
$('#date-picker input').datepicker({
language: "pl",
multidateSeparator: " - ",
maxViewMode: 0,
multidate: true,
daysOfWeekDisabled: "0,6",
defaultDate: 1,
startDate: '+1d',
});
}
if (todaytime >= '12:00:00'){
$('#date-picker input').datepicker({
language: "pl",
multidateSeparator: " - ",
maxViewMode: 0,
multidate: true,
daysOfWeekDisabled: "0,6",
defaultDate: 1,
startDate: '+2d',
});
}


Calendar is on input i called "Wybierz datÄ™".

Edu Edu
Answer

An aproximation should be...

function getDayAfterExcludingWeekEnds(dateParam,dayNum){
  var newDate = new Date( dateParam.getFullYear(), dateParam.getMonth(), dateParam.getDate() + dayNum);
  if (newDate.getDay() == 6){
    return getDayAfterExcludingWeekEnds( newDate, 2);
  }
  if (newDate.getDay == 0){
    return getDayAfterExcludingWeekEnds( newDate, 1);
  }
  return newDate;
}
$("#select").change(function() {
    var count = parseInt($(this).val().replace("ppl", ""));
    $("#npersons").html("");
    $(".dataTable").hide();
    for (var i = 0; i < count; i++) {
      var $row = $("#food").clone().prop("name", "foods" + i).show();
      $row.find("td").eq(0).html(i + 1);
      $("#persons #food").removeAttr('required');
      $("#npersons").append($row);
    }
    if (count > 0) {
      $(".dataTable").show();
    }
    });

  var date = new Date();
  var today = new Date(date.getFullYear(), date.getMonth(), date.getDate());
  var todaytime = new Date().toLocaleTimeString('pl-PL', {
    hour12: false,
    hour: "numeric",
    minute: "numeric"
  }),
  startDate = '+1d';
  if (todaytime > '12:00:00'){
    startDate = '+2d';
  }
  $('#date-picker input').datepicker({
    language: "pl",
    multidateSeparator: " - ",
    maxViewMode: 0,
    multidate: true,
    daysOfWeekDisabled: "0,6",
    defaultDate: 1,
    startDate: startDate 
  }).on('changeDate', function( event ) {
          if( event.dates.length > 1 && event.dates.length < 6){
            return;
          }
          var selectedDate = event.date,
          selectedDateAfterOne = getDayAfterExcludingWeekEnds( selectedDate, 1),
          selectedDateAfterTwo = getDayAfterExcludingWeekEnds( selectedDateAfterOne, 1),
          selectedDateAfterTree = getDayAfterExcludingWeekEnds( selectedDateAfterTwo, 1),
          selectedDateAfterFour = getDayAfterExcludingWeekEnds( selectedDateAfterTree, 1),
          dates = [selectedDate,selectedDateAfterOne,selectedDateAfterTwo,selectedDateAfterTree,selectedDateAfterFour];
          $('#date-picker input').datepicker('setDates',dates);
        });

Good luck

Comments