js格式化时间戳,可以匹配多种格式
const now = new Date().getTime()
const time = this.timeToHourFn(now, 'yyyy-MM-dd HH:mm:ss')
console.log(time)
timeToHourFn(time, formatter = 'yyyy-MM-dd HH:mm:ss'){
if(!time) return ''
const date = new Date(time)
const yyyy = date.getFullYear()
const MM = (date.getMonth() + 1) < 10 ? '0' + (date.getMonth() + 1) : (date.getMonth() + 1)
const M = date.getMonth() + 1
const dd = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
const d = date.getDate()
const HH = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
const H = date.getHours()
const mm = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
const m = date.getMinutes()
const ss = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
const s = date.getSeconds()
let timeObject = {
yyyy: `${yyyy}`,
MM: `${MM}`,
dd: `${dd}`,
HH: `${HH}`,
mm: `${mm}`,
ss: `${ss}`,
M: `${M}`,
d: `${d}`,
H: `${H}`,
m: `${m}`,
s: `${s}`,
}
const reg = new RegExp(Object.keys(timeObject).join('|'),'g')
const res = formatter.replace(reg, (k) => {
return timeObject[k]
})
return res
}
更新:代码优化,对入参可进行指定格式
下方的代码使用方式:
获取中国标准时间:
console.log(timejs().t)
获取当前时间:
console.log(timejs().format())
按指定格式获取当前时间:
console.log(timejs().format('yyyy.MM.dd HH.mm.ss 星期DD'))
通过指定时间修改时间格式:
console.log(timejs('20220612', 'yyyyMMdd').format('yyyy-MM-dd'))
前后N天的时间:
向前加一天:
console.log(timejs().addDay(1).format('yyyy-MM-dd'))
向前加一天并补23:59:59
console.log(timejs().addDay(1).format('yyyy-MM-dd 23:59:59'))
向后减一天:
console.log(timejs().addDay(-1).format('yyyy-MM-dd'))
向后减一天并补00:00:00
console.log(timejs().addDay(-1).format('yyyy-MM-dd 00:00:00'))
当前周的 datarange[]
当前周一至周日:
console.log(timejs().getWeek('start').format('yyyy-MM-dd 00:00:00 星期DD'))
console.log(timejs().getWeek('end').format('yyyy-MM-dd 23:59:59 星期DD'))
当前月的 datarange[]
当前月的第一天至当前月的最后一天:
console.log(timejs().getMonth('start').format('yyyy-MM-dd 00:00:00 星期DD'))
console.log(timejs().getMonth('end').format('yyyy-MM-dd 23:59:59 星期DD'))
class Time{
constructor(m, f) {
this.getTime(m, f)
}
getTime(m, f){
if(!m) m = new Date()
m = m.toString()
if(f) {
let date = new Date()
let timeObject = {
yyyy: date.getFullYear(),
MM: (date.getMonth() + 1) < 10 ? '0' + (date.getMonth() + 1) : (date.getMonth() + 1),
dd: date.getDate() < 10 ? '0' + date.getDate() : date.getDate(),
HH: date.getHours() < 10 ? '0' + date.getHours() : date.getHours(),
mm: date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes(),
ss: date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds(),
}
if(f.indexOf('yyyy') !== -1) {
let i = f.indexOf('yyyy')
timeObject = {
yyyy: m.slice(i, i + 4),
MM: '01',
dd: '01',
HH: '00',
mm: '00',
ss: '00',
}
}
if(f.indexOf('MM') !== -1) {
let i = f.indexOf('MM')
timeObject = {
...timeObject,
MM: m.slice(i, i + 2),
dd: '01',
HH: '00',
mm: '00',
ss: '00',
}
}
if(f.indexOf('dd') !== -1) {
let i = f.indexOf('dd')
timeObject = {
...timeObject,
dd: m.slice(i, i + 2),
HH: '00',
mm: '00',
ss: '00',
}
}
if(f.indexOf('HH') !== -1) {
let i = f.indexOf('HH')
timeObject = {
...timeObject,
HH: m.slice(i, i + 2),
mm: '00',
ss: '00',
}
}
if(f.indexOf('mm') !== -1) {
let i = f.indexOf('mm')
timeObject = {
...timeObject,
mm: m.slice(i, i + 2),
ss: '00',
}
}
if(f.indexOf('ss') !== -1) {
let i = f.indexOf('ss')
timeObject = {
...timeObject,
ss: m.slice(i, i + 2),
}
}
let time = timeObject.yyyy + '-' + timeObject.MM + '-' + timeObject.dd + ' ' + timeObject.HH + ':' + timeObject.mm + ':' + timeObject.ss
console.log(time);
this.t = new Date(time)
} else {
this.t = new Date(m)
}
}
addDay(d = 0){
d = parseFloat(d)
this.t = this.t.getTime() + d * 24 * 60 * 60 * 1000
return this
}
format(f = 'yyyy-MM-dd HH:mm:ss'){
const week = ['日','一','二','三','四','五','六']
const date = new Date(this.t)
const yyyy = date.getFullYear()
const MM = (date.getMonth() + 1) < 10 ? '0' + (date.getMonth() + 1) : (date.getMonth() + 1)
const M = date.getMonth() + 1
const dd = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
const d = date.getDate()
const HH = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
const H = date.getHours()
const mm = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
const m = date.getMinutes()
const ss = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
const s = date.getSeconds()
const DD = week[date.getDay()]
let timeObject = {
yyyy: `${yyyy}`,
MM: `${MM}`,
dd: `${dd}`,
HH: `${HH}`,
mm: `${mm}`,
ss: `${ss}`,
M: `${M}`,
d: `${d}`,
H: `${H}`,
m: `${m}`,
s: `${s}`,
DD: `${DD}`,
}
const reg = new RegExp(Object.keys(timeObject).join('|'),'g')
const res = f.replace(reg, (k) => {
return timeObject[k]
})
return res
}
getWeek(type){
const date = new Date(this.t)
let w = date.getDay()
if(w === 0) w = 7
if(type === 'end') {
let d = 7 - w
this.addDay(d)
} else if(type === 'start') {
let d = w - 1
this.addDay(-d)
}
return this
}
getMonth(type){
const date = new Date(this.t)
const year = date.getFullYear()
const w = date.getMonth() + 1
let n = date.getDate()
let dif
if([1,3,5,7,8,10,12].includes(w)) {
dif = 31
} else if([4,6,9,11].includes(w)) {
dif = 30
} else {
dif = 28
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
dif = 29
}
}
if(type === 'end') {
let d = dif - n
this.addDay(d)
} else if(type === 'start') {
let d = n - 1
this.addDay(-d)
}
return this
}
}
export function timejs(m = new Date(), f){
return new Time(m, f)
}