Josh
Josh

Reputation: 1058

Javascript creating new Date from UK Formated datetime in Chrome returns invalid date

I am attempting to debug an issue causing chrome to have issues. It seems to occur when a UK formated datetime string is passed to the Date constructor. My system is all en-us I just had to change my browser settings to en-uk for testing purposes, not sure if that matters.

I have simplified the problem down to the following

<html>
<body>
<script>
    alert('hi')
    var inactiveDateValue = new Date("18/04/2013");
    alert(inactiveDateValue);
</script>
Testing some DateTime Functionality with JS.
</body>
</html>

I have set my language to English (UK) as my highest priority.

EDIT: I need to be able to parse USA or UK datetime formats so the value may be 04/18/2013 OR 18/04/2013.

Upvotes: 0

Views: 691

Answers (1)

Artyom Neustroev
Artyom Neustroev

Reputation: 8715

If your goal is to parse string DD/MM/YYYY to JavaScript Date, you can do the following:

var parseUKDate = function (source, delimiter) {
   return new Date(source.split(delimiter).reverse().join(delimiter))
};

var activeDateValue = parseUKDate("18/04/2013", "/");
alert(activeDateValue);

Upvotes: 1

Related Questions