<!DOCTYPE html> <html> <head> <meta charset="{CHARSET}"> <title></title> <script> // 第一个参数为日期,第二个参数为年月日分割格式 '/'或'-' function format(Date,str){ var obj = { Y: Date.getFullYear(), M: Date.getMonth() + 1, D: Date.getDate(), H: Date.getHours(), Mi: Date.getMinutes(), S: Date.getSeconds() } // 拼接时间 hh:mm:ss var time = ' ' +supplement(obj.H) + ':' + supplement(obj.Mi) + ':' + supplement(obj.S); // yyyy-mm-dd if(str.indexOf('-') > -1){ return obj.Y + '-' + supplement(obj.M) + '-' + supplement(obj.D) + time; } // yyyy/mm/dd if(str.indexOf('/') > -1){ return obj.Y + '/' + supplement(obj.M) + '/' + supplement(obj.D) + time; } } // 位数不足两位补全0 function supplement(nn){ return nn = nn < 10 ? '0' + nn : nn; } var todaydate = new Date(); var end_time = format(todaydate,'-'); alert(end_time); var predata = new Date(todaydate-24*3600*1000); var start_time = format(predata,'-'); alert(start_time);
</script> </head> <body> 1212 </body> </html>