Home > front end >  Responsive Daterangepicker
Responsive Daterangepicker

Time:09-07

$(function() {
  $('input[name="daterange"]').daterangepicker({
    opens: 'left'
  }, function(start, end, label) {
    console.log("A new date selection was made: "   start.format('YYYY-MM-DD')   ' to '   end.format('YYYY-MM-DD'));
  });
});
</script>
<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>
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/daterangepicker/daterangepicker.css" />

<input type="text" name="daterange" value="01/01/2018 - 01/15/2018" />

I need to make responsive calendar in desktop 2 months but in tablet and mobile display 1 months.

CodePudding user response:

You can use singleDatePicker for decide if use 1/2 months and use a function for detect if is mobile (i used a simple code for example):

$(function() {
  let isMobile = (window.matchMedia("(max-width: 767px)").matches) ? true : false;
  $('input[name="daterange"]').daterangepicker({
    opens: 'left',
    singleDatePicker: isMobile,
  }, function(start, end, label) {
    console.log("A new date selection was made: "   start.format('YYYY-MM-DD')   ' to '   end.format('YYYY-MM-DD'));
  });
}); 
<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>
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/daterangepicker/daterangepicker.css" />

<input type="text" name="daterange" value="01/01/2018 - 01/15/2018" />

  • Related