javascript 得到任意间隔的日期

//得到今天为起点的任意一天地日期
function getDateByCount(addDayCount) {
    var dd = new Date();
    dd.setDate(dd.getDate()+addDayCount);//获取addDayCount天后的日期
    var y = dd.getFullYear();
    var m = dd.getMonth()+1;
    var d = dd.getDate();
    return y+"-"+m+"-"+d;
}

//得到以 otherDate 为起点的任意一天的日期
function getDateByCountWithOtherDate(otherDate,addDayCount) {
		if(!otherDate || otherDate.constructor != Date){
			alert('otherDate 不是日期类型,请检查!');
			return;
		}
		var dd = new Date(otherDate.getTime());
		dd.setDate(dd.getDate()+addDayCount);//获取addDayCount天后的日期
		return dd;
}

document.write("<br />昨天:"+getDateByCount(-1));
document.write("<br />今天:"+getDateByCount(0));
document.write("<br />明天:"+getDateByCount(1));
document.write("<br />后天:"+getDateByCount(2));

 

你可能感兴趣的:(JavaScript)