Madhan
Madhan

Reputation: 53

new Date() function returns wrong value

The new date(code) returns working value,Please Help me

var cdt = new Date();

dob = "15/01/1999";//From date picker
alert(dob);

    var bdy = dob.split("/");
    var by = bdy[2];
    var bm = bdy[0];
    var bd = bdy[1];

    var dob = new Date(bd, bm, by);
alert(bd+","+bm+","+by);
alert(dob);

Date format changed for new date() function:

enter image description here

Values return by that function:

enter image description here

Upvotes: 0

Views: 298

Answers (2)

Alessandro
Alessandro

Reputation: 4472

You could use JavaScript ISO Dates format that is the format: yyyy-mm-dd, see following example please:

var dString = "15/01/1999";
console.log("From date picker", dString);

var bdy = dString.split("/").reverse().join("-")
var dob = new Date(bdy);

console.log("Javascript Date" , dob);

I hope it helps you, bye.

Upvotes: 1

Mihai Alexandru-Ionut
Mihai Alexandru-Ionut

Reputation: 48327

new Date() method takes three parameters on constructor.

The order of parameters is following: year,month and day.

Something like this: var date=new Date(1999,01,01).

var cdt = new Date();
dob = "15/01/1999";//From date picker

var bdy = dob.split("/");
var by = bdy[2];
var bm = bdy[1];
var bd = bdy[0];
var dob = new Date(by, (bm-1), bd);
console.log(bd+","+bm+","+by);
console.log(dob.toLocaleDateString());

Upvotes: 1

Related Questions