欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

获取当前日期的周一周日,月初月尾,年初年尾

程序员文章站 2022-05-15 09:53:33
...

该可以直接运行

getFirstDayOfWeek() {
	var now = new Date();
	var nowTime = now.getTime();
	var year = now.getFullYear();
	var month = now.getMonth() + 1;
	var day = now.getDay();
	//显示周一
	var MondayTime = nowTime - (day - 1) * 24 * 60 * 60 * 1000;
	//显示周日
	var SundayTime = nowTime + (7 - day) * 24 * 60 * 60 * 1000;
	//初始化日期时间

	var startYear = new Date(MondayTime).getFullYear();
	var startMonth = new Date(MondayTime).getMonth() + 1;
	var startDay = new Date(MondayTime).getDate();

	var endYear = new Date(SundayTime).getFullYear();
	var endMonth = new Date(SundayTime).getMonth() + 1;
	var endDay = new Date(SundayTime).getDate();

	this.weekStartTime = startYear + '-' + startMonth + '-' + startDay //这周的周一
	this.weekEndTime = endYear + '-' + endMonth + '-' + endDay //这周的周日
	console.log(this.weekStartTime, this.weekEndTime);


	var firstDay = new Date(year, month - 1, 1);
	var lastDay = new Date(year, month, 0);

	var startYear = firstDay.getFullYear();
	var startMonth = firstDay.getMonth() + 1;
	var startDay = firstDay.getDate();

	var endYear = lastDay.getFullYear();
	var endMonth = lastDay.getMonth() + 1;
	var endDay = lastDay.getDate();
	this.monthStartTime = startYear + '-' + startMonth + '-' + startDay //这周的周一
	this.monthEndTime = endYear + '-' + endMonth + '-' + endDay //这周的周日

	this.yearStartTime = startYear + '-1-1'
	this.yearEndTime = startYear + '-12-31'
	
	console.log(this.weekStartTime, this.weekEndTime, this.monthStartTime, 
	this.monthEndTime, this.yearStartTime, this.yearEndTime);
	},