BarclayVision
BarclayVision

Reputation: 863

Datepicker set by day of year

This is a followup to a previous question about Datepicker. Trying to show only every third day on the calendar. figured out how to get DOY as integer and use in calendar, however it fails every March - also not able to span two years?

On a second part, if I need to disable a certain day of the week, how do I combine that with current 3rd day feature.

here is 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);
});

Upvotes: 0

Views: 90

Answers (1)

Zentoaku
Zentoaku

Reputation: 767

Fixed unavailable function:

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"]
}

Upvotes: 1

Related Questions