此類封裝了關於date的常用功能
export default class MyDate {
/**
* 構造器
* @param {string|number|undefined} value 初始化時間
*/
constructor (value) {
this.sourceDate = value ? new Date(value) : new Date()
}
get year () {
return this.sourceDate.getFullYear()
}
get month () {
return this.sourceDate.getMonth()
}
get date () {
return this.sourceDate.getDate()
}
get hour () {
return this.sourceDate.getHours()
}
get minute () {
return this.sourceDate.getMinutes()
}
get second () {
return this.sourceDate.getSeconds()
}
get timestamp () {
return this.sourceDate.getTime()
}
/*
首字母大寫
*/
firstToUpper (string) {
return `${string.slice(0, 1).toUpperCase()}${string.slice(1).toLowerCase()}`
}
/**
* 秒格式化
* @param {number} value 格式化資料
* @param {string} level 格式化等級
* @param {number} fixed 小數位
*/
standard (value, level, fixed = 0) {
let map = {
'minute': 60,
'hour': 3600,
'date': 24 * 3600,
'week': 7 * 24 * 3600,
'month': 30 * 24 * 3600,
'year': 365 * 24 * 3600
}
if (!map[level] || isNaN(value)) {
console.error('[MyDate/formatSecound]Error: 輸入錯誤或輸出格式錯誤!')
return
}
return Number(value / map[level]).toFixed(fixed)
}
/**
* 數字補零
* @example frontAddZero(1, 2) // => '01'
* @param {string|number} value 值
* @param {number} length 轉換結果長度
*/
frontAddZero (value, length = 2) {
if (String(value).length > length) {
console.warn('[MyDate/frontAddZero]Warn: 源資料長度大於結果長度!')
}
return ('0'.repeat(length) + value).substr(-1 * length)
}
/**
* 日期操作[加/減]
* @example new Dater().add(10, 'date')今天加10天
* @param {number} value 新增數字
* @param {string} level 等級
*/
add (value, level) {
let copyTime = this.timestamp
let func = `set${this.firstToUpper(level)}`
let newDate = new MyDate(this.sourceDate[func](this[level] + value))
if (newDate > new Date(new Date().getTime() - 1)) {
newDate = new MyDate()
}
this.sourceDate.setTime(copyTime)
return newDate
}
/**
* 日期差
* @param {string|number|date} date 對比日期
*/
diff (date, level) {
let thisDate = this.timestamp
let compare = date instanceof MyDate
? date.timestamp
: new Date(date).getTime()
return this.standard((thisDate - compare) / 1000, level)
}
// Vue注入
install (Vue) {
Vue.prototype.$add = this.add.bind(this)
Vue.prototype.$diff = this.diff.bind(this)
Vue.prototype.$format = this.format.bind(this)
}
// 獲取在一年中第幾周
mothInYear (value) {
if (arguments.length === 0) {
let days = (new Date(this.year + '-12-31').getTime() - new Date(this.year + '-01-01').getTime()) / (1000 * 3600 * 24)
return [Math.ceil(days / 7)]
}
let months = Math.ceil((new Date(value).getTime() - new Date(this.year + '-01-01').getTime()) / (1000 * 3600 * 24) / 7),
days = (months - 1) > 0 ? (months - 1) : 0
return [
months,
new MyDate(new Date(this.year + '-01-01').getTime() + days*7*3600*24*1000),
new MyDate(new Date(this.year + '-01-07').getTime() + days*7*3600*24*1000),
]
}
/**
* 日期格式化
* @example {string} 'YYYY-MM-DD HH:mm:SS'
* @param {string} template 日期輸出格式
* [年] YYYY-2018 YY-18
* [月] MM-02 M-2
* [日] DD-08 D-8
* [時] HH-02 H-2
* [分] mm-02 m-2
* [秒] SS-02 S-2
* [時間戳] X-1410715640.579 x-1410715640579
*/
format (template = 'YYYY-MM-DD') {
let map = {
'YYYY': this.year,
'YY': String(this.year).slice(-2),
'MM': this.frontAddZero(this.month + 1),
'M': this.month + 1,
'DD': this.frontAddZero(this.date),
'dd': this.date,
'HH':this. frontAddZero(this.hour),
'H': this.hour,
'mm': this.frontAddZero(this.minute),
'm': this.minute,
'SS': this.frontAddZero(this.second),
'S': this.second,
// 'X': parseInt(this.timestamp / 1000),
// 'x': this.timestamp
}
Object.keys(map).forEach(key => {
template = template.replace(key, map[key])
})
return isNaN(template) ? template : Number(template)
}
}
export default MyDate
複製程式碼
應用
template(lang='pug')
div.content
div {{$add(1, 'date').format()}}
複製程式碼