Search code examples
twitter-bootstrapdatepickerdatetimepickerbootstrap-datetimepicker

how to select entire row in bootstrap date picker within the month


I want to select the entire row within the month.

let me explain my task.

for example:august 2017 start in Tuesday , I want to show the date of Tuesday to Saturday in the text box.

If the user select 28 th of august 2017 means I want to show the date only Sunday to Thursday in the text box. similarly I want to get the result by every month.

Here I have created fiddle Click here to see the fiddle

   var firstDate = moment(value, "DD-MM-YYYY").day(0).format("DD-MM-YYYY");
      var lastDate =  moment(value, "DD-MM-YYYY").day(6).format("DD-MM-YYYY");

I don't know how to do this. can anyone tell me is this possible to achieve this and give some tips to achieve this.

Thanks

vinoth


Solution

  • Try this code

    compare first date of row with month first date && last date of row with month last date.

      $('#weeklyDatePicker').on('dp.change', function (e) {
          var _year=e.date.year(),
          _month=e.date.month(),
          _date=e.date.date(),
          _day=e.date.day(),
          monthFirstDate = new Date(_year, _month, 1),
          monthLastDate = new Date(_year, _month + 1, 0),
          firstDate = new Date(_year, _month, _date - _day),
          lastDate = new Date(_year, _month, _date - _day + 6),
          fromDate=(firstDate<=monthFirstDate)?monthFirstDate:firstDate,
          toDate=(lastDate>=monthLastDate)?monthLastDate:lastDate,
          fromDateFormated=moment(fromDate).format("DD-MM-YYYY"),
          toDateFormated=moment(toDate).format("DD-MM-YYYY");
          $("#weeklyDatePicker").val(fromDateFormated);
          $("#weeklyDatePickerend").val(toDateFormated);
      });
    

      
    $(document).ready(function(){
    
    
      $("#weeklyDatePicker").datetimepicker({
          format: 'DD-MM-YYYY',
      });
      $('#weeklyDatePicker').on('dp.change', function (e) {
          var _year=e.date.year(),
          _month=e.date.month(),
          _date=e.date.date(),
          _day=e.date.day(),
          monthFirstDate = new Date(_year, _month, 1),
          monthLastDate = new Date(_year, _month + 1, 0),
          firstDate = new Date(_year, _month, _date - _day),
          lastDate = new Date(_year, _month, _date - _day + 6),
          fromDate=(firstDate<=monthFirstDate)?monthFirstDate:firstDate,
          toDate=(lastDate>=monthLastDate)?monthLastDate:lastDate,
          fromDateFormated=moment(fromDate).format("DD-MM-YYYY"),
          toDateFormated=moment(toDate).format("DD-MM-YYYY");
          $("#weeklyDatePicker").val(fromDateFormated);
          $("#weeklyDatePickerend").val(toDateFormated);
      });
    });
    .bootstrap-datetimepicker-widget tr:hover {
        background-color: #0d75b3;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
    <script src="https://cdn.jsdelivr.net/momentjs/2.10.6/moment.min.js"></script>
    <script src="https://cdn.jsdelivr.net/bootstrap.datetimepicker/4.17.37/js/bootstrap-datetimepicker.min.js"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.37/css/bootstrap-datetimepicker.css">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
    <div class="container">    
        <div class="row">
            <div class="col-sm-6 form-group">
                <div class="input-group" id="DateDemo">
                  From Date:<input type='text' id='weeklyDatePicker' placeholder="Select Week" />
                  <br>
                       To Date:<input type='text' id='weeklyDatePickerend' placeholder="Select Week" />
              </div>
          </div>
      </div>
    </div>