获取指定日期范围

 function timeLimit(e) {
	let now = new Date(); //当前日期
	let nowDayOfWeek = now.getDay(); //今天本周的第几天
	let nowDay = now.getDate(); //当前日
	let nowMonth = now.getMonth(); //当前月
	let nowYear = now.getFullYear(); //当前年份
	let arr = [];
	if (e === '0') {
		arr[0] = `${nowYear}-${nowMonth+1}-${nowDay}`;
		arr[1] = `${nowYear}-${nowMonth+1}-${nowDay}`;
	} else if (e === '1') { //本月
		arr[0] = getMonthStartDate(nowYear, nowMonth);
		arr[1] = getMonthEndDate(nowYear, nowMonth);
	} else if (e === '2') { //本周
		arr[0] = getWeekStartDate(nowYear, nowMonth, nowDay, nowDayOfWeek);
		arr[1] = getWeekEndDate(nowYear, nowMonth, nowDay, nowDayOfWeek);
	} else if (e === '3') { //上月
		if (nowMonth === 0) {
			arr[0] = getMonthStartDate(nowYear - 1, 11);
			arr[1] = getMonthEndDate(nowYear - 1, 11);
		} else {
			arr[0] = getMonthStartDate(nowYear, nowMonth - 1);
			arr[1] = getMonthEndDate(nowYear, nowMonth - 1);
		}
	} else if (e === '4') { //三个月
		let interval = 2 - (nowMonth + 1);
		if (interval < 0) {
			arr[0] = getMonthStartDate(nowYear, nowMonth - 2);
			arr[1] = getMonthEndDate(nowYear, nowMonth);
		} else {
			arr[0] = getMonthStartDate(nowYear - 1, 11 - interval);
			arr[1] = getMonthEndDate(nowYear, nowMonth);
		}
	} else if (e === '5') { //半年
		let interval = 5 - (nowMonth + 1);
		if (interval < 0) {
			arr[0] = getMonthStartDate(nowYear, nowMonth - 5);
			arr[1] = getMonthEndDate(nowYear, nowMonth);
		} else {
			arr[0] = getMonthStartDate(nowYear - 1, 11 - interval);
			arr[1] = getMonthEndDate(nowYear, nowMonth);
		}
	}
	return arr;
}
//格式化日期  
function formatDate(date) {
	let theYear = date.getFullYear();
	let theMonth = date.getMonth() + 1;
	let theDay = date.getDate();
	theMonth = theMonth < 10 ? "0" + theMonth : theMonth;
	theDay = theDay < 10 ? "0" + theDay : theDay;
	return (theYear + "-" + theMonth + "-" + theDay);

}
//获得某月的天数  
function getMonthDays(year, month) {
	let monthStartDate = new Date(year, month, 1);
	let monthEndDate = new Date(year, month + 1, 1);
	return (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24);
}
//获得本月的开始日期 
function getMonthStartDate(year, month) {
	let monthStartDate = new Date(year, month, 1);
	return formatDate(monthStartDate);

}
//获得本月的结束日期  
function getMonthEndDate(year, month) {
	let monthEndDate = new Date(year, month, getMonthDays(year, month));
	return formatDate(monthEndDate);
}
//获得本周的开始日期  
function getWeekStartDate(year, month, day, week) {
	let weekStartDate = new Date(year, month, day - week);
	return formatDate(weekStartDate);

}
//获得本周的结束日期  
function getWeekEndDate(year, month, day, week) {
	let weekEndDate = new Date(year, month, day + (6 - week));
	return formatDate(weekEndDate);
}

new Date转化为yyyy-MM-dd HH:mm:ss

let date = new Date()
date.toLocaleString()  // 2020/7/9 上午9:30:27
//转化为dd-MM-yyyy HH:mm:ss
let time = date.toLocaleString("en-US", { hour12: false }).replace(/\b\d\b/g, '0$&').replace(new RegExp('/','gm'),'-')    //07-09-2020, 09:30:27
//转化为yyyy-MM-dd HH:mm:ss
let time = date.toLocaleString("zh-cn", { hour12: false }).replace(/\b\d\b/g, '0$&').replace(new RegExp('/','gm'),'-') // 2020-07-09 09:30:27

let time = date.toLocaleString("zh-cn", { hour12: true }).replace(/\b\d\b/g, '0$&').replace(new RegExp('/','gm'),'-') // 2020-07-09 上午09:30:27

将yyyy-MM-dd转化为new Date()

new Date('2020-07-09 9:44:00');

获取当前的时间yyyy-MM-dd HH:mm:ss

let date = new Date();
let year = date.getFullYear();
let month = date.getMonth() + 1;
let day=date.getDate();
let hours=date.getHours();
let minu=date.getMinutes();
let second=date.getSeconds();
//判断是否满10
let arr=[month,day,hours,minu,second];
arr.forEach(item=>{
item< 10?"0"+item:item;
})
let dataFormat = year+'-'+arr[0]+'-'+arr[1]+' '+arr[2]+':'+arr[3]+':'+arr[4]

将时间戳转化为yyyy-MM-dd HH:mm:ss

let middleDate=new Date(1594352983166);
let dateFormat = middleDate.toLocaleString('zh-CN',{hour12:false}).replace(/\b\d\b/g, '0$&').replace(new RegExp('/','gm'),'-')

比较yyyy-MM-dd时间大小

export default const compareTwo=(dateOne,dateTwo)=>{
    return Number(dateOne.replace(/\-/g,""))<Number(dateTwo.replace(/\-/g,""))
}
compareTwo('2020-07-01','2020-06-04')  //false

计算两个日期格式为(yyyy-MM-dd)相差几个月

export default const disparityFewMonth = (dateOne, dateTwo) => {
    let datesOne = dateOne.split('-').map(item => Number(item));
    let datesTwo = dateTwo.split('-').map(item => Number(item));
    const diff = [0, 0, 0].map((value, index) => {
        return datesOne[index] - datesTwo[index]
    });
    return (diff[0] * 12 + diff[1]) + '月' + diff[2] + '天'
}
disparityFewMonth('2020-09-03','2020-03-03')//"6月0天"
disparityFewMonth('2020-04-03','2020-05-09')//"-1月-6天"