问题

来自Sheet和Google日历的匹配日期总是假的,如何格式化两个日期进行比较?

我已经尝试格式化日期,即使它们看起来完全相同,它会返回false.

 var Sheet_StartDate =  Spreadsheet.getActiveSheet().getRange(1,1).getValue();
var calendar = CalendarApp.getCalendarById('####');
var event = calendar.getEventById(eventId); 
var calendar_StartTime;

try {
  // Get all day event
  calendar_StartTime = event.getAllDayStartDate();          
}
catch (e) {

  //Multi-day event
  calendar_StartTime = event.getStartTime();

}


if (calendar_StartTime === Sheet_StartDate )
{
 //This comes back false 

}
 

  最佳答案

尝试这个:

 function compareDates() {
  var ss=SpreadsheetApp.getActive();
  var sh=ss.getActiveSheet();
  var dt=new Date(sh.getRange(1,1).getValue());
  var Sheet_StartDate_value=new Date(dt.getFullYear(),dt.getMonth(),dt.getDate()).valueOf();//This removes the time portion
  var calendar = CalendarApp.getCalendarById('####');
  var event = calendar.getEventById(eventId); 
  dt=new Date(event.getAllDayStartDate());
  var calendar_StartTime_value=new Date(dt.getFullYear(),dt.getMonth(),dt.getDate()).valueOf();      
  if(calendar_StartTime_value == Sheet_StartDate_value ) {
    //This will come back true if in fact they are the same date
  }
}
 

您也可以使用Date().getTime()

  相同标签的其他问题

javascriptgoogle-apps-script