JS的关于星期

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<script>
			// 中国标准时间转换为特定格式时间(以2022-03-02为例)
			function initDate(date){
			  return  date.getFullYear() + "-" + (date.getMonth()> 9 ? (date.getMonth() + 1) : "0" + (date.getMonth() + 1)) + "-" +(date.getDate()> 9 ? (date.getDate()) : "0" + (date.getDate()));
			};
			// 本周、上周的开始日期和结束日期
			function getTime(n) {
				let now = new Date();
				let year = now.getFullYear();
				let month = now.getMonth() + 1;
				let day = now.getDay(); //返回星期几的某一天;
				n = day == 0 ? n + 6 : n + (day - 1)
				now.setDate(now.getDate() - n);
				let date = now.getDate();
				let s = year + "-" + (month < 10 ? ('0' + month) : month) + "-" + (date < 10 ? ('0' + date) : date);
				return s;
			};
			/***参数都是以周一为基准的***/
			//上周的开始时间
			console.log('上周的开始时间:' + this.getTime(7));
			//上周的结束时间
			console.log('上周的结束时间:' + this.getTime(1));
			//本周的开始时间
			console.log('本周的开始时间:' + this.getTime(0));
			//本周的结束时间
			console.log('本周的结束时间:' + this.getTime(-6));
			//下周的开始时间
			console.log('下周的开始时间:' + this.getTime(-7));
			//下周的结束时间
			console.log('下周的结束时间:' + this.getTime(-13));
			
			// 上月开始时间和结束时间
			function lastMonth(){
			    let nowday = new Date();
			    let year = nowday.getFullYear();
			    let month = nowday.getMonth();
			    if(month==0){
			        month=12;
			        year=year-1;
			    }
			    if (month < 10) {
			        month = "0" + month;
			    }
			    let firstDayOfPreMonth = year + "-" + month + "-" + "01";
			    let lastDay = new Date(year, month, 0);
			    let lastDayOfPreMonth = year + "-" + month + "-" + lastDay.getDate();
			    firstDayOfPreMonth = firstDayOfPreMonth.toString();
			    lastDayOfPreMonth = lastDayOfPreMonth.toString();
			    let value4 = [];
			    value4.push(firstDayOfPreMonth); //开始时间
			    value4.push(lastDayOfPreMonth); //结束时间
			    return value4
			};
			console.log('上月开始时间和结束时间:' + lastMonth());
			// 输出结果为 2022-02-01,2022-02-28
			
			// 本月开始日期、结束日期
			function CurrentMonthLast(){
				let date=new Date();
				let currentMonth=date.getMonth();
				let nextMonth=++currentMonth;
				let nextMonthFirstDay=new Date(date.getFullYear(),nextMonth,1);
				let oneDay=1000*60*60*24;
				let start = this.initDate(new Date(new Date().setDate(1)))
				let end = this.initDate(new Date(nextMonthFirstDay-oneDay))
				return  [start,end] 
			};
			console.log('本月开始日期、结束日期:' + CurrentMonthLast());
			//输出结果 2022-03-01,2022-03-31
		</script>
	</head>
	<body>
	</body>
</html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值