日期与今天的比较

问题描述 投票:0回答:1

我从谷歌工作表中的单元格中获取一个值。在此示例中,为“22/03/2024”。

// getting the value that I want to test
let jourtestee = sheetRaw.getRange(2,5).getValue().getDate();
let moistestee = sheetRaw.getRange(2,5).getValue().getMonth();
let annéetestee = sheetRaw.getRange(2,5).getValue().getFullYear();
let NewDateTestee = new Date(annéetestee,moistestee,jourtestee,0,0,0)


// getting today date
const d = new Date();
let day = d.getDate();
let month = d.getMonth();
let year = d.getFullYear();
let NewToday = new Date(year,month,day,0,0,0);

console.log(NewToday == NewDateTestee) // true

它运行良好,但我认为它非常复杂,我不需要小时、分钟、秒。我只想测试是否是同一天

也许有更简单的方法?

date google-apps-script
1个回答
0
投票

检查表格中的日期是否与今天的日期相同。

function checkDate() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName('Sheet1'); // Replace 'Sheet1' with your sheet name
  var cell = sheet.getRange('A1'); // Replace 'A1' with the cell containing the date value

  var cellDate = cell.getValue(); // Get the date value from the cell
  var todayDate = new Date(); // Get today's date

  // Format both dates to compare only the date part (without time)
  var cellDateFormatted = Utilities.formatDate(cellDate, ss.getSpreadsheetTimeZone(), 'yyyy-MM-dd');
  var todayDateFormatted = Utilities.formatDate(todayDate, ss.getSpreadsheetTimeZone(), 'yyyy-MM-dd');

  // Compare the formatted dates
  if (cellDateFormatted === todayDateFormatted) {
    Logger.log('The date in cell A1 is the same as today\'s date.');
    // Perform additional actions if needed
  } else {
    Logger.log('The date in cell A1 is not the same as today\'s date.');
    // Perform alternative actions if needed
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.