简单的日期转换(当前时间,月,季度,年)
最近在项目中用到了很多关于时间处理的函数,此处作为整理记录。
获取当前时间的数组
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()]
//此处返回为时间戳,根据需要可进行调整
}