I want to assign date to new date object. when I am using it is not giving correct result http://jsfiddle.net/Cqw5c/
new Date (12/12/2012)
I want to assign date to new date object. when I am using it is not giving correct result http://jsfiddle.net/Cqw5c/
new Date (12/12/2012)
function getD() {
var d = new Date("12/12/2012");
var month = d.getMonth()+1;
var day = d.getDate();
var year = d.getFullYear();
alert( month + "/" + day + "/" + year);
}
Update
HTML:
$('#clickme').click(function() {
var thetestdate = $('#testdate').val();
mynewdate = getD(thetestdate);
alert(mynewdate);
});
JS:
function getD(mydatevariable) {
var d = new Date(mydatevariable);
var month = d.getMonth()+1;
var day = d.getDate();
var year = d.getFullYear();
var wholedate = ( month + "-" + day + "-" + year);
return wholedate;
}
Quote the argument:
var d= new Date("12/12/2012");
Here's an updated fiddle. You're currently creating a date with the result of the expression 12/12/2012
, which is three numbers and two division operators:
console.log(12 / 12 / 2012); // 0.0004970178926441351
You're creating a new date based on
(12 / 12) / 2012 == 0.0004970178926441351
which will be some fraction of a second after January 1, 1970.
You can create a Javascript date by parsing a string:
new Date( "12/12/2012");
or by passing in individual components:
new Date( 2012, 12, 12 );
See Date at MDN for more variations.