vue 根据指定日期 获取日期所在月份的第一天和最后一天

1.需要在以下方法中传入年月(2022-05)

2.代码如下,可供大家参考

getDate(val) {
			let date = new Date(val);
			let new_year = date.getFullYear(); // 取当前的年份
			let month = date.getMonth();
			let new_month = month + 1; // 取当前的月份
			let mon = "";
			let day = "";
			if (month > 12) {
				new_month -= 12; // 月份减
				new_year++; // 年份增
			}
			let firstDay = new Date(new_year, new_month, 1); // 取当年当月中的第一天
			let lastDay = new Date(
				firstDay.getTime() - 1000 * 60 * 60 * 24
			).getDate(); // 获取当月最后一天日期
			if (firstDay.getMonth() < 10) {
				mon = "0" + firstDay.getMonth();
			} else {
				mon = firstDay.getMonth();
			}
			if (lastDay < 10) {
				day = "0" + lastDay;
			} else {
				day = lastDay;
			}
			let startDate =
				firstDay.getFullYear() +
				"-" +
				mon +
				"-" +
				"0" +
				firstDay.getDate();
			let endDate = firstDay.getFullYear() + "-" + mon + "-" + day;
			this.dateRange = startDate + "~" + endDate;
		},

你可能感兴趣的:(vue.js,前端,javascript)