custom-time
Version:
高度自定义化日期插件
29 lines (28 loc) • 760 B
JavaScript
import masks from './masksArr'
class DateFormat {
constructor(date) {
this._date = new Date(date)
}
formatNumber(num) {
return num < 10 ? `0${num}` : num
}
initDate() {
this.fullYear = this._date.getFullYear()
this.month = this._date.getMonth() + 1
this.date = this._date.getDate()
this.hours = this._date.getHours()
this.minutes = this._date.getMinutes()
this.seconds = this._date.getSeconds()
}
}
export default function (date, format) {
const df = new DateFormat(date)
df.initDate()
masks.forEach(function(value, key) {
if(format.includes(key)) {
let formatTime = key.length > 1 ? df.formatNumber(df[value]) : df[value]
format = format.replace(key, formatTime)
}
})
return format;
}