moment.js 算是我最常用的工具之一了,但是100+kb的体积属实过大,最近有个项目仅使用format功能,参考着写了这个精简版的,后面有什么需要的功能,用到再加了
//! 精简版moment.js 仅实现format
;
(function(global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
global.moment = factory()
}(this, (function() {
class moment {
constructor(arg = new Date().getTime()) {
this.date = new Date(arg)
}
format(formatStr = "YYYY-MM-DD HH:mm:ss") {
const date = this.date
const year = date.getFullYear()
const month = date.getMonth() + 1
const day = date.getDate()
const week = date.getDay()
const hour = date.getHours()
const minute = date.getMinutes()
const second = date.getSeconds()
return formatStr.replace(/Y{2,4}|M{1,2}|D{1,2}|d{1,4}|H{1,2}|m{1,2}|s{1,2}/g, (match) => {
switch (match) {
case 'YY':
return String(year).slice(-2)
case 'YYY':
case 'YYYY':
return String(year)
case 'M':
return String(month)
case 'MM':
return String(month).padStart(2, '0')
case 'D':
return String(day)
case 'DD':
return String(day).padStart(2, '0')
case 'd':
return String(week)
case 'dd':
return weeks[week]
case 'ddd':
return '周' + weeks[week]
case 'dddd':
return '星期' + weeks[week]
case 'H':
return String(hour)
case 'HH':
return String(hour).padStart(2, '0')
case 'm':
return String(minute)
case 'mm':
return String(minute).padStart(2, '0')
case 's':
return String(second)
case 'ss':
return String(second).padStart(2, '0')
default:
return match
}
})
}
}
return (t) => {
if (t) {
t = t.replace(/-/g, '/')
}
return new moment(t)
}
})));