根据输入的日期计算出上一周每天的日期



<script>
	function getFirstWeekDate( date ) {
		var currentTime = date.getTime();
		var day = date.getDay();
		var passTime = (day + 7) * 86400000; // 上周日至当前日所经过的毫秒数
		var firstDay = new Date(currentTime - passTime);	// 当前日毫秒数-去所经过的毫秒数,得出上周日的日期 	
		var weekDays = new Array();
		for(var i=0; i<7; i++){
			var date = new Date(firstDay.getTime() + (86400000 * i));
			weekDays.push(date); //上一周的日期对象
		}
		return weekDays;
	}

	var currentDate = new Date();
	currentDate.setFullYear(2015);
	var month = 1;		//月份
	currentDate.setMonth(month - 1);
	currentDate.setDate(1);
	console.log(currentDate);
	var dates = getFirstWeekDate(currentDate);
	var zh_cn = ["日","一","二","三","四","五","六"]
	for(var i=0; i<dates.length; i++){
		var d = dates[i].getFullYear() + "-" + (dates[i].getMonth()+1) + "-" + dates[i].getDate()
		console.log( "上星期"+zh_cn[i]+"是:"+d);
	}	 
</script>


你可能感兴趣的:(日期)