-4

I have requirement to disable tuesday and thursday on this calendar function but it should enable the current date also. Below script will disable past dates and enable only tuesday and thursday but current date is not enabled. i need to enable current date selection and only future tuesday & thursday dates only.

function datefunc(date) {
  var today = new Date();
  if (date != null) {
    var yesterday = new Date();
    var futureDate = new Date();
    var today = new Date();
    today = today.setDate(today.getDate() - 1)
    yesterday = yesterday.setDate(yesterday.getDate() - 1);
    futureDate = futureDate.setDate(futureDate.getDate() + 1);

    if (date < yesterday)
      return true;
    else if (date == today || date.getDay() != 2 && date.getDay() != 4)
      return true;
  }
  return false;
}
Mark Schultheiss
  • 32,614
  • 12
  • 69
  • 100
  • Why are you minusing 1 off of today's date? (line 7) – MCMXCII Feb 13 '18 at 16:42
  • And what does this have to do with PHP? – MCMXCII Feb 13 '18 at 16:43
  • You have a very confusing question: "disable tuesday and thursday on this calendar function but it should enable the current date " then later " I need to enable current date selection and only future tuesday & thursday dates only." So which is it? Enable today and all future Tuesday and Wednesday? Disable all past dates? If today is a Tuesday or Thursday should it be enabled or disabled? What if a "future" date is a Wednesday, enable or disable it? By "disable/enable" do you mean return `true` each "enable" "requirement? – Mark Schultheiss Feb 13 '18 at 17:57
  • Also clarify how you want to handle conditions such as Daylight Savings Time on a given submitted date (if it IS date just before, on or just after a Daylight Savings Time change, how do you want it handled? Clarify also if you just want to compare the "date" part or include the time part also, including with the DST consideration. – Mark Schultheiss Feb 13 '18 at 18:04
  • Sorry for the confusion. My requirement is below. 1. disable past dates from selection, 2. Only enable tuesday and thursday in future weeks. 3) need to enable current day (today).. say eg. today is wednesday if i open the calendar it should enable today date and future tuesday and thursday only remaining should be disabled.. likewise for all current date. – user3715261 Feb 14 '18 at 05:21

1 Answers1

1

I am going to go ahead and attempt to answer a very ambiguous question. Given that, I am only going to post something that hopefully you can work with.

First off, this date stuff is "hard" so let's leverage StackOverflow to find stuff we can work with (it MIGHT be better to use a date library but let's build from others efforts - up vote them as useful!)

Some additional information on date comparisons: https://stackoverflow.com/a/493018/125981

// credit here: https://stackoverflow.com/a/1353711/125981
function isDate(d) {
  if (Object.prototype.toString.call(d) === "[object Date]") {
    // it is a date
    if (isNaN(d.getTime())) { // d.valueOf() could also work
      // date is not valid
      return false;
    } else {
      // date is valid
      return true;
    }
  } else {
    // not a date
    return false;
  }
}

// return true if prior to yesterday, OR is today(exactly), is not tuesday, or is not thursday
function datefunc(date) {
  var today = new Date();
  if (isDate(date)) {
    var yesterday = new Date(new Date().setDate(today.getDate() - 1));
    var tomorrow = new Date(new Date().setDate(today.getDate() + 1));
    console.log(today.getTime(), yesterday.getTime(), tomorrow.getTime());
    if (date.getTime() < yesterday.getTime() || date.getTime() == today.getTime() || (date.getDay() != 2 && date.getDay() != 4)) {
      return true;
    }
  }
  return false;
}
// credit here: https://stackoverflow.com/a/27336600/125981
// if d is dow, return that else next dow
function currentOrNexDowDay(d, dow) {
  d.setDate(d.getDate() + (dow + (7 - d.getDay())) % 7);
  return d;
}
// credit here: https://stackoverflow.com/a/27336600/125981
// if cd is dow and not d, return that else next dow
function nextDowDay(d, dow) {
  var cd = new Date(d);
  cd.setDate(cd.getDate() + (dow + (7 - cd.getDay())) % 7);
  if (d.getTime() === cd.getTime()) {
    cd.setDate(cd.getDate() + 7);
  }
  return cd;
}
// a Tuesday
var checkDate = new Date("2018-02-13T17:30:29.569Z");
console.log(datefunc(checkDate));
// a Wednesday
checkDate = new Date("2018-02-14T17:30:29.569Z");
console.log(datefunc(checkDate));
// a Sunday
checkDate = new Date("2018-02-11T17:30:29.569Z");
console.log(datefunc(checkDate));

// Next Monday
var d = new Date();
d = currentOrNexDowDay(d, 1);
//d.setDate(d.getDate() + (8 - d.getDay()) % 7);
console.log(d);
console.log(datefunc(d));

// Next Tuesday
var dt = new Date();
dt = nextDowDay(dt, 2);
console.log(dt);
console.log(datefunc(dt));
Mark Schultheiss
  • 32,614
  • 12
  • 69
  • 100