-7

I have string value '05-Jan-18' to be converted to date format using javascript. can someone help me on this.

Herohtar
  • 5,347
  • 4
  • 31
  • 41
Ohviya
  • 11

1 Answers1

-2

You can just do:

var d = new Date("05-Jan-18")

Yeah '05-Jan-18' does not work everywhere. However

new Date("05-Jan-18".replace(new RegExp('-', 'g'), ' '))

should work

kimy82
  • 4,069
  • 1
  • 22
  • 25
  • 2
    The Date construct uses `Date.parse()` which is heavily discouraged at this moment (well, if you use it with non-ISO 8601 format like this one). It is not so reliable if I am talking from my experience. It is better to parse the string yourself or use known libraries. – KarelG Mar 22 '18 at 12:44
  • Not a good idea.. :( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date Even if the date was an ISO 8601 format, due to browsers inconsisteancies even that could go wrong. – Keith Mar 22 '18 at 12:45