9

I am using Date.parse to convert a string to a date in javascript, however, if the string looks like this '10/11/2016' it is interpreted as Oct 11 2016, i need it to be interpreted as Nov 10 2016

Suggestions?

D.B
  • 4,009
  • 14
  • 46
  • 83
  • 2
    You can't change the format that the function accepts, so you have to just format it differently. Or split on `/` and make a date object by passing in the separate values `new Date(year, month-1, day)`. – Jonathan Kuhn Nov 09 '16 at 23:45
  • According to the documentation, the format must be: "A string representing an [RFC2822](http://tools.ietf.org/html/rfc2822#section-3.3) or ISO 8601 date (other formats may be used, but results may be unexpected)." –  Nov 10 '16 at 00:53
  • @Amy—that might be what [*MDN says*](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse), but the language specification does not mention RFC2822 at all. MDN also says "*It is not recommended to use Date.parse…*". – RobG Nov 10 '16 at 01:00

1 Answers1

9

By default Date.parse consider in this format that month precede the day to be in your case MM/DD/YYYY not as you want DD/MM/YYYY.

I prefer/suggest using 3rd party date parser library as Moment.js

It can take your date-string and the format to be like this:

moment("10/11/2016", "DD-MM-YYYY");
Basim Hennawi
  • 2,651
  • 3
  • 19
  • 31
  • 3
    Your "by default" statement is not consistent with EMCA-262. Except for ISO 8601 extended format, parsing of date strings with Date.parse and the Date constructor is implementation dependent. – RobG Nov 10 '16 at 00:59