获取当前日期日历面板上的开始和结束日期

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<script src="https://cdn.bootcdn.net/ajax/libs/moment.js/2.29.1/moment.js"></script>
		<script src="https://cdn.bootcdn.net/ajax/libs/moment.js/2.29.1/locale/zh-cn.js"></script>
		<script>
			const getPanelDateRange = (date = new Date(), week = 6, isSunDayFirst = false ) => {
					let start = moment(date).startOf('month').startOf('week');
					if(isSunDayFirst){  //如果周日开始的布局  再往前一天
						start = start.subtract(1,'d')
					}
					let end = moment(date).endOf('month').endOf('week');
					let weekDiff = end.diff(start,'week')
					if(weekDiff + 1 < week){ //默认显示6周日期,如果不够6周 就再加适当的周凑够6周
						end = end.add(week - weekDiff - 1, 'week')
					}
					if(isSunDayFirst){
						//如果周日开始的布局  再往前一天
						end = end.subtract(1,'d')
					}
					return [start.format('YYYY-MM-DD'), end.format('YYYY-MM-DD')]
			} 
			console.log(getPanelDateRange(moment()))
			
		</script>
	</body>
</html>
posted @ 2022-05-20 15:09  橙云生  阅读(164)  评论(0编辑  收藏  举报