Reputation: 3610
I want to parse date
var newDateTime = new Date(Date.parse($("#SelctedCalendarDay").val()));
$("#SelctedCalendarDay").val()
value is 14-okt-2014 in string.
When the date is 14-Oct-2014 it parses it correctly.
So how can I parse this date 14-okt-2014?
Upvotes: 0
Views: 432
Reputation: 147553
I'm not sure what language you are using (Dutch?) so I'll use English, it's easy to substitute whatever language you like. You can parse a date string like 14-Oct-2014 using:
function parseDMMMY(s) {
// Split on . - or / characters
var b = s.split(/[-.\/]/);
// Months in English, change to whatever language suits
var months = {jan:0, feb:1, mar:2, apr:3, may:4, jun:5,
jul:6, aug:7, sep:8, oct:9, nov:10, dec:11};
// Create a date object
return new Date(b[2], months[b[1].toLowerCase()], b[0]);
}
console.log(parseDMMMY('14-Oct-2014')); // Tue Oct 14 2014 00:00:00
Note that the above will create a local date object. It doesn't do any validation of values so if you give it a string like 31-Jun-2014 you'll get a Date object for 01-Jul-2014. It's easy to add validation (it takes one more line of code), but that may not be required if you know only valid strings will be passed to the function.
Upvotes: 1
Reputation: 2239
From the documentation of Date.parse @ Mozilla
The date time string may be in ISO 8601 format. For example, "2011-10-10" (just date) or "2011-10-10T14:48:00" (date and time) can be passed and parsed. The UTC time zone is used to interpret arguments in ISO 8601 format that do not contain time zone information (note that ECMAScript ed 6 draft specifies that date time strings without a time zone are to be treated as local, not UTC).
It seems to me the native Date.parse does not convert date strings matching a specific locale. There also seem to be some slight differences between specific browsers.
Maybe you want to use a library like moment.js for that?
Or you may want to wire up some PHP strtotime to your JS.
Upvotes: 0