Javascript数据功能禁用特定日期

时间:2018-02-13 16:35:44

标签: javascript

我要求在此日历功能上禁用星期二和星期四,但它也应启用当前日期。下面的脚本将禁用过去的日期,并且仅在星期二和星期四启用,但不启用当前日期。我需要启用当前日期选择,只有未来的星期二&星期四只有日期。

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;
}

1 个答案:

答案 0 :(得分:1)

我将继续尝试回答一个非常含糊的问题。鉴于此,我只会发布一些希望你可以使用的东西。

首先,这个日期的东西是“硬”所以让我们利用StackOverflow找到我们可以使用的东西(可能更好地使用日期库,但让我们从其他人的努力中建立起来 - 将它们投票为有用!)

有关日期比较的一些其他信息: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));