每次在进行日期操作的时候,日期格式化等操作都很繁琐,为此我自定义了一个日期类,内置了日期格式化等等操作。
一、新建一个js文件,写入一下代码
class ZyDate {
constructor() {
this.date = new Date()
this.year = this.date.getFullYear() // 年
this.month = this.date.getMonth() + 1 // 月
this.doubleMonth = this.doubleNum(this.month) // 月(小于10在前面加了一个0)
this.day = this.date.getDate() //日
this.doubleDay = this.doubleNum(this.day) // 日(小于10在前面加了一个0)
this.hours = this.date.getHours() // 时
this.doubleHours = this.doubleNum(this.hours) // 时(小于10在前面加了一个0)
this.minutes = this.date.getMinutes() // 分
this.doubleMinutes = this.doubleNum(this.minutes) // 分(小于10在前面加了一个0)
this.second = this.date.getSeconds() //秒
this.doubleSecond = this.doubleNum(this.second) // 秒(小于10在前面加了一个0)
this.weekNum = this.date.getDay() //星期
this.week = this.parseWeek(this.weekNum) //格式化后的星期
this.getTime = this.date.getTime() // 时间戳
}
/**
* 日期格式化
* @param time 需要格式化的日期
* @param pattern 格式化的类型 默认 {y}-{m}-{d} {h}:{i}:{s}
* @returns {string|null}
*/
parseTime(time, pattern) {
if (!time) {
return null
}
const format = pattern || '{y}-{m}-{d} {h}:{i}:{s}'
let date
if (typeof time === 'object') {
date = time
} else {
if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {
time = parseInt(time)
} else if (typeof time === 'string') {
time = time.replace(new RegExp(/-/gm), '/').replace('T', ' ').replace(new RegExp(/\.[\d]{3}/gm), '');
}
if ((typeof time === 'number') && (time.toString().length === 10)) {
time = time * 1000
}
date = new Date(time)
}
const formatObj = {
y: date.getFullYear(),
m: date.getMonth() + 1,
d: date.getDate(),
h: date.getHours(),
i: date.getMinutes(),
s: date.getSeconds(),
a: date.getDay()
}
const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
let value = formatObj[key]
// Note: getDay() returns 0 on Sunday
if (key === 'a') { return this.parseWeek(value) }
if (result.length > 0) {
value = this.doubleNum(value)
}
return value || 0
})
return time_str
}
// 获取当前格式化时间
parseTimeNow(pattern) {
return this.parseTime(this.getTime, pattern)
}
doubleNum(num) {
if(Number(num) < 10 && Number(num) > 0) {
return `0${num}`
}
return num
}
// 格式化星期
parseWeek(week, weekList) {
const weekData = weekList || ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六']
return weekData[week]
}
}
export default ZyDate
二、使用
使用之前别忘了引入ZyDate类
const zyData = new ZyDate()
// 格式化指定日期zyData.parseTime(时间,格式化样式(默认{y}-{m}-{d} {h}:{i}:{s}))
// y:年 m:月 d:日 h:时 i:分 s:秒 a:星期
const time = zyData.parseTime('2023/7/20 10:14:01', {y}-{m}-{d} {h}:{i}:{s} {a})
console.log(time) // 2023-07-20 10:14:01 星期四
// 格式化当前zyData.parseTimeNow(格式化样式(默认{y}-{m}-{d} {h}:{i}:{s}))
const time = zyData.parseTimeNow({y}-{m}-{d} {h}:{i}:{s} {a})
还能获取更多的值
zyData.year // 年
zyData.month // 月
zyData.doubleMonth // 月(小于10在前面加了一个0)
zyData.day //日
zyData.doubleDay // 日(小于10在前面加了一个0)
zyData.hours // 时
zyData.doubleHours // 时(小于10在前面加了一个0)
zyData.minutes // 分
zyData.doubleMinutes // 分(小于10在前面加了一个0)
zyData.second //秒
zyData.doubleSecond // 秒(小于10在前面加了一个0)
zyData.weekNum //星期
zyData.week //格式化后的星期
zyData.getTime // 时间戳