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?
-
@all: No, that string can't be used reliably with
new Date
orDate.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
2 Answers
Reset to default 6Non-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).
本文标签:
版权声明:本文标题:regex - Parsing a string containing a date in a specific format into a Date object in Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744087540a2588807.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论