js获取当前日期的前几天的日期

setDate() 、 getDate()方法

function getBeforeDate(n){
	var date = new Date() ;
	var year,month,day ;
	date.setDate(date.getDate()-n);
	year = date.getFullYear();
	month = date.getMonth()+1;
	day = date.getDate() ;
	s = year + '-' + ( month < 10 ? ( '0' + month ) : month ) + '-' + ( day < 10 ? ( '0' + day ) : day) ;
	return s ;
}  
console.log(getBeforeDate(1));//昨天的日期   
console.log(getBeforeDate(15));//前15天的日期  
console.log(getBeforeDate(365));//前365天的日期


猜你喜欢

转载自blog.csdn.net/hanxue_tyc/article/details/78120635