make the default single date picker date

Issue

I have a case where I use the daterangepicker library to retrieve the date, here is the jsfiddle link: https://jsfiddle.net/heriira_/0bgqxdLa/5/

$(function () {
   var start = moment().subtract(1, "days");

   $("#demo").daterangepicker(
    {
     singleDatePicker: true,
     startDate: start,
    },
   function (start, end, label) {
    console.log(start.format("YYYY-MM-DD"));
   }
  );
});

in the code above how to make a handle if, for example, the default value is yesterday, and when we choose a time that is more than yesterday, for example two days before it will return to the default, which is yesterday’s date.

Solution

based on the documentation https://www.daterangepicker.com/#options

you just need to add minDate:
so the date before yesterday can’t be selected

$(function() {
  var start = moment().subtract(1, "days");

  $("#demo").daterangepicker({
      singleDatePicker: true,
      startDate: start,
      minDate: start
    },
    function(start, end, label) {
      console.log(start.format("YYYY-MM-DD"));
    }
  );
});
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/daterangepicker/daterangepicker.css" />
<script type="text/javascript" src="https://cdn.jsdelivr.net/jquery/latest/jquery.min.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/momentjs/latest/moment.min.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/daterangepicker/daterangepicker.min.js"></script>

<input type="text" id="demo" value="10/24/1984" />

Answered By – Atmahadli

Answer Checked By – Candace Johnson (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.