简体   繁体   English

Google 应用程序脚本一直将我的计算视为字符串,如何解决?

[英]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.

相关问题 在 Google 表格中使用 Google Apps 脚本时如何防止日期变成字符串? - How to keep the date from turning into a String when using a Google Apps Script in Google Sheets? Google Apps脚本:如何解决“序言中不允许内容” - Google Apps Script: How to fix “content is not allowed in prolog” 如何修复以随机顺序运行的 Google Apps 脚本功能 - How to fix Google Apps Script Function Running in random sequence 如何在 Google Apps 脚本中修复或调整此查找和替换 - How to fix or adapt this Find and Replace in Google Apps Script Google Apps脚本正在将我的数组更改为字符串。 为什么? - Google Apps Script is changing my Array into a string. Why? Google Apps 脚本中的字符串等式 - String Equality in Google Apps Script Google Apps脚本中的字符串匹配 - String Matching in Google Apps Script 如何解决我在谷歌地图距离和谷歌地图 API 距离计算中得到的差异? - How to fix the difference I'm getting in the google map distance and my google map API distance calculation? 如何在谷歌应用程序脚本中用空格分隔符分割字符串 - How to split a string with space delimiter in google apps script 如何在不使用 XmlService 的情况下解析 Google Apps 脚本中的 HTML 字符串? - How to parse an HTML string in Google Apps Script without using XmlService?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM