日期:2014-05-16 浏览次数:20477 次
function GetDay() { var today=new Date(); var yesterday_milliseconds=today.getTime()-1000*60*60*24; var yesterday=new Date(); yesterday.setTime(yesterday_milliseconds); var strYear=yesterday.getFullYear(); var strDay=yesterday.getDate(); var strMonth=yesterday.getMonth()+1; if(strMonth<10) { strMonth="0"+strMonth; } var strYesterday=strYear+"-"+strMonth+"-"+strDay; alert(strYesterday); }
s = "20120301"; alert(getPreDay(s)); function getPreDay(s){ var y = parseInt(s.substr(0,4), 10); var m = parseInt(s.substr(4,2), 10)-1; var d = parseInt(s.substr(6,2), 10); var dt = new Date(y, m, d-1); y = dt.getFullYear(); m = dt.getMonth()+1; d = dt.getDate(); m = m<10?m:"0"+m; d = d<10?d:"0"+d; return y + "" + m + "" + d; }
------解决方案--------------------
正解
function GetDay(year, month, day) { var today=new Date(year, month-1 ,day); var yesterday_milliseconds=today.getTime()-1000*60*60*24; var yesterday=new Date(); yesterday.setTime(yesterday_milliseconds); var strYear=yesterday.getFullYear(); var strDay=yesterday.getDate(); var strMonth=yesterday.getMonth()+1; if(strMonth<10) { strMonth="0"+strMonth; } var strYesterday=strYear+"-"+strMonth+"-"+strDay; alert(strYesterday); } GetDay(2012, 1, 5);