首页 > 代码库 > js 时间
js 时间
function preDate(num){
if(isNaN(num)){
alert("参数必须为数字");
return;
}
var now = new Date(),
year = now.getFullYear(),
month = now.getMonth()+ 1,
day = now.getDate(),
monthDay = [31, (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0) ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
var m = day <= num ? month-1 : month,
d = day <= num ? monthDay[m] : day,
y = m <= 0 ? year-1 : year, //推导年
m = m <= 0 ? 12 : m, //推导月
d = d - num + (day <= num ? day : 0);
if(d<10){
d="0"+d
}
if(m<10){
m=‘0‘+m
}
return y+"/"+m+"/"+d
}
alert( preDate(0) ) //今天的日期 2016/09/23
alert( preDate(1) ) //昨天的日期 2016/09/22
js 时间