1

I need to parse the date in javascript for 17Dec2010 as javascript date. How to do that?

3 Answers 3

3

The short answer is: There's no standard means in JavaScript for doing that, you'll have to do it yourself. JavaScript only recently got any standard string representation for dates (as of ECMAScript 5th edition, about a year ago — the format is a simplified version of ISO-8601), and your format doesn't match that format.

However, there are add-on libraries that can help, such as DateJS.

Your particular format is pretty easy to parse (see below), but if you get into variations, it can get complex fast.

Simple example:

var months = {
    en: {
        "jan": 0,
        "feb": 1,
        "mar": 2,
        "apr": 3,
        "may": 4,
        "jun": 5,
        "jul": 6,
        "aug": 7,
        "sep": 8,
        "oct": 9,
        "nov": 10,
        "dec": 11
    }
};
var dateString = "17Dec2010";
var dt = new Date(
    parseInt(dateString.substring(5), 10),               // year
    months.en[dateString.substring(2, 5).toLowerCase()], // month
    parseInt(dateString.substring(0, 2), 10)             // day
);
alert(dt); // alerts "Fri Dec 17 2010 00:00:00 GMT+0000 (GMT)" or similar

Live example

...but that only handles English (hence the en property of months) and again, it can get complex fairly quickly.


mplungjan quite correctly points out that the above will fail on, say "7Dec2010". Here's a version that's a bit more flexible, but again, I'd probably look to a library if there's any variety in the format:

var months = {
    en: {
        "jan": 0,
        "feb": 1,
        "mar": 2,
        "apr": 3,
        "may": 4,
        "jun": 5,
        "jul": 6,
        "aug": 7,
        "sep": 8,
        "oct": 9,
        "nov": 10,
        "dec": 11
    }
};
var dateString = "17Dec2010";
var parts = /^(\d+)(\D+)(\d+)$/.exec(dateString);
if (parts && parts.length == 4) {
  var dt = new Date(
      parseInt(parts[3], 10),            // year
      months.en[parts[2].toLowerCase()], // month
      parseInt(parts[1], 10)             // day
  );
  display(dt);
}
else {
  display("Date '" + dateString + "' not recognized");
}

Live example

Sign up to request clarification or add additional context in comments.

4 Comments

@mplugjan: Like I said, complexities... But you're quite right, it's plenty easy to break things up with a regex rather than substring -- I may as well update to do that... (Edit: Done)
Great. I was missing the () to capture the parts. My name is mplungjan btw ;)
@mplungjan: Sorry, I missed out the "g" both times, didn't I?
@mplungjan: Yeah. :-) (Neither can I.)
1

Using Crowder's regExp instead of the one I came up with when I first answered, there is no need for a month array. My script was tested in IE8, Fx 3.6, Opera 10, Safari 5, Mozilla 1.7 on windows and Blackberry native browser. If the month part of your string is English, then all major browsers and even some not so major ones - I expect all browsers with javascript support and Date.parse support will handle my script just fine. MDC says Date.parse does RFC822

<script type="text/javascript">
var date = "12Dec2011"
var reg = /^(\d+)(\D+)(\d+)$/
var parts = reg.exec(date)
var dateString = parts[1]+' '+parts[2]+' '+parts[3]; // parts[0] is the match itself
alert(new Date(Date.parse(dateString)))
</script>

3 Comments

Re a better regex: The regex I used in my updated answer (after your comment) is /(\d+)(\D+)(\d+)$/, which means "a series of one or more digits followed by a series of one or more non-digits followed by a series of one or more digits".
Re "No need for a month array - at least not for English" That's not specified behavior, and so you shouldn't rely on it (and I bet it won't work in non-English localized browsers). There is no requirement in the spec that either the Date constructor or Date.parse accept month names, in English or otherwise. Some implementations do it, but it's implementation-dependant, not specified behavior. See Section 15.9.1.15 of the spec.
Please see my update. I am pretty pragmatic about such things and do not read the ECMAscript spec when I code. I prefer to test in real life :) tools.ietf.org/html/rfc822#section-5
0

Here's a small (once you take the comments out, at least) utility I wrote based on Python's time module - it takes format strings and generates RegExps accordingly to extract date information. The unit tests give a flavour of what it does without having to grok the code, but in your case:

>>> var parts = time.strptime('17Dec2010', '%d%b%Y');
>>> var date = new Date(parts[0], parts[1]-1, parts[2]);
Fri Dec 17 2010 00:00:00 GMT+0000 (GMT Standard Time) {}

It'll also take care of basic validation:

>>> try
... {
...     time.strptime('32Dec2010', '%d%b%Y');
... }
... catch (e)
... {
...     console.log(e);
... }
Error: Day is out of range: 32

It can also go the other way too, if you need that:

>>> time.strftime(date, '%d%b%Y');
"17Dec2010"

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.