简体   繁体   中英

how to get date ,year , month from datetime using javascript?

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 .

try this

var month = newdate.getUTCMonth();
var day = newdate.getUTCDate();
var year = newdate.getUTCFullYear();

when newdate is the date object

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM