I have string value '05-Jan-18' to be converted to date format using javascript. can someone help me on this.
Asked
Active
Viewed 48 times
-7
-
You can use [`moment.js`](https://momentjs.com/) or other libraries if you are lazy. Or do some worthy research utilizing javascript's `Date` API – KarelG Mar 22 '18 at 12:42
-
`var date = new Date(str_val);` – David Mar 22 '18 at 12:43
1 Answers
-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
-
2The 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