【问题标题】:jQuery Datepicker - issue with start and end dates classesjQuery Datepicker - 开始和结束日期类的问题
【发布时间】:2021-07-16 08:59:48
【问题描述】:

最初我正在寻找在jQuery Datepicker 下为开始和结束日期生成一些类的解决方案,并在这篇帖子https://stackoverflow.com/a/67191802/14723575 下给了我解决方案

但仅当开始日期和结束日期在同一个月内时才有效。如果我需要滑到下个月以选择结束日期,则这两个建议的代码都存在问题。例如,如果我选择 4 月 24 日作为开始日期并选择 5 月 6 日作为结束日期,它会在当月的最后一天刹车。

我有建议为这个问题打开单独的票,所以我们开始吧。

【问题讨论】:

  • 我已经发布了新代码,如果问题再次存在,请告诉我..
  • 比你先生。这非常有效。

标签: jquery datepicker date-range


【解决方案1】:

我已修改代码以适应您的问题。如您所见,代码必须测试很多不同的情况,我想我没有忘记...

我已经把代码放在sn-p里了,因为有很多行代码,但是sn-p不起作用...

$(function () {
  var dates_selected = [];
  var firstdate_present = false;//is firstdate present in the current display?
  var lastdate_present = false;//is lastdate present in the current display?

  $(".date-picker-input").datepick({
    rangeSelect: true,
    dateFormat: "dd/mm/yyyy",
    changeMonth: false,
    prevText: "<",
    nextText: ">",
    showOtherMonths: true,
    dayNamesShort: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
    onShow: function (dates) {setTimeout(() => {Open();}, 0);},
    onChangeMonthYear: function (year, month) { Change(year, month);},
    onDate: highlightDays,
    onClose: Close,
    showTrigger: "#calImg",
  });

  // function waits a return, false or true or other but
  // dunno the utility
  function highlightDays(date) {
    if (dates_selected.length == 0) return false;

    if (compareDates(date, dates_selected[0]) == 0) {
      firstdate_present = true;//yes present in current display
    }
    if (compareDates(date, dates_selected[1]) == 0) {
      lastdate_present = true;//yes present in current display
    }
    return true;
  }

  function Change(y, m) {
    firstdate_present = false;
    lastdate_present = false;
  }

  function Open() {
    if (dates_selected.length == 0) return;
    //current year month displayed YYYYMM
    let current = parseDate($(".datepick-month-header").text());
    //year month for first and last date of range
    let months = new objDate(dates_selected[0].getFullYear() * 100 + dates_selected[0].getMonth(), 
                             dates_selected[1].getFullYear() * 100 + dates_selected[1].getMonth());
    let days = new objDate(dates_selected[0].getDate(), dates_selected[1].getDate());
    //let years = new objDate(dates_selected[0].getFullYear(), dates_selected[1].getFullYear() );

    let others = $(".datepick-other-month").map( (_, e) => parseInt($(e).text())).get();

    if (firstdate_present && lastdate_present) {
      if (current == months.first && current == months.last) {
        $(".datepick-popup .datepick-selected:first, .datepick-popup .datepick-selected:last").addClass("selected");
      } else if (current == months.first) {
        $(".datepick-popup .datepick-selected:first").addClass("selected");
        let indexfirst = others.findIndex( num => num < 15);
        let indexlast = others.findIndex( num => num == days.last);
        for(let i = indexfirst; i <= indexlast; i++){
          if(i == indexlast ) $(".datepick-other-month").eq(i).addClass("selected");
          $(".datepick-other-month").eq(i).addClass("datepick-selected");
        }
      } else if (current == months.last) {
        $(".datepick-popup .datepick-selected:last").addClass("selected");
        let indexfirst = others.findIndex( num => num == days.first);
        for(let i = indexfirst; i < others.length && others[i] > 15; i++){
          if(i == indexfirst ) $(".datepick-other-month").eq(i).addClass("selected");
          $(".datepick-other-month").eq(i).addClass("datepick-selected");
        }
      } else if (current != months.first && current != months.last) {  
        let indexfirst = others.findIndex( num => num == days.first);
        let indexlast = others.findIndex( num => num == days.last);
        for(let i = indexfirst; i <= indexlast; i++){
            if( i == indexfirst || i == indexlast ) $(".datepick-other-month").eq(i).addClass("selected");
            $(".datepick-other-month").eq(i).addClass("datepick-selected");
        }
      }
    } else if (firstdate_present) {
      if (current == months.first) {
        $(".datepick-popup .datepick-selected:first").addClass("selected");
        let indexfirst = others.findIndex( num => num < 15);
        for(let i = indexfirst; i <= others.length && i >= 0; i++){
          $(".datepick-other-month").eq(i).addClass("datepick-selected");
        }
      } else if (current != months.first) {  
        let indexfirst = others.findIndex( num => num == days.first);
        for(let i = indexfirst; i < others.length; i++){
            if( i == indexfirst ) $(".datepick-other-month").eq(i).addClass("selected");
            $(".datepick-other-month").eq(i).addClass("datepick-selected");
        }
      }
    } else if (lastdate_present) {
      if (current == months.last) {
        $(".datepick-popup .datepick-selected:last").addClass("selected");
        for(let i = 0; i <= others.length && others[i] >= 15; i++){
          $(".datepick-other-month").eq(i).addClass("datepick-selected");
        }
      } else if (current != months.last) {  
        let indexfirst = others.findIndex( num => num == days.last);
        for(let i = indexfirst; i < others.length; i++){
            if( i == indexfirst ) $(".datepick-other-month").eq(i).addClass("selected");
            $(".datepick-other-month").eq(i).addClass("datepick-selected");
        }
      }
    } else if( current > months.first && current < months.last){
      $(".datepick-other-month").addClass("datepick-selected");
    }

  }

  function Close(dates) {
    dates_selected = dates;
    firstdate_present = false;
    lastdate_present = false;
  }

  function compareDates(a, b) {
    if (a < b) return -1;
    if (a > b) return +1;

    return 0; // dates are equal
  }

  function objDate(first, last) {
    this.first = first;
    this.last = last;
    this.same = first == last;
  }

  // arg:string MMMM YYYY, return:number YYYYMM
  function parseDate(s) {
    // to adapt following the setting of name month displayed
    var months = {january: 0, february: 1, march: 2, april: 3, may: 4, june: 5, july: 6, august: 7, september: 8, october: 9, november: 10, december: 11};
    var p = s.split(" ");
    return parseInt(p[1]) * 100 + parseInt(months[p[0].toLowerCase()]);
  }
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-08
    相关资源
    最近更新 更多