Date.parse(new Date(...))Safari中的NaN [英] Date.parse(new Date(...)) NaN in Safari

查看:732
本文介绍了Date.parse(new Date(...))Safari中的NaN的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是Safari支持一个功能的具体问题,我确定这是我自己的无能为力的。请参阅,这会在Chrome和Firefox中返回一个数字,但在Safari中返回NaN(最新版本,7)。

  var something = Date.parse(new Date(dataLabels [0])); 
console.log(something); //返回历元数字Chrome / Firefox NOT Safari

似乎从一个字符串生成一个历元时间戳像这样,但这是唯一的方法,我可以让它在我的图表项目中工作我注意到这一点。我接近mm / dd / yyyy的翻译时代错误?


解决方案

...我将提交一个错误报告,

我是否接近mm / dd / yyyy的翻译时间错误?


是的。 ES5说


无法识别字符串或包含格式为String的非法元素值的日期将导致Date.parse返回NaN。


不要将日期字符串解析为Date构造函数。直到ES5,解析字符串完全依赖于实现。使用ES5引入了一个版本的!SO 8601,但是所有使用的浏览器都不支持此功能,并且根据字符串的不同,浏览器可能会被UTC或本地时间处理(包括最新版本)。



最安全的方法是手动解析字符串。



鉴于格式为m / d / y,以下将在所有浏览器中可靠地工作:

  //返回给定日期字符串的日期对象,格式为m / d / y格式
function parseDate(s){
var b = s.split(/ \D + /);
return new Date(b [2], - b [0],b [1]);
}

如果你是实际时间值,您可以使用 getTime 或仅使用一元 + 操作符

  var timeValue = + parseDate 5/24/2014'); 

请注意,当您执行以下操作:

 > Date.parse(new Date(dataLabels [0]))

date构造函数首先解析字符串到日期,然后将其转换为字符串,然后将其转换回日期,然后返回时间值。因此,如果初始解析失败,其余的也将如此,如果初始解析成功(并且它可能或可能不取决于浏览器),结果将没有不同:

  + new Date(dataLabels [0]); 

除非Date构造函数不能解析自己的字符串表示形式的Date(这是可能的,但是不符合ECMA-262)。


This is a specific issue with Safari supporting a feature I have, and I'm certain it's my own inability making it moot. See, this returns a number in Chrome and Firefox, but returns NaN in Safari (latest version, 7).

var something = Date.parse(new Date(dataLabels[0]));
console.log(something); // returns epoch number Chrome/Firefox NOT Safari

It seems obtuse to generate a epoch timestamp from a string like that, but that's the only way I could get it to work in my charting project I noticed this in. Am I approaching the translation of mm/dd/yyyy to epoch wrong?

...I'll file a bug report, but wanted to check here for a programmatic error first.

解决方案

Am I approaching the translation of mm/dd/yyyy to epoch wrong?

Yes. ES5 says:

Unrecognizable Strings or dates containing illegal element values in the format String shall cause Date.parse to return NaN.

Do not leave parsing of date strings to the Date constructor. Until ES5, parsing of strings was entirely implementation dependent. A version of !SO 8601 was introduced with ES5, however it's not supported by all browsers in use and, depending on the string, may be handled as either UTC or local time by different browsers (including the most recent versions).

The safest way is to manually parse the string.

Given that the format is m/d/y, the following will work reliably in all browsers:

// Return a date object given a dates string in m/d/y format
function parseDate(s) {
  var b = s.split(/\D+/);
  return new Date(b[2], --b[0], b[1]);
}

If you are after the actual time value, you can use getTime or just use the unary + operator:

var timeValue = +parseDate('5/24/2014');

Note that when you do:

> Date.parse(new Date(dataLabels[0]))

The date constructor will first parse the string to a date, then convert that to a string, then convert that back to a date, then return the time value. So if the initial parsing failed, the rest will too and if the initial parsing succeeds (and it may or may not depending on the browser) the result will be no different to:

+new Date(dataLabels[0]);

unless the Date constructor is incapable of parsing its own string representation of a Date (which is possible, but not consistent with ECMA-262).

这篇关于Date.parse(new Date(...))Safari中的NaN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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