Date parse in javascript

javascript date
date parse java
javascript convert date format
javascript string to time
javascript date format dd-mmm-yyyy
javascript new date() returns invalid date
javascript date output format
moment js

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

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

JavaScript Date parse() Method, Date Input - Parsing Dates. If you have a valid date string, you can use the Date.​parse() method to convert it to milliseconds. Date  The parse() method parses a date string and returns the number of milliseconds between the date string and midnight of January 1, 1970.

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>

JavaScript Date Formats, The Date.parse() function is an inbuilt function in JavaScript which helps us to know the exact number of milliseconds that have passed since midnight, January​  Browser compatibility Creates a JavaScript Date instance that represents a single moment in time in a platform-independent format. Date objects contain a Number that represents milliseconds since 1 January 1970 UTC. The source for this interactive example is stored in a GitHub repository.

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"

JavaScript, The best string format for string parsing is the date ISO format together with the JavaScript Date object constructor. Examples of ISO format: YYYY-MM-DD or  If you have a valid date string, you can use the Date.parse() method to convert it to milliseconds. Date.parse() returns the number of milliseconds between the date and January 1, 1970: Example

Date JS is fantastic.

Converting a string to a date in JavaScript, Javascript date parse() method takes a date string and returns the number of milliseconds since midnight of January 1, 1970. Syntax. Its syntax is as follows − Date. new Date(Date.parse(myArr[0][0])); Use the Date.parse method to parse the string into the number of milliseconds since January 1, 1970, 00:00:00 UTC. Take that number of milliseconds and call the Date method once again to turn that time into a date object.

JavaScript - Date parse() Method, The Date object is a data type built into the JavaScript language. Date objects are created with the new Date( ) as shown below. Once a Date  JavaScript JS Array. JavaScript Date Reference parse() Parses a date string and returns the number of milliseconds since January 1, 1970:

Date.parse() function in JavaScript, Date.parse will return a timestamp (in milliseconds) rather than a Date object. You can also pass a set of ordered values that represent each part  By default, JavaScript will use the browser's time zone and display a date as a full text string: You will learn much more about how to display dates, later in this tutorial.

The definitive guide to JavaScript Dates, parse uses, we'll cover it later. let date = new Date ( "2017  Parsing Dates. Date objects are not allowed in JSON. If you need to include a date, write it as a string. You can convert it back into a date object later:

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.)
  • 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