Single calendar Date Range Picker

北城以北 提交于 2020-01-11 11:11:32

问题


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> 


来源:https://stackoverflow.com/questions/36722158/single-calendar-date-range-picker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!