js日期时间工具类收集

获取指定日期范围

 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天"

列举本周日期(周一到周日)

var today = new Date();// 获取今天的日期
//计算一周的起始日期,即本周的第一天(通过减去星期几)
var startOfWeek = new Date(today.getFullYear(), today.getMonth(), today.getDate() - today.getDay());
// 获取本周的所有日期
//使用一个循环来遍历一周的每一天,并使用 Date 对象表示每一天的日期。循环中,通过将起始日期的毫秒数加上一天的毫秒数来计算每一天的日期,并将其添加到 dates 数组中。最后,输出包含上一周所有日期的数组。
var dates = [];
for (var i = 1; i <= 7; i++) {
    dates.push(new Date(startOfWeek.getTime() + i * 24 * 60 * 60 * 1000));
}
// 输出本周的所有日期
console.log(dates);

列举上周日期(周一到周日)

var lastWeek = new Date(today.getTime() - 7 * 24 * 60 * 60 * 1000);
var today = new Date(lastWeek);
var startOfWeek = new Date(today.getFullYear(), today.getMonth(), today.getDate() - today.getDay());
// 获取下一周的所有日期
var dates = [];
for (var i = 1; i <= 7; i++) {
    dates.push(new Date(startOfWeek.getTime() + i * 24 * 60 * 60 * 1000));
}
// 输出下一周的所有日期
console.log(dates);

列举下周日期(周一到周日)

var nextWeek = new Date(today.getTime() + 7 * 24 * 60 * 60 * 1000);
var today = new Date(lastWeek);
var startOfWeek = new Date(today.getFullYear(), today.getMonth(), today.getDate() - today.getDay());
// 获取下一周的所有日期
var dates = [];
for (var i = 1; i <= 7; i++) {
    dates.push(new Date(startOfWeek.getTime() + i * 24 * 60 * 60 * 1000));
}
// 输出下一周的所有日期
console.log(dates);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值