JavaScrip常用日期操作

//当前日期是星期几
function showTodayWeek()
{
	var nowDate = new Date();
	//nowDate.setFullYear(2011,6,1);
	return nowDate.getDay();
	//0 为星期日
   	//1 为星期一
  	//2 为星期二
  	//3 为星期三
  	//4 为星期四
  	//5 为星期五
  	//6 为星期六
}

//本周第一天日期
function showWeekFirstDay()
{
	var now = new Date();
        var weekFirstDay = new Date(now-(now.getDay()-1)*86400000);
	return weekFirstDay;
}

//本周最后一天日期
function showWeekLastDay()
{
 	var nowDate = new Date();
	var weekFirstDay = new Date(nowDate-(nowDate.getDay()-1)*86400000);
	var weekLastDay = new Date((weekFirstDay/1000+6*86400)*1000);
	return weekLastDay;
}

//本月第一天日期
function showMonthFirstDay()
{
	var nowDate = new Date();
	var monthFirstDay = new Date(nowDate.getYear(),nowDate.getMonth(),1);
	return monthFirstDay;
}

//本月最后一天日期
function showMonthLastDay()
{
	var nowDate = new Date();
	var tmpDate = new Date(nowDate.getYear(),nowDate.getMonth()+1,1);
	var monthLastDay = new Date(tmpDate-86400000);
	return monthLastDay;
}

//本季度第一天
function showQuarterFirstDay()
{
	var nowDate = new Date();
	if (nowDate.getMonth()<3) return 
		new Date(now Date.getYear(),0,1);
	else if (nowDate.getMonth()>2 && nowDate.getMonth()<6) 
		return new Date(nowDate.getYear(),3,1);
	else if (nowDate.getMonth()>5 && nowDate.getMonth()<9)
		return new Date(nowDate.getYear(),6,1);
	else if (nowDate.getMonth()>8)
		return new Date(newDate.getYear(),9,1);
}
</script>

 

 

注意:表示月份的参数介于 0 到 11 之间。也就是说,如果希望把月设置为 8 月,则参数应该是 7。

注意:如果增加天数会改变月份或者年份,那么日期对象会自动完成这种转换。

你可能感兴趣的:(javascrip)