0

I am currently trying to convert a dd/mm/yyyy string to a date so i can compare it to another date.

The string, for example, is 16/12/2015 but when i use the below code to convert it to a date it just gives random months / years for example 19/11/1902.

Here is the code:

date = (date.substring(0, 6) + year);
var dsplit = date.split("/");
var myDate=new Date(dsplit[0],dsplit[1]-1,dsplit[2]);

in that above code dssplit[0] is 16, next one is 12 and the last one is 2015 - im not sure why it is doing this.

gpgekko
  • 3,506
  • 3
  • 32
  • 35
Gaz Smith
  • 1,100
  • 1
  • 16
  • 30

2 Answers2

2

You should try the below:

    var date = "16/12/2015";
    var dsplit = date.split("/");
    var showDate = new Date(dsplit[2], dsplit[1] - 1, dsplit[0]);
    alert(showDate)

Please see the jsfiddle here demonstrating this

Paul Fitzgerald
  • 11,770
  • 4
  • 42
  • 54
0

You have wrong order of arguments:

var date = '16/12/2015';
var dsplit = date.split('/');
var myDate = new Date(dsplit[2], dsplit[1]-1, dsplit[0]);
alert(myDate);
jcubic
  • 61,973
  • 54
  • 229
  • 402