Apps脚本功能用于检查是否已经过了一定数量的工作日

时间:2017-01-23 16:10:59

标签: google-apps-script

我有以下脚本来检查自发送电子邮件以来是否已经过了所需的天数:

function test(){
  var ready = readyToSend('01/19/2017 08:00','2d');
}
function readyToSend(lastDate,days){
  var minTimeAgo = new Date();
  minTimeAgo.setTime(subtract(minTimeAgo, days));
  var earliestDate = Utilities.formatDate(minTimeAgo, "GMT-7", "MM/dd/yyyy HH:mm");
  if(earliestDate > lastDate){
    return true;
  }
  return false;
}

这适用于检查天数是否已过,但我需要知道所需的工作日数是否已过。因此,如果lastDate是星期四而days是2,则该函数在星期二之前不应返回true。现在,它在星期一返回true,因为已经过了2天。

如何在检查中忽略周末的失效天数?

1 个答案:

答案 0 :(得分:1)

你可以试试这个:

function test(){
  var ready = readyToSend('01/19/2017 08:00','2');
}
function readyToSend(lastDate,days){
  var minTimeAgo = new Date(new Date()-days*24*60*60*1000); // calculate the latest datetime required to meet the delay
  lastDate = new Date(lastDate); // convert lastDate to a Date
  var newDow = lastDate.getDay()*1 + days*1; // calculate the day of the week the new date should be
  if(newDow > 5){ //verify that the lastDate + days does not cross a weekend
    minTimeAgo = new Date(minTimeAgo - 2*24*3600*1000); // if so, subtract another 2 days from the latest acceptible datetime
  }
  if(minTimeAgo > lastDate){
    return true;
  }
  return false;
}

如果您需要检查过去几天(最多一个工作周),它就无法工作。