js封装当前日期+年月日+格式转换格式(YYYY-mm-dd hh:mm:ss)

function padLeftZero(str) {
	return ('00' + str).substr(str.length)
}

function formatDate(date, fmt) {
	if (!date) {
		return ''
	}
	if (/(y+)/.test(fmt)) {
		fmt = fmt.replace(
			RegExp.$1,
			(date.getFullYear() + '').substr(4 - RegExp.$1.length)
		)
	}
	const o = {
		'M+': date.getMonth() + 1,
		'd+': date.getDate(),
		'h+': date.getHours(),
		'm+': date.getMinutes(),
		's+': date.getSeconds()
	}
	for (const k in o) {
		if (new RegExp(`(${k})`).test(fmt)) {
			const str = o[k] + ''
			fmt = fmt.replace(
				RegExp.$1,
				RegExp.$1.length === 1 ? str : padLeftZero(str)
			)
		}
	}
	return fmt
}
export function getDateRange(e) {
	let today = new Date();
	let todays = new Date(); // 今日
	let tomorrow = new Date();
	tomorrow.setDate(tomorrow.getDate() - 1); //昨日

	let startOfWeek = new Date(today.setDate(today.getDate() - today.getDay())); //本周的开始日期
	let endOfWeek = new Date(today.setDate(today.getDate() - today.getDay() + 6)); //本周结束时间

	let startOfLastWeek = new Date(startOfWeek);
	startOfLastWeek.setDate(startOfLastWeek.getDate() - 7); //上周开始时间
	let endOfLastWeek = new Date(endOfWeek);
	endOfLastWeek.setDate(endOfLastWeek.getDate() - 7); // 上周结束时间

	let Monthday = new Date();
	let year = Monthday.getFullYear();
	let month = Monthday.getMonth() + 1;
	let daysInMonth = new Date(year, month, 0).getDate();
	let thisStartDate = new Date(year, month - 1, 1) //本月开始时间
	let thisEndDate = new Date(year, month - 1, daysInMonth) //本月结束时间

	// let lastMonth = getThisMonth();
	let lastStartDate = new Date(new Date(thisStartDate).setMonth(new Date(thisStartDate).getMonth() -
		1)); //上月开始
	let lastEndDate = new Date(new Date(thisEndDate).setMonth(new Date(thisEndDate).getMonth(), 0)); //上月结束


	const yearStartDate = new Date(); // 获取今年第一天
	yearStartDate.setDate(1);
	yearStartDate.setMonth(0);

	const yearEndDate = new Date();
	yearEndDate.setFullYear(yearEndDate.getFullYear() + 1); // 最后一天
	yearEndDate.setMonth(0); // 明年的0月,也就是对应到1月,是存在的哦,不是不存在的0
	yearEndDate.setDate(0); // 明年的0日	


	const lastYearSta = new Date();
	lastYearSta.setFullYear(lastYearSta.getFullYear() - 1); // 去年第一天
	lastYearSta.setMonth(0); // 明年的0月,也就是对应到1月,是存在的哦,不是不存在的0
	lastYearSta.setDate(1); // 明年的0日	

	const lastYearEnd = new Date();
	lastYearEnd.setFullYear(lastYearEnd.getFullYear()); // 最后一天
	lastYearEnd.setMonth(0); // 明年的0月,也就是对应到1月,是存在的哦,不是不存在的0
	lastYearEnd.setDate(0); // 明年的0日


	let dateObj = {}
	const sta = 'yyyy-MM-dd 00:00:00'
	const end = 'yyyy-MM-dd 23:59:59'
	if (e == '昨日') {
		dateObj = {
			sta: formatDate(tomorrow, sta),
			end: formatDate(tomorrow, end)
		}
	} else if (e == '今日') {
		dateObj = {
			sta: formatDate(todays, sta),
			end: formatDate(todays, end)
		}
	} else if (e == '本周') {
		dateObj = {
			sta: formatDate(startOfWeek, sta),
			end: formatDate(endOfWeek, end)
		}

	} else if (e == '上周') {

		dateObj = {
			sta: formatDate(startOfLastWeek, sta),
			end: formatDate(endOfLastWeek, end)
		}

	} else if (e == '本月') {
		dateObj = {
			sta: formatDate(thisStartDate, sta),
			end: formatDate(thisEndDate, end)
		}

	} else if (e == '上月') {
		dateObj = {
			sta: formatDate(lastStartDate, sta),
			end: formatDate(lastEndDate, end)
		}

	} else if (e == '今年') {
		dateObj = {
			sta: formatDate(yearStartDate, sta),
			end: formatDate(yearEndDate, end)
		}

	} else if (e == '去年') {
		dateObj = {
			sta: formatDate(lastYearSta, sta),
			end: formatDate(lastYearEnd, end)
		}

	}
	return {
		dateObj
	};
}

export default getDateRange

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值