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

javascript - JS Date() - Leading zero on days - Stack Overflow

programmeradmin1浏览0评论

A leading zero for the day within a string seems to break the Javascript Date object in Chrome. There are also some inconsistencies between browsers, since Firefox handles the leading zero correctly, but fails when the zero is not included. See this example: /

Date('2015-11-01'); // works in Firefox, not in Chrome

Date('2015-11-1'); // works in Chrome, not in Firefox

Why? Is there a good way to work around/with the leading zero?

Please note, the strings are ing from MySQL via AJAX and all dates will contain the leading zero, and I can fix this by formating the dates server-side. What format would work the best?

EDIT

Just to specify what my problem was, it looks like Chrome is applying a time zone to the YYYY-MM-DD format, which reverts the Nov. 1st date back to the Oct. 31st date (because of my EDT local time).

A leading zero for the day within a string seems to break the Javascript Date object in Chrome. There are also some inconsistencies between browsers, since Firefox handles the leading zero correctly, but fails when the zero is not included. See this example: https://jsfiddle/3m6ovh1f/3/

Date('2015-11-01'); // works in Firefox, not in Chrome

Date('2015-11-1'); // works in Chrome, not in Firefox

Why? Is there a good way to work around/with the leading zero?

Please note, the strings are ing from MySQL via AJAX and all dates will contain the leading zero, and I can fix this by formating the dates server-side. What format would work the best?

EDIT

Just to specify what my problem was, it looks like Chrome is applying a time zone to the YYYY-MM-DD format, which reverts the Nov. 1st date back to the Oct. 31st date (because of my EDT local time).

Share Improve this question edited Nov 2, 2015 at 15:34 Siphon asked Oct 30, 2015 at 15:33 SiphonSiphon 1,06110 silver badges18 bronze badges 7
  • Date('2015-11-01') works fine in Chrome for me. – Pointy Commented Oct 30, 2015 at 15:35
  • Possible duplicate of Difference between '01' and '1' in a JavaScript date – rajuGT Commented Oct 30, 2015 at 15:35
  • Here is the spec with the standard JavaScript date format. – Pointy Commented Oct 30, 2015 at 15:37
  • @rajuGT That doesn't tell me how to fix the problem; those answers just tell me what I already know. – Siphon Commented Oct 30, 2015 at 15:40
  • @Pointy It looks like a full date, including time and time zone, could be the best format. Thanks for the documentation. – Siphon Commented Oct 30, 2015 at 15:43
 |  Show 2 more ments

2 Answers 2

Reset to default 7

According to ECMA-262 (5.1):

The function first attempts to parse the format of the String according to the rules called out in Date Time String Format (15.9.1.15). If the String does not conform to that format the function may fall back to any implementation-specific heuristics or implementation-specific date formats.

The date/time string format as described in 15.9.1.15 is YYYY-MM-DDTHH:mm:ss.sssZ. It can also be a shorter representation of this format, like YYYY-MM-DD.

2015-11-1 is not a valid date/time string for Javascript (note it's YYYY-MM-D and not YYYY-MM-DD). Thus, the implementation (browser) is able to do whatever it wants with that string. It can attempt to parse the string in a different format, or it can simply say that the string is an invalid date. Chrome chooses the former (see DateParser::Parse) and attempts to parse it as a "legacy" date. Firefox seems to choose the latter, and refuses to parse it.

Now, your claim that new Date('2015-11-01') doesn't work in Chrome is incorrect. As the string conforms to the date/time string format, Chrome must parse it to be specification pliant. In fact, I just tried it myself -- it works in Chrome.

So, what are your options here?

  1. Use the correct date/time format (i.e. YYYY-MM-DD or some extension of it).

  2. Use the new Date (year, month, date) constructor, i.e. new Date(2015, 10, 1) (months go from 0-11) in this case.

Whichever option is up to you, but there is a date/time string format that all specification pliant browsers should agree on.

As an alternative, why not use unix timestamps instead? In JavaScript, you would would multiply the timestamp value by 1000, e.g

var _t = { time: 1446220558 };
var _d = new Date( _t.time*1000 );

Test in your browser console:

new Date( 14462205581000 );
// prints Fri Oct 30 2015 11:55:58 GMT-0400 (EDT)

There's a little benefit in it as well (if data es via JS) - you'd save 2 bytes on every date element '2015-10-30' VS 1446220558 :)

发布评论

评论列表(0)

  1. 暂无评论