[英]Google apps script keep considering my calculation as string, how to fix it?
I got a code like this:我有这样的代码:
var date = new Date(new Date() + dateAdjust*3600*1000 );
However, the result came out weird.然而,结果却很奇怪。 But if I change the plus sign to a minus sign, and make the dateAdjust become negative, it results right.但是,如果我将加号更改为减号,并使 dateAdjust 变为负数,则结果正确。 I think it considers dateAdjust 3600 1000 as a string.我认为它将 dateAdjust 3600 1000 视为字符串。 How do I fix that?我该如何解决? I heard some people say to use parseInt(), but it doesn't work either.我听说有人说要使用 parseInt(),但它也不起作用。
var date = new Date(new Date() + parseInt(dateAdjust*3600*1000) );
Regarding the first code line, the part that is evaluated first is new Date() + dateAdjust
.关于第一行代码,首先评估的部分是new Date() + dateAdjust
。 Here the problem is that when using +
having a Date object to the left, makes the date object to be converted into a string.这里的问题是,当使用+
时,左侧有一个日期 object,会使日期 object 转换为字符串。
Having a string to the left of +
, makes the right operand to be converted into a string.在+
的左边有一个字符串,使得右边的操作数被转换成一个字符串。
The fix might be to use use new Date().getTime()
.解决方法可能是使用new Date().getTime()
。 This will return the date-time in milliseconds since January 1, 1970 00:00:00 UTC
.这将返回自January 1, 1970 00:00:00 UTC
以来的日期时间(以毫秒为单位)。 Assuming that dateAjust
has assigned a number, then the final code might be:假设dateAjust
分配了一个数字,那么最终代码可能是:
var date = new Date(new Date().getTime() + dateAdjust * 3600 * 1000 );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.