【问题标题】:How Can I find the difference between two days我如何才能找到两天之间的差异
【发布时间】:2020-02-24 18:35:23
【问题描述】:

这里我有两个不同的字段current_daydate_to_valuated_within。在current_day 中,我给出了我的当前日期并使用day 函数转换为day,在date_to_valuated_within 字段中,我有两天星期一和星期四。在这里,我必须检查 current_day 字段是否在 date_to_valuated_within 天之内。

例如,

如果今天()是“星期一”,那么它必须将天间隔与date_to_valuated_within 返回为 TRUE 进行比较

如果 today() 是“星期六”,那么它必须将日期间隔与 date_to_valuated_within 返回为 FALSE 进行比较

【问题讨论】:

  • 嘿 Ed,如果今天位于 date_to_valuated_within 字段内,那么它必须返回为真。从逻辑上讲,如果今天()不是周末,那么它必须返回为真,否则它应该是假的。

标签: google-apps-script google-sheets google-sheets-formula


【解决方案1】:

解决方案

在这里,我为您提供解决问题的方法。在下面的一段代码中,您可以找到解释此函数功能的 cmets,以便您了解此代码是如何以及为什么是这样的。

注意:例如,您需要将日期作为“25/02/2020”作为参数传递到此函数中。

function checkDay(day) {
  
  // Create current date by converting the day we provide into a Date variable
  var current_day = new Date(day);
  // Convert our day into a integer (number) according to the day of the week with getDay() (for example, Monday is 1 and Sunday is 7)
  // We do this for then compare it with Monday (1) and Thursday (4) and check if it is within those values
  var current_day_number = current_day.getDay();
  
  // if the day is between monday and thursday including these days (if you don't want to include them you just need to remove the = meaning it would be either tuesday or wednesday)
  if(current_day_number>=1 && current_day_number<=4){
    return true;
  }
  // if the day is not between your desired days return false
  else{
    return false;
  }  
}

// This function is simply for testing if the one above has worked. Simply change in the run tab-> run function to test
function test(){
Logger.log(checkDay('02/23/2020'));
}

我希望这对您有所帮助。让我知道您是否需要其他任何内容,或者您​​是否不理解某些内容。 :)

【讨论】:

    【解决方案2】:
    function isWorkDay(date) {
      const day=new Date(date).getDay();
      return (day>0 && day<6);
    }
    

    Date.getDay()

    也许这会有所帮助:

    function d1isBetweend2Andd3Inclusive(d1,d2,d3) {
      const da1=new Date(d1).getDay();
      const da2=new Date(d2).getDay();
      const da3=new Date(d3).getDay();
      if(da3>da2) {
        return (da1>=da2 && da1<=da3);
      }else if(da2>da3) {
        return (da1>=da3 && da1<=da2);
      }else{
        return da1==da2;
      }
    }
    
    function d1isBetweend2Andd3NonInclusive(d1,d2,d3) {
      const da1=new Date(d1).getDay();
      const da2=new Date(d2).getDay();
      const da3=new Date(d3).getDay();
      if(da3>da2) {
        return (da1>da2 && da1<da3);
      }else if(da2>da3) {
        return (da1>da3 && da1<da2);
      }else{
        return false;
      }
    }
    

    【讨论】:

    • 谢谢@cooper,你能解释一下如何完成这项工作吗?我是脚本新手。如果您能在这方面向我分享更多信息,将会很有帮助。
    • 你想用这个来完成什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-24
    • 2022-12-12
    • 2011-05-29
    • 1970-01-01
    • 2013-01-02
    • 2020-06-07
    • 2010-12-25
    相关资源
    最近更新 更多