仅按月和日比较日期
Comparing dates by month and day only
我即将完成为我的学校自动发布公告的脚本。
最后一块是将生日添加到公告的末尾。用户将已经为公告输入了日期,导致大部分公告被写入。
我想使用输入的日期与名为 Birthdays 的 sheet 中的学生生日进行比较。我试图根据 this answer 将每个生日转换为一年中某一天的整数值。
应该检查日期,即使年份可能不同,并将学生姓名添加到下一行。
到目前为止,这是这部分的代码:
var bDayHeader= "Birthdays"
bulletin.getRange(nextBullRow,1).setValue(bDayHeader);
nextBullRow+=2;
var birthdays = ss.getSheetByName("Birthdays");
var lastBdayRow = birthdays.getLastRow();
for(var i=2; i<=lastBdayRow; i++) {
var bRow = birthdays.getRange(i,1,1,3).getValues();
var bDay = new Date(bRow[0][0]);
var bDayStudent = bRow[0][2];
if(compareDate.getMonth() == bDay.getMonth() && compareDate.getDate() == bDay.getDate()){
Logger.log(bDayStudent);
bulletin.getRange(nextBullRow,1).setValue(bDayStudent);
bulletin.getRange(nextBullRow,1).setFontWeight("normal");
nextBullRow++ ;
}
}
我会做类似的事情
if(compareDate.getMonth() == bDay.getMonth() && compareDate.getDate() == bDay.getDate()) {
// its the same day of the month.
}
对于 2 月 29 日出生的人来说,这显然是刻薄的。 :D
您可以在 MDN Date reference 上阅读有关 Date
对象可用方法的更多信息。
我即将完成为我的学校自动发布公告的脚本。 最后一块是将生日添加到公告的末尾。用户将已经为公告输入了日期,导致大部分公告被写入。 我想使用输入的日期与名为 Birthdays 的 sheet 中的学生生日进行比较。我试图根据 this answer 将每个生日转换为一年中某一天的整数值。
应该检查日期,即使年份可能不同,并将学生姓名添加到下一行。
到目前为止,这是这部分的代码:
var bDayHeader= "Birthdays"
bulletin.getRange(nextBullRow,1).setValue(bDayHeader);
nextBullRow+=2;
var birthdays = ss.getSheetByName("Birthdays");
var lastBdayRow = birthdays.getLastRow();
for(var i=2; i<=lastBdayRow; i++) {
var bRow = birthdays.getRange(i,1,1,3).getValues();
var bDay = new Date(bRow[0][0]);
var bDayStudent = bRow[0][2];
if(compareDate.getMonth() == bDay.getMonth() && compareDate.getDate() == bDay.getDate()){
Logger.log(bDayStudent);
bulletin.getRange(nextBullRow,1).setValue(bDayStudent);
bulletin.getRange(nextBullRow,1).setFontWeight("normal");
nextBullRow++ ;
}
}
我会做类似的事情
if(compareDate.getMonth() == bDay.getMonth() && compareDate.getDate() == bDay.getDate()) {
// its the same day of the month.
}
对于 2 月 29 日出生的人来说,这显然是刻薄的。 :D
您可以在 MDN Date reference 上阅读有关 Date
对象可用方法的更多信息。