const dater = {
/**
* @function 格式化时间
* @param {Number/Date} date // 日期值
* @param {String} pattern // 格式化字符串
* @param {Boolean} isAutoDate 是否使用当前时间
* @return {String}
*/
format (date, pattern, isAutoDate) {
// 参数处理
pattern = pattern || 'yyyy-MM-dd HH:mm:ss'
// 日期格式字符串
if (/\d+-\d+/.test(date)) {
let d = new Date(date)
if (d.toString() !== 'Invalid Date' && !/null/i.test(d.toString())) {
date = d
} else {
d = new Date(String(date).replace(/-/g, '/'))
if (d.toString() !== 'Invalid Date' && !/null/i.test(d.toString())) {
date = d
}
}
}
if (!(date instanceof Date) && /^\d+$/.test(date)) {
date = parseInt(date, 10)
if (isNaN(date)) {
if (isAutoDate !== true) {
return ''
}
date = new Date()
}
else {
date = new Date(date)
}
}
// 验证date是否正确
if (!date || !date.getFullYear) {
if (isAutoDate !== true) {
return ''
}
date = new Date()
}
let year = date.getFullYear()
let month = date.getMonth() + 1
let date2 = date.getDate()
let day = date.getDay()
let hours = date.getHours()
let minutes = date.getMinutes()
let seconds = date.getSeconds()
replacer(/yyyy/g, pad(year, 4))
replacer(/yy/g, pad(year.toString().slice(2), 2))
replacer(/MM/g, pad(month, 2))
replacer(/M/g, month)
replacer(/dd/g, pad(date2, 2))
replacer(/d/g, date2)
replacer(/HH/g, pad(hours, 2))
replacer(/H/g, hours)
replacer(/hh/g, pad(hours % 12, 2))
replacer(/h/g, hours % 12)
replacer(/mm/g, pad(minutes, 2))
replacer(/m/g, minutes)
replacer(/ss/g, pad(seconds, 2))
replacer(/s/g, seconds)
replacer(/W/g, ['星期天', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'][day])
replacer(/w/g, ['Sunday', 'Monday', 'Tuesday', 'Wedesday', 'Thursday', 'Friday', 'Saturday'][day])
return pattern
// 格式替换
function replacer (patternPart, result) {
pattern = pattern.replace(patternPart, result)
}
// 对目标字符串的前面补0,使其达到要求的长度
function pad (source, length) {
return (source < 0 ? '-' : '') + strer.padLeft(source,length, '0')
}
}
}
// 字符串处理
const strer = {
// 删减字符
pad (source, len, char, from) {
char = char || '0'
source = source.toString()
var pre = source.length < len ? (new Array(len - source.length + 1)).join(char) : ''
return from == 'left' ? (pre + source) : (source + pre)
},
// 删除左边字符
padLeft (source, len, char) {
return this.pad(source, len, char, 'left')
},
// 删除右边字符
padRight (source, len, char) {
return this.pad(source, len, char, 'right')
}
}
日期格式说明:
y // 年
M // 月
d // 日
H // 小时(24小时制)
h // 小时(12小时制)
m // 分钟
s // 秒
W // '星期天', '星期一', '星期二', '星期三', '星��四', '星期五', '星期六'
w // 'Sunday', 'Monday', 'Tuesday', 'Wedesday', 'Thursday', 'Friday', 'Saturday'
使用示例:
// 传入日期对象
dater.format(new Date()) // '2023-11-20 14:12:12'
// 传入时间戳
dater.format(new Date().getTime()) // '2023-11-20 14:12:51'
// 给定格式-年月日时分秒
dater.format(new Date().getTime(), 'yyyy-MM-dd HH:mm:ss') // '2023-11-20 14:13:35'
// 给定格式-年月日
dater.format(new Date().getTime(), 'yyyy-MM-dd') // '2023-11-20'
// 给定格式-时分秒
dater.format(new Date().getTime(), 'HH:mm:ss') // '14:15:22'
在线演练:
演练页面:http://www.daelui.com/#/tigerlair/saas/practice/lp6ipgvh6r26
预览页面:http://www.daelui.com/#/tigerlair/saas/preview/lp6ipgvh6r26