58 lines
1.4 KiB
JavaScript
58 lines
1.4 KiB
JavaScript
/*
|
|
* @Author: 杨汝岱
|
|
* @Date: 2022-06-14 17:16:10
|
|
* @LastEditors: ag 2663588772@qq.com
|
|
* @LastEditTime: 2022-07-07 11:35:23
|
|
* @Description: 文件描述
|
|
*/
|
|
class CellsysDate {
|
|
|
|
constructor(timestamp) {
|
|
if (timestamp) {
|
|
this.timestamp = timestamp
|
|
this.date = new Date(parseInt(this.timestamp))
|
|
} else {
|
|
this.date = new Date()
|
|
this.timestamp = this.date.getTime()
|
|
}
|
|
}
|
|
|
|
getDate(formats) {
|
|
return this._dateFormat(formats)
|
|
}
|
|
|
|
getTime(formats = 'H:m:s') {
|
|
return this._dateFormat(formats)
|
|
}
|
|
|
|
_dateFormat(formats) {
|
|
formats = formats || 'y-M-d H:m:s'
|
|
const date = this.date
|
|
let zero = function (value) {
|
|
if (value < 10) {
|
|
return '0' + value
|
|
}
|
|
return value
|
|
}
|
|
let year = date.getFullYear()
|
|
let month = zero(date.getMonth() + 1)
|
|
let day = zero(date.getDate())
|
|
|
|
let hour = zero(date.getHours())
|
|
let minite = zero(date.getMinutes())
|
|
let second = zero(date.getSeconds())
|
|
|
|
return formats.replace(/y|M|d|H|m|s/ig, function (matches) {
|
|
return ({
|
|
y: year,
|
|
M: month,
|
|
d: day,
|
|
H: hour,
|
|
m: minite,
|
|
s: second
|
|
})[matches]
|
|
})
|
|
}
|
|
}
|
|
|
|
export default CellsysDate |