Google-apps-script日期比较。无法比较每月的第一天。getMonth()

huangapple go评论89阅读模式
英文:

Google-apps-script date comparison. Can't compare first day of the month. getMonth()

问题

"EDIT: There was no bug. Somehow the project setting in google script was set to the wrong timezone. Ahh... :D"

"I cant figure out why when I'm collecting dates from my sheet in 'var sheetDate' and compare it with 'var date' every date works and the email is sent, from 02.01.2020 to 31.01.2020 but not 01.01.2020."

"I use .getmonth() to compare so this script can run once a month for years. Is it something special with 01.01 that it doesn't work?"

function sendEmail() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var startRow = 1;  // First row of data to process
  var numRows = sheet.getLastRow();   // Number of rows to process
  // Fetch the range of cells
  var dataRange = sheet.getRange(startRow, 1, numRows, sheet.getLastColumn());
  // Fetch values for each row in the Range.
  var data = dataRange.getValues();
  Logger.log(data)

  for (var i in data) {
    var row = data[i];
    var date = new Date();
    var sheetDate = new Date(row[18]);
        if (date.getMonth() == sheetDate.getMonth()){
          var emailAddress = row[19];  // Email
          var message = row[1];       // Info
          var subject = row[11];
          MailApp.sendEmail(emailAddress, subject, message);
          Logger.log('SENT :' + emailAddress + '  ' + subject + '  ' + message)
    }    
  }
}

"Edit: The log as requested"

[20-01-04 15:03:48:762 CET] Sheetdate:Wed Jan 01 2020 18:00:00 GMT-0500 (EST)
[20-01-04 15:03:48:764 CET] Date:Sat Jan 04 2020 09:03:48 GMT-0500 (EST)
[20-01-04 15:03:48:764 CET] date.getMonth():0
[20-01-04 15:03:48:765 CET] sheetdate.getMonth():0
[20-01-04 15:03:48:837 CET] Sheetdate:Tue Jan 03 2023 18:00:00 GMT-0500 (EST)
[20-01-04 15:03:48:838 CET] Date:Sat Jan 04 2020 09:03:48 GMT-0500 (EST)
[20-01-04 15:03:48:839 CET] date.getMonth():0
[20-01-04 15:03:48:839 CET] sheetdate.getMonth():0
[20-01-04 15:03:48:934 CET] Sheetdate:Thu Jan 02 2020 18:00:00 GMT-0500 (EST)
[20-01-04 15:03:48:935 CET] Date:Sat Jan 04 2020 09:03:48 GMT-0500 (EST)
[20-01-04 15:03:48:936 CET] date.getMonth():0
[20-01-04 15:03:48:936 CET] sheetdate.getMonth():0
[20-01-04 15:03:48:998 CET] Sheetdate:Fri Jan 31 2020 18:00:00 GMT-0500 (EST)
[20-01-04 15:03:48:999 CET] Date:Sat Jan 04 2020 09:03:48 GMT-0500 (EST)
[20-01-04 15:03:48:999 CET] date.getMonth():0
[20-01-04 15:03:49:000 CET] sheetdate.getMonth():0

Image of part of sheet

英文:

EDIT: There was no bug. Somehow the project setting in google script was set to the wrong timezone. Ahh... Google-apps-script日期比较。无法比较每月的第一天。getMonth()

I cant figure out why when I'm collecting dates from my sheet in "var sheetDate" and compare it with "var date" every date works and the email is sent, from 02.01.2020 to 31.01.2020 but not 01.01.2020.

I use .getmonth() to compare so this script can run once a month for years. Is it something special with 01.01 that it doesn't work?

function sendEmail() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var startRow = 1;  // First row of data to process
  var numRows = sheet.getLastRow();   // Number of rows to process
  // Fetch the range of cells
  var dataRange = sheet.getRange(startRow, 1, numRows, sheet.getLastColumn());
  // Fetch values for each row in the Range.
  var data = dataRange.getValues();
  Logger.log(data)

  for (var i in data) {
    var row = data[i];
    var date = new Date();
    var sheetDate = new Date(row[18]);
        if (date.getMonth() == sheetDate.getMonth()){
          var emailAddress = row[19];  // Email
          var message = row[1];       // Info
          var subject = row[11];
          MailApp.sendEmail(emailAddress, subject, message);
          Logger.log('SENT :'+emailAddress+'  '+subject+'  '+message)
    }    
  }
}

