Google Apps脚本:将字符串解析为日期对象 [英] Google Apps Script: parsing strings as Date objects

查看:95
本文介绍了Google Apps脚本:将字符串解析为日期对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所有,

请考虑以下JavaScript代码片段,它接受字符串化的日期并创建一个新的 Date 对象:

  var str ='2016-02-01'; 
var d = new Date(str);
console.log(d);

运行上述退货,例如 Mon Feb 01 2016 00:00 :00 GMT + 0000(GMT标准时间)



然而,将等效代码作为Google Apps脚本功能运行并不会产生相同的结果结果:
$ b

function strToDateTest(){
var str ='2016-02- 01' ;
var d = new Date(str);
Logger.log(d);

$ / code>

在这种情况下,输出是 Thu Jan 01 01:00:00 GMT + 01:00 1970



我假设,鉴于不同的日志输出,Google Apps脚本正在使用它自己的实现 Date 而不是原生JavaScript对象



有人可以对此有所了解,并建议如何最好地解析Google Apps脚本中的日期值? p>

解决方案

形成我的Google Apps脚本未运行最后一个ECMAScript版本( doc here )。因此,为了我可以阅读,这未在这个版本。如果你用THH:mm:ss.sssZ装饰你的字符串,它应该可以工作:

  function strToDateTest(){
var str ='2016-02-01';
str + ='T00:00:00.000Z';
var d = new Date(str);
Logger.log(d);
}


All,

Consider the following JavaScript snippet, which takes a stringified date and creates a new Date object:

var str = '2016-02-01';
var d = new Date(str);
console.log(d);

Running the above returns, for example, Mon Feb 01 2016 00:00:00 GMT+0000 (GMT Standard Time).

However, running the equivalent code as a Google Apps Script function does not yield the same result:

function strToDateTest() {
   var str = '2016-02-01';
   var d = new Date(str);
   Logger.log(d);
}

In this case, the output is Thu Jan 01 01:00:00 GMT+01:00 1970.

I assume, given the differing log outputs, Google Apps Script is using its own implementation of Date and not the native JavaScript object.

Could someone shed some light on this, and advise how best to parse date values in Google Apps Script?

解决方案

Form me Google Apps Script is not running the last ECMAScript version (doc here). So for what I could read this is not implemented in this version. If you decorate your string with "THH:mm:ss.sssZ" it should work:

function strToDateTest() {
   var str = '2016-02-01';
   str +='T00:00:00.000Z';
   var d = new Date(str);
   Logger.log(d);
}

这篇关于Google Apps脚本:将字符串解析为日期对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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