简体   繁体   English

为什么 Date().getHours() 有时会返回一个布尔值 True?

[英]Why does Date().getHours() sometimes return a boolean of True?

I'm using Google Apps Script to try and send automatic emails during business hours.我正在使用 Google Apps 脚本尝试在工作时间自动发送电子邮件。 As part of this, I use some of the Date functions to get the current day, hour, minute, etc.作为其中的一部分,我使用一些 Date 函数来获取当前的日期、小时、分钟等。

  var nowH=new Date().getHours();
  var nowD=new Date().getDay();
  var nowM=new Date().getMinutes();
  Logger.log('Day: ' + nowD + ', Hour: ' + nowH + ', Min: ' + nowM )

When I run this, it runs fine.当我运行它时,它运行良好。 For example, it currently returns:例如,它当前返回:

[18-10-04 13:18:02:743 AEST] Day: 4, Hour: 13, Min: 18

So it looks like it's working fine, but when I use my if statements, the timing seems off.所以看起来它工作正常,但是当我使用 if 语句时,时间似乎不对。 I decided to print some of these in the HTML email as well:我决定在 HTML 电子邮件中打印其中一些:

+ '<br>Debug: Day: ' + nowD + '  Hour: ' + nowH + '  Min:  ' + nowM

And to my surprise, I get something like:令我惊讶的是,我得到了类似的信息:

Debut: Day: 4 Hour: true Min: 18

So it looks like the 'nowH' hour data, has suddenly turned into 'True'.所以看起来'nowH'小时数据突然变成了'True'。 I don't reference nowH any other time before the if-statement or adding this to the email.我不会在 if 语句之前的任何其他时间引用 nowH 或将其添加到电子邮件中。

Are there any obvious reasons how this might change to a boolean?是否有任何明显的原因可能会更改为布尔值?

As suggested by @Jaromanda X, it was the single = sign which seemed to change the value of 'nowH'.正如@Jaromanda X 所建议的那样,是单个 = 符号似乎改变了“nowH”的值。

I tested this by adding a separate logger of the same 3 fields.我通过添加相同 3 个字段的单独记录器对此进行了测试。

Before the change: if(( nowH=10 && nowM > 25 && nowM < 35 && nowD!=6 && nowD!=0 ) || (nowH>=10 && nowH<=17 && nowM <5 && nowM>58 && nowD!=6 && nowD!=0))更改前: if(( nowH=10 && nowM > 25 && nowM < 35 && nowD!=6 && nowD!=0 ) || (nowH>=10 && nowH<=17 && nowM <5 && nowM>58 && nowD!=6 && nowD!=0))

[18-10-04 13:47:02:337 AEST] Day: 4, Hour: 13, Min: 47 [18-10-04 13:47:02:337 AEST] 天:4,小时:13,分钟:47

[18-10-04 13:47:02:338 AEST] Day2: 4, Hour2: false , Min2: 47 [18-10-04 13:47:02:338 AEST] Day2: 4, Hour2: false , Min2 : 47

After the change: if(( nowH==10 && nowM > 25 && nowM < 35 && nowD!=6 && nowD!=0 ) || (nowH>=10 && nowH<=17 && nowM <5 && nowM>58 && nowD!=6 && nowD!=0))更改后: if(( nowH==10 && nowM > 25 && nowM < 35 && nowD!=6 && nowD!=0 ) || (nowH>=10 && nowH<=17 && nowM <5 && nowM>58 && nowD!=6 && nowD!=0))

[18-10-04 13:47:02:337 AEST] Day: 4, Hour: 13, Min: 47 [18-10-04 13:47:02:337 AEST] 天:4,小时:13,分钟:47

[18-10-04 13:47:02:338 AEST] Day2: 4, Hour2: 13 , Min2: 47 [18-10-04 13:47:02:338 AEST] Day2: 4, Hour2: 13 , Min2 : 47

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM