How do I get Date Range Picker selected date to a

2020-07-22 18:54发布

问题:

hi i am using data range picker for filter option. there is change with default date picker range is . here i am using a div instead of text box. so tthat i need the selected start date and end date in a variable how can i due it?. i try like this way...

 $('#Date').daterangepicker();
  $(document).on("click",".applyBtn",function() {
//         var range = $('#Date').datarangepicker.getRange(); 
// var startDate = range.start;
// var endDate = range.end;
 var x =$('#Date').data('daterangepicker').StartDate()
  alert(x);
  });
<!-- Include Required Prerequisites -->
<script type="text/javascript" src="//cdn.jsdelivr.net/jquery/1/jquery.min.js"></script>
<script type="text/javascript" src="//cdn.jsdelivr.net/momentjs/latest/moment.min.js"></script>
<link rel="stylesheet" type="text/css" href="//cdn.jsdelivr.net/bootstrap/3/css/bootstrap.css" />
 
<!-- Include Date Range Picker -->
<script type="text/javascript" src="//cdn.jsdelivr.net/bootstrap.daterangepicker/2/daterangepicker.js"></script>
<link rel="stylesheet" type="text/css" href="//cdn.jsdelivr.net/bootstrap.daterangepicker/2/daterangepicker.css" />


<div id="Date" class="col-xs-5 col-md-4 paddingNull filterImageAlign" >here select</div>

回答1:

Try this.

var startDate = $('#Date').data('daterangepicker').startDate._d;
var endDate = $('#Date').data('daterangepicker').endDate._d;


回答2:

If you need to get it formatted locally

$('#Date').daterangepicker().on('apply.daterangepicker', function (e, picker) {
    var startDate = picker.startDate.format('DD-MM-YYYY');
    var endDate = picker.endDate.format('HH:mm');
})