问题描述
我正在创建一个新的电子表格,这将提供给我一个提前一周的审查日期。我试图做一个非常基本的检查,检查从今天7天前的日期是否等于电子表格上的日期。
I'm creating a new spreadsheet that is set to give me a week ahead reminder for a review date. I'm trying to do a very basic test of checking if the date 7 days ahead from today is equal to the date that is on the spreadsheet.
调试这个我注意到时间被添加到日期,所以我将时间设置为00:00:00尝试获得匹配,这仍然不起作用。
When debugging this I noticed the times get added to the date so I set the times to be 00:00:00 to try and get a match, this still does not work.
function main(){
var d = new Date();
d.setDate(d.getDate()+7);
d.setHours(0,0,0,0);
for (var j = 53; j < lastrow-1; j++){
var Staff = StaffObjects[j];
var name = Staff.firstName + " " + Staff.surname;
var onemonth = Staff.oneMonth;
onemonth.setHours(0,0,0,0);
if (d == onemonth){
messagePT2+= name + "is due there one month review on " + onemonth + "<br>";
}
}
}
调试时,完全一样:
d = Fri Mar 07 2014 00:00:00 GMT-0000(GMT)
onemonth = Fri Mar 07 2014 00:00 :00 GMT-0000(GMT)
d = Fri Mar 07 2014 00:00:00 GMT-0000 (GMT)onemonth = Fri Mar 07 2014 00:00:00 GMT-0000 (GMT)
然而,这并没有注册为比赛,为什么会这样呢,我该怎么办呢?最后。一些日期有不同的时区,如这个.. Wed May 07 2014 00:00:00 GMT + 0100(BST)有没有办法检查日期?
However this does not register as a match, why is this and how could i go about fixing this? Lastly. Some dates have different time zones such as this.. Wed May 07 2014 00:00:00 GMT+0100 (BST) is there any way to check for just the date?
非常感谢,
推荐答案
比较平等的日期对象不行。如果将它们转换为数字(或字符串)进行比较,则会更好。尝试这样:
Comparing date objects for equality does not work like that. It's better if you convert them to numbers (or strings) to compare. Try this:
if (d.getTime() == onemonth.getTime()){
//etc
}
请注意,与>
和<
和变体它可以工作(因为它使用 valueOf
)。
Note that when comparing with >
and <
and variants it does work (because it uses valueOf
).
这篇关于Google Script等于日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!