Edit: The log as requested

 [20-01-04 15:03:48:762 CET] Sheetdate:Wed Jan 01 2020 18:00:00 GMT-0500 (EST)
[20-01-04 15:03:48:764 CET] Date:Sat Jan 04 2020 09:03:48 GMT-0500 (EST)
[20-01-04 15:03:48:764 CET] date.getMonth():0
[20-01-04 15:03:48:765 CET] sheetdate.getMonth():0
[20-01-04 15:03:48:837 CET] Sheetdate:Tue Jan 03 2023 18:00:00 GMT-0500 (EST)
[20-01-04 15:03:48:838 CET] Date:Sat Jan 04 2020 09:03:48 GMT-0500 (EST)
[20-01-04 15:03:48:839 CET] date.getMonth():0
[20-01-04 15:03:48:839 CET] sheetdate.getMonth():0
[20-01-04 15:03:48:934 CET] Sheetdate:Thu Jan 02 2020 18:00:00 GMT-0500 (EST)
[20-01-04 15:03:48:935 CET] Date:Sat Jan 04 2020 09:03:48 GMT-0500 (EST)
[20-01-04 15:03:48:936 CET] date.getMonth():0
[20-01-04 15:03:48:936 CET] sheetdate.getMonth():0
[20-01-04 15:03:48:998 CET] Sheetdate:Fri Jan 31 2020 18:00:00 GMT-0500 (EST)
[20-01-04 15:03:48:999 CET] Date:Sat Jan 04 2020 09:03:48 GMT-0500 (EST)
[20-01-04 15:03:48:999 CET] date.getMonth():0
[20-01-04 15:03:49:000 CET] sheetdate.getMonth():0

Image of part of sheet

答案1

得分: 0

function sendEmail() {
  var sh = SpreadsheetApp.getActive();
  var rg = sh.getRange(1, 1, sh.getLastRow(), sh.getLastColumn());
  var data = rg.getValues();
  for (var i = 0; i < data.length; i++) {
    var toda = new Date().valueOf();
    var dt = new Date(data[i][18]);
    var s = new Date(dt.getFullYear(), dt.getMonth(), 1).valueOf();//beginning of month
    var e = new Date(dt.getFullYear(), dt.getMonth() + 1, 0).valueOf();//end of month
    if (toda >= s && toda <= e) {
      var emailAddress = data[i][19];
      var message = data[i][1];
      var subject = data[i][11];
      MailApp.sendEmail(emailAddress, subject, message);
      Logger.log('SENT :' + emailAddress + '  ' + subject + '  ' + message)
    }
  }
}
英文:
function sendEmail() {
  var sh=SpreadsheetApp.getActive();
  var rg=sh.getRange(1, 1, sh.getLastRow(), sh.getLastColumn());
  var data=rg.getValues();
  for(var i=0;i&lt;data.length;i++) {
    var toda=new Date().valueOf();
    var dt=new Date(data[i][18]);
    var s=new Date(dt.getFullYear(),dt.getMonth(),1).valueOf();//beginning of month
    var e=new Date(dt.getFullYear(),dt.getMonth()+1,0).valueOf();//end of month
    if (toda&gt;=s &amp;&amp; toda&lt;=e){
      var emailAddress=data[i][19];  
      var message=data[i][1];       
      var subject=data[i][11];
      MailApp.sendEmail(emailAddress, subject, message);
      Logger.log(&#39;SENT :&#39;+emailAddress+&#39;  &#39;+subject+&#39;  &#39;+message)
    }    
  }
}

答案2

得分: 0

没有错误。不知何故,Google脚本中的项目设置已设置为错误的时区。啊... Google-apps-script日期比较。无法比较每月的第一天。getMonth()

英文:

EDIT: There was no bug. Somehow the project setting in google script was set to the wrong timezone. Ahh... Google-apps-script日期比较。无法比较每月的第一天。getMonth()

huangapple
  • 本文由 发表于 2020年1月3日 19:22:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/59577725.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定