前端计算当天、本周、本月、本季、今年的起止日期

这篇博客分享了如何在前端计算并获取当前日期的当天、本周、本月、本季和今年的起止日期。提供了完整代码示例,并且代码已经通过eslint校验,适用于vue项目。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.完整代码

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<button type="button" onclick="btnClick(1)">今天</button>
		<input id="day"></input>
		<br/>
		<br/>
		<button type="button" onclick="btnClick(2)">本周</button>
		<input id="week"></input>
		<br/>
		<br/>
		<button type="button" onclick="btnClick(3)">本月</button>
		<input id="month"></input>
		<br/>
		<br/>
		<button type="button" onclick="btnClick(4)">本季</button>
		<input id="quarter"></input>
		<br/>
		<br/>
		<button type="button" onclick="btnClick(5)">今年</button>
		<input id="year"></input>
		<script type="text/javascript">
			// 日期格式化
			Date.prototype.format = function(format) {
				var o = {
					"M+": this.getMonth() + 1, //month
					"d+": this.getDate(), //day
					"h+": this.getHours(), //hour
					"m+": this.getMinutes(), //minute
					"s+": this.getSeconds(), //second
					"q+": Math.floor((this.getMonth() + 3) / 3), //quarter
					"S": this.getMilliseconds() //millisecond
				}
				if (/(y+)/.test(format))
					format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
				for (var k in o)
					if (new RegExp("(" + k + ")").test(format))
						format = format.replace(RegExp.$1,
							RegExp.$1.length == 1 ? o[k] :
							("00" + o[k]).substr(("" + o[k]).length));
				return format;
			}

			function chooseDate(val) {
				var params;
				var now = new Date() // 当前日期
				var nowDayOfWeek = now.getDay() - 1 // 获取今天是本周的第几天,通过减1将周一改为本周的第一天
				var nowDay = now.getDate() // 当前日
				var nowMonth = now.getMonth() // 当前月
	
以下是Python的datetime实现本周、上周、本月和上月起止日期的代码: ```python import datetime # 本周起止日期 now = datetime.datetime.now() start_of_week = now - datetime.timedelta(days=now.weekday()) end_of_week = start_of_week + datetime.timedelta(days=6) # 上周起止日期 start_of_last_week = start_of_week - datetime.timedelta(days=7) end_of_last_week = end_of_week - datetime.timedelta(days=7) # 本月起止日期 start_of_month = datetime.datetime(now.year, now.month, 1) if now.month == 12: end_of_month = datetime.datetime(now.year+1, 1, 1) - datetime.timedelta(days=1) else: end_of_month = datetime.datetime(now.year, now.month+1, 1) - datetime.timedelta(days=1) # 上月起止日期 last_month = now.month - 1 if now.month > 1 else 12 last_year = now.year - 1 if last_month == 12 else now.year start_of_last_month = datetime.datetime(last_year, last_month, 1) end_of_last_month = datetime.datetime(now.year, now.month, 1) - datetime.timedelta(days=1) # 输出起止日期 print("本周起止日期:", start_of_week.date(), "-", end_of_week.date()) print("上周起止日期:", start_of_last_week.date(), "-", end_of_last_week.date()) print("本月起止日期:", start_of_month.date(), "-", end_of_month.date()) print("上月起止日期:", start_of_last_month.date(), "-", end_of_last_month.date()) ``` 其中关键的方法是`datetime.timedelta()`表示时间差,例如`datetime.timedelta(days=7)`表示7天时间差。`datetime.datetime()`则表示一个具体的日期时间。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值