Reputation: 19
I am trying to get the year, month, date, hour, and minutes from a datetime
in Javascript. This is my code:
var newdatestring=document.getElementById("2012-12-11 10:36").value;
var newdate=new Date(newdatestring);
newdate.setMinutes ( newdate.getMinutes() + 30 );
var formdate=newdate.getYear()+"-"+(newdate.getMonth()+1)+"-"+newdate.getDate()+" "+newdate.getHours()+":"+newdate.getMinutes();
alert(formdate);
this code working fine in Chrome and Opera browsers, but it doesn't work in IE or Firefox. In those browsers, it shows something like Nan-Nan-Nan Nan:Nan
.
Upvotes: 1
Views: 2492
Reputation: 1384
try this
var month = newdate.getUTCMonth();
var day = newdate.getUTCDate();
var year = newdate.getUTCFullYear();
when newdate
is the date object
Upvotes: 1