程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了如何检查单元格中的日期+ 5 = 今天? (Google Apps 脚本 + Google 表格)大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决如何检查单元格中的日期+ 5 = 今天? (Google Apps 脚本 + Google 表格)?

开发过程中遇到如何检查单元格中的日期+ 5 = 今天? (Google Apps 脚本 + Google 表格)的问题如何解决?下面主要结合日常开发的经验,给出你关于如何检查单元格中的日期+ 5 = 今天? (Google Apps 脚本 + Google 表格)的解决方法建议,希望对你解决如何检查单元格中的日期+ 5 = 今天? (Google Apps 脚本 + Google 表格)有所启发或帮助;

我正在尝试从单元格中获取日期,为其添加 5 天,然后检查该日期是否为今天。这是我尝试过的:

function test() {
  sheet = SpreadsheetApp.getActive();
  var today = new Date(new Date().setHours(0,0));

  var weekStartDate = sheet.getRange('B5').getValue().getTime();
    var newDate1 = new Date(new Date().setHours(0,0));
    var newDate5 = new Date(new Date().setHours(0,0));
    newDate1.setTime(weekStartDate + (1*24*60*60*1000));
    newDate5.setTime(weekStartDate + (5*24*60*60*1000));

  if(newDate5==today){
    var answer = 'Yes';
  } else{
    var answer = 'No';
  }

   Logger.log(today);
   Logger.log(newDate1);
   Logger.log(newDate5);
   Logger.log(answer);
}

日志显示:

11:29:59 AM Info    Wed Jul 21 00:00:00 GMT-04:00 2021
11:29:59 AM Info    Sat Jul 17 00:00:00 GMT-04:00 2021
11:29:59 AM Info    Wed Jul 21 00:00:00 GMT-04:00 2021
11:29:59 AM Info    No

如何获得“答案”以返回“是”?

Screenshot

解决方法

为了比较两个日期,我们使用这些日期的值。这意味着如果您将 newDate5 == today 更改为 newDate5.valueOf() == today.valueOf()它将返回 Yes

这是您的代码的简化版本:

function test() {
  var sheet = SpreadsheetApp.getActive();
  var today = new Date().setHours(0,0).valueOf(); 
  var dateToCompare= new Date(sheet.getRange('B5').getValue()).valueOf();
  var dayInMs = 24*60*60*1000

  Logger.log(today);
  Logger.log(dateToComparE);
  Logger.log(today == dateToCompare + 5*dayInMs);

}
,

以防万一

function MyFunction() {
  // date from cell 'A1'
  var date = SpreadsheetApp.getActiveSheet().getRange('A1').getValue();

  // today
  var today = new Date();

  // function to add 5 days to a date
  const five_days_later = (d) => new Date(d.setDate(d.getDate() + 5));

  // function to compare two dates
  const if_same_date = (d1,d2) => d1.getDate() == d2.getDate() &&
    d1.getMonth() == d2.getMonth() && d1.getYear() == d2.getYear();

  // test
  Logger.log(if_same_date(five_days_later(datE),today));
}

var now = new Date();
var then = new Date("2021-07-16");

const to_str = x => '' + x.getYear() + x.getMonth() + x.getDate();
const if_same_date = (d1,d2) => to_str(d1) == to_str(d2);
const plus_days = (date,gap) => new Date(date.valueOf() + gap*24*60*60*1000);

console.log(if_same_date(now,plus_days(then,5)));

大佬总结

以上是大佬教程为你收集整理的如何检查单元格中的日期+ 5 = 今天? (Google Apps 脚本 + Google 表格)全部内容,希望文章能够帮你解决如何检查单元格中的日期+ 5 = 今天? (Google Apps 脚本 + Google 表格)所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。