我为Google脚本编辑器编写了这两个函数,以扩展到工作表中,以确定事件在最后一次发生时是否应给出以及应该发生的频率。
例如,某药物每天需要服用两次,而上一次服用时间是2020年3月29日,因此如果现在的时间是2020年3月30日,则此功能需要返回“到期” :00,因为它是到期的一个小时,即2020年3月30日00:00。
调用“ IsDue”时得到的结果不是我期望的。我可以正确比较日期/时间到现在吗?
function ReturnFrequencyInDays( unit, quantity) {
switch(unit) {
case "Hours":
return quantity/24;
break;
case "Days":
return quantity;
break;
case "Weeks":
return quantity * 7;
break;
case "Months":
return quantity * 30;
break;
case "Years":
return quantity * 365;
break;
default:
return "Invalid unit";
}
}
function IsDue(how_often,unit,every,last_occurance){
var currentTime=new Date();
if (((ReturnFrequencyInDays(unit,every)/how_often) + last_occurance )<=currentTime) {
return "Due";
} else {
return "Not Due";
}
}
答案 0 :(得分:1)
您需要提供更多有关功能要执行的解释。但是我从您的术语中假设,您试图在此处将天数与日期进行比较:
var currentTime = new Date();
if(((ReturnFrequencyInDays(unit,every)/how_often) + last_occurance)<= currentTime){
return "Due";
}
那是行不通的。您可以使用valueOf()或getMilliseconds()方法从参考日期获取毫秒数,然后将其转换为天数,但是我想那也不是您真正想要的。因此,为了获得更好的答案,请更好地解释预期的用法以及如何调用这些函数。