admin管理员组

文章数量:1395408

I'm learning Javascript as a new language, and I figured a good way to get used to how JS handles reg exp and string manipulation is to parse a string containing a date into a Date object.

I have a string in the form "2005-05-28". What is the best Javascript-y way to parse that into a Date object?

I'm learning Javascript as a new language, and I figured a good way to get used to how JS handles reg exp and string manipulation is to parse a string containing a date into a Date object.

I have a string in the form "2005-05-28". What is the best Javascript-y way to parse that into a Date object?

Share Improve this question asked Dec 13, 2010 at 23:29 Justin L.Justin L. 13.6k5 gold badges50 silver badges83 bronze badges 1
  • @all: No, that string can't be used reliably with new Date or Date.parse. Not yet. (There was an early, now-deleted answer saying you could, and now a second one.) That will fail on all currently-released versions of IE, for instance: jsbin./otite5 That ISO 8601 subset stuff was only standardized as of the 5th ed. spec (~a year ago). Previously, there was no standard string-to-Date conversion in the spec (just the statement that it "should" handle whatever Date-to-string output). The new stuff is a very wele addition, but it'll take time for implementations to catch up. – T.J. Crowder Commented Dec 14, 2010 at 6:28
Add a ment  | 

2 Answers 2

Reset to default 6

Non-RegExp

I don't know about "best", but you don't need a regex:

var str = "2005-05-28";
var parts = str.split("-");
var dt = new Date(parseInt(parts[0], 10),
                  parseInt(parts[1], 10) - 1,
                  parseInt(parts[2], 10));

Live example

That uses String#split to break the string on the - characters, then the Date constructor that accepts year, month (starting with 0), and day (starting with 1).

RegExp

Mind you, if you really want to use a regexp, you can:

var str = "2005-05-28";
var parts = str.match(/^(\d{1,4})-(\d{1,2})-(\d{1,2})$/);
if (parts) {
    var dt = new Date(parseInt(parts[1], 10),
                      parseInt(parts[2], 10) - 1,
                      parseInt(parts[3], 10));
}

Live example

That uses a regex with capture groups, doing the match via String#match, which returns an array with the full match at index 0 followed by the capture groups. But for my money, the regex isn't really buying you anything.

Beyond

If your date strings get more plicated or may vary, I'd look at farming the problem out to a library like DateJS. But if your format is that regular, you're golden.

That format is acceptable for a standard JS date. So you only have to do:

var dateObject = new Date("2005-05-28");

EDIT: The above is wrong and TJ is right; I've been busted with a lazy answer. And it even parses incorrectly in Firefox, subtracting one from the date.

Seeing that this question was trickier than I thought, I ran some tests carefully. IE (and others) will accept a slash instead of a dash (implementation trumps specification). So, the above will work with a simple replace():

var dateObject = new Date(("2005-05-28").replace("-", "/"));

Note that I also tested this successfully with MM/DD/YYYY:

var dateObject = new Date("05/28/2005");

So the slashes will parse in all major browsers (including IE6).

本文标签: