Datepicker按年份设置

时间:2014-08-26 12:37:53

标签: javascript jquery datepicker

这是关于Datepicker的上一个问题的后续内容。试图在日历上每隔三天显示一次。想出了如何将DOY作为整数并在日历中使用,但它每年三月失败 - 也不能跨越两年?

在第二部分,如果我需要禁用一周中的某一天,我该如何将其与当前的第3天功能相结合。

这里是JSFiddle DatePicker

function noWeekEnds(date) {
  var dow = date.getDay();
  if(dow>5 || dow<1) return [false,''];
  return [true,''];  
}

function unavailable(date) {

  var now = date;
  var start = new Date(now.getFullYear(), 0, 0);
  var diff = now - start;
  var oneDay = 1000 * 60 * 60 * 24;
  var day = Math.floor(diff / oneDay);
  var shift = (day%3===0);

    return [shift, "red2", "available"];
    return noWeekEnds(date);

/*
need this to span across 2 years i.e.: Jan 8 2015 thru Jan 12/2016

also it fails the 3rd week of every March ???
*/
}

$(document).ready(function() {
  $("#datepicker").datepicker({
    beforeShowDay: unavailable
  });

$('#datepicker').attr('readonly',true);
});

1 个答案:

答案 0 :(得分:1)

修复了不可用的功能:

http://jsfiddle.net/nbL98a2r/13/

function unavailable(date) {
    var start = new Date(2015,0,8);
    var end = new Date(2016,0,12);
    var now = date;
    if(now < start || now > end) return [false, "red2", "available"]
    var timeDiff = Math.abs(now.getTime() - start.getTime());
    var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));
    var nwe = noWeekEnds(date);
    var shift = (diffDays%3===0) && nwe[0];

    return [shift, "red2", "available"]
}