简单的日期转换(当前时间,月,季度,年)

简单的日期转换(当前时间,月,季度,年)

最近在项目中用到了很多关于时间处理的函数,此处作为整理记录。

获取当前时间的数组

function getnow() {
	let now = new Date();
	let year = now.getFullYear()
	let month = now.getMonth() + 1 < 10 ? '0' + (now.getMonth() + 1) : now.getMonth() + 1
	let date = now.getDate() < 10 ? '0' + now.getDate() : now.getDate()
	let hours = now.getHours() < 10 ? '0' + now.getHours() : now.getHours()
	let minutes = now.getMinutes() < 10 ? '0' + now.getMinutes() : now.getMinutes()
	let seconds = now.getSeconds() < 10 ? '0' + now.getSeconds() : now.getSeconds()
	let defaultVal = [year.toString(), month.toString(), date.toString(), hours.toString(), minutes.toString(), seconds.toString()]
	return defaultVal 
	//此处返回了当前时间的年月日时分秒的数组,根据需要可进行调整
}

获取本月开始结束时间的数组

function getNowMonth() {
	let now = new Date();
	let year = now.getFullYear();
	let month = now.getMonth() + 1;
	let nextMonth = month == 12 ? 1 : month + 1;
	let startDate = month < 10 ? year + '-' + ('0' + month) + '-01T00:00:00' : year + '-' + month + '-01T00:00:00';
	let endDate = ''
	if (month == 12) {
		endDate = (year + 1) + '-01-01T00:00:00';
	} else {
		endDate = nextMonth < 10 ? year + '-' + ('0' + nextMonth) + '-01T00:00:00' : year + '-' + nextMonth + '-01T00:00:00';
	}
	return [new Date(startDate).getTime(), new Date(endDate).getTime()]
	//此处返回为本月1号0点0分0秒~下月1号0点0分0秒的数组,根据需要可进行调整
	//此处返回为时间戳,根据需要可进行调整
}

获取本月、本季度、本年的开始结束时间的数组

/**
* tyte:  0:本月;  1:本季度;   2:本年
* 此处结束时间为当前时间(例如今天12-20,查询本月返回12-1~12-20 。查询本季度返回10-1~12-20)
*/
function computTimeHorizon(type) {
	let startDate, endDate;
	let d = new Date();
	let year = d.getFullYear()
	let month = d.getMonth() + 1;
	let date = d.getDate();
	let minutes = d.getMinutes();
	let hours = d.getHours();
	let seconds = d.getSeconds();
	if (type == 0) {
		startDate = year + '-' + (month < 10 ? ('0' + month) : month) + '-' + '01' + 'T' + '00:00:00'
		endDate = year + '-' + (month < 10 ? ('0' + month) : month) + '-' + (date < 10 ? ('0' + date) : date) + 'T' + (hours <
			10 ? ('0' + hours) : hours) + ':' + (minutes < 10 ? (
			'0' + minutes) : minutes) + ':' + (seconds < 10 ? ('0' + seconds) : seconds)
	}
	if (type == 1) {
		if (month < 4) {
			startDate = year + '-01-01' + 'T' + '00:00:00'
			endDate = year + '-' + (month < 10 ? ('0' + month) : month) + '-' + (date < 10 ? ('0' + date) : date) + 'T' + (
				hours < 10 ? ('0' + hours) : hours) + ':' + (minutes < 10 ? (
				'0' + minutes) : minutes) + ':' + (seconds < 10 ? ('0' + seconds) : seconds)
		} else if (month > 3 && month < 7) {
			startDate = year + '-03-01' + 'T' + '00:00:00'
			endDate = year + '-' + (month < 10 ? ('0' + month) : month) + '-' + (date < 10 ? ('0' + date) : date) + 'T' + (
				hours < 10 ? ('0' + hours) : hours) + ':' + (minutes < 10 ? (
				'0' + minutes) : minutes) + ':' + (seconds < 10 ? ('0' + seconds) : seconds)
		} else if (month > 6 && month < 10) {
			startDate = year + '-07-01' + 'T' + '00:00:00'
			endDate = year + '-' + (month < 10 ? ('0' + month) : month) + '-' + (date < 10 ? ('0' + date) : date) + 'T' + (
				hours < 10 ? ('0' + hours) : hours) + ':' + (minutes < 10 ? (
				'0' + minutes) : minutes) + ':' + (seconds < 10 ? ('0' + seconds) : seconds)
		} else if (month > 9) {
			startDate = year + '-10-01' + 'T' + '00:00:00'
			endDate = year + '-' + (month < 10 ? ('0' + month) : month) + '-' + (date < 10 ? ('0' + date) : date) + 'T' + (
				hours < 10 ? ('0' + hours) : hours) + ':' + (minutes < 10 ? (
				'0' + minutes) : minutes) + ':' + (seconds < 10 ? ('0' + seconds) : seconds)
		}
	}
	if (type == 2) {
		startDate = year + '-01-01' + 'T' + '00:00:00'
		endDate = year + '-' + (month < 10 ? ('0' + month) : month) + '-' + (date < 10 ? ('0' + date) : date) + 'T' + (hours <
			10 ? ('0' + hours) : hours) + ':' + (minutes < 10 ? (
			'0' + minutes) : minutes) + ':' + (seconds < 10 ? ('0' + seconds) : seconds)
	}
	return [new Date(startDate).getTime(), new Date(endDate).getTime()]
	//此处返回为时间戳,根据需要可进行调整
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值