-->

Single calendar Date Range Picker

2019-02-27 04:17发布

问题:

I am looking to build a date range picker that's values only consist of January-December of a single year at a time.It will look something like this:

(This is taken from my existing kendo date range picker and poorly photoshopped)

I have built one using the kendo date picker but it seems to require two calendars, one for start date and the other for end date. Being that my range can only span months in a single year, I just need one calendar like below where the user can either click and drag for the desired range, or they can just click two dates. Does anyone know of a date range picker that already functions this way? Or if there is a way to alter the kendo one to do the same?

Thanks.

Edit: Here is my code for my existing date range picker:

            var start = $("#StartDate").kendoDatePicker({
                value: LastMonthString,
                format: "MM/yyyy",
                start: 'year',
                depth: 'year',
                change: startChange,
                open: function() {
                    $('#StartCalendar').append($('#StartDate_dateview'));
                },
                close: function(e) {
                    e.preventDefault();
                }
            }).data("kendoDatePicker");

            var end = $("#EndDate").kendoDatePicker({
                value: TodaysDateString,
                format: "MM/yyyy",
                start: 'year',
                depth: 'year',
                change: endChange,
                open: function() {
                    $('#EndCalendar').append($('#EndDate_dateview'));
                },
                close: function(e) {
                    e.preventDefault();
                }
            }).data("kendoDatePicker");

            start.max(end.value());
            end.min(start.value());
            $("#StartDate").attr("readonly", true);
            $("#EndDate").attr("readonly", true);

            start.open();
            end.open();

回答1:

This is how I've made it to show years only but you can change the format obviously

$('#sandbox-container').datepicker({ 
  format: "yyyy", 
  startView: 1, 
  viewMode: "years", 
  minViewMode: "years", 
  minViewMode: "years", 
  multidate: true, 
  multidateSeparator: ", ", 
  autoClose: true, 
}).on("changeDate",function(event){ 
  var dates = event.dates, elem = $('#sandbox-container'); 
  if(elem.data("selecteddates") == dates.join(",")) return; //To prevernt recursive call, that lead to lead the maximum stack in the browser. 
  if(dates.length>2) dates=dates.splice(dates.length-1); 
  dates.sort(function(a,b){return new Date(a).getTime() - new Date(b).getTime()}); 
  elem.data("selecteddates",dates.join(",")).datepicker('setDates', dates); 
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.7.1/css/bootstrap-datepicker.min.css" rel="stylesheet" type="text/css" media="screen">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.7.1/js/bootstrap-datepicker.min.js"></script>

<div id="sandbox-container">
<input type="text" id="date" value="">
</div>