Google Apps脚本新日期无效,但在JavaScript中有效 [英] Google Apps Script new Date is invalid but in JavaScript it works

查看:154
本文介绍了Google Apps脚本新日期无效,但在JavaScript中有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我使用此字符串08.12.2017 00:00:00创建新日期,Google Apps脚本会返回无效日期,但是当我使用jsfiddle尝试该示例时,它可以正常工作。



Google Apps脚本

  var limit = new Date(08.12.2017 00:00:00)$ 


$ b

无效日期

  var limit = new Date(08.12.2017 00:00:00); 
console.log(limit);

2017年8月12日星期六00:00:00 GMT + 0200(中欧日照时间)



这是为什么? 解决方案

不同的引擎,不同的解析规则。 Google Apps脚本基于Rhino。从 Rhino测试日期解析你可以猜到它不支持很多日期时间格式。以下是可以接受的:

  var limit = new Date(2017-12-08T00:00:00); //在脚本的时区
var limit = new Date(2017-12-08T00:00:00Z); //在UTC中

日期必须是yyyy-mm-dd,它必须是T和不是日期和时间之间的空间。

If I create a new Date with this String "08.12.2017 00:00:00" Google Apps Scripts return an Invalid date but when i try the example with jsfiddle it works.

Google Apps script

var limit = new Date("08.12.2017 00:00:00")

Invalid Date

Jsfiddle

var limit = new Date("08.12.2017 00:00:00");
console.log(limit);

Sat Aug 12 2017 00:00:00 GMT+0200 (Central Europe Daylight Time)

Why is that so?

解决方案

Different engines, different parsing rules. Google Apps Script is based on Rhino. From the look at Rhino tests for date parsing you can guess it doesn't support a lot of datetime formats. The following are acceptable for it:

var limit = new Date("2017-12-08T00:00:00");  // in the timezone of the script
var limit = new Date("2017-12-08T00:00:00Z");  // in UTC

The date has to be yyyy-mm-dd, and it must be T and not a space in between date and time.

这篇关于Google Apps脚本新日期无效,但在JavaScript中有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