最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Date.parse(new Date(...)) NaN in Safari - Stack Overflow

programmeradmin1浏览0评论

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.

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.

Share Improve this question asked May 3, 2014 at 5:07 user393219user393219 3
  • does this also happen in IE9? i had a similar issue in that browser but im surprised its happening in safari and not chrome, also why are you parsing a new date object? – undefined Commented May 3, 2014 at 5:18
  • I haven't been able to test it on Windows at all, but I will update when I do. I suppose I'm manipulating like I am because it's in a format that I can't use, and when it give it a string, it mistakes mm/dd for dd/mm. – user393219 Commented May 3, 2014 at 16:11
  • 1 Using the Date constructor to parse date strings is known to be faulty across browsers. There is only one standardised format and it isn't supported by all browsers in use. Manually parse the string. – RobG Commented May 3, 2014 at 22:51
Add a ment  | 

2 Answers 2

Reset to default 3

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(dataLabels[0]).toString()) should work across browsers. In a less obtuse way, new Date(dataLabels[0]).valueOf() should also have the same effect. valueOf() is the toString() equivalent that returns a number, and it's well-supported too.

发布评论

评论列表(0)

  1. 暂无评论