export function formatDate(date, fmt) {
|
try {
|
if (!(date instanceof Date)) {
|
date = new Date(date)
|
}
|
if (fmt == undefined || fmt == '') {
|
fmt = 'yyyy-MM-dd hh:mm:ss'
|
}
|
if (/(y+)/.test(fmt)) {
|
fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
|
}
|
const o = {
|
'M+': date.getMonth() + 1,
|
'd+': date.getDate(),
|
'h+': date.getHours(),
|
'm+': date.getMinutes(),
|
's+': date.getSeconds()
|
}
|
for (const k in o) {
|
if (new RegExp(`(${k})`).test(fmt)) {
|
const str = o[k] + ''
|
fmt = fmt.replace(RegExp.$1, RegExp.$1.length === 1 ? str : padLeftZero(str))
|
}
|
}
|
return fmt
|
} catch (e) {
|
console.log(e)
|
return '-'
|
}
|
}
|
|
function padLeftZero(str) {
|
return ('00' + str).substr(str.length)
|
}
|
|
/**
|
* @Author wh
|
* @Date 2022/2/25 - 14:35
|
* @Description //日期比较
|
**/
|
export function compare(date1, date2) {
|
const dates1 = new Date(date1)
|
const dates2 = new Date(date2)
|
if (dates1 > dates2) {
|
return true
|
} else {
|
return false
|
}
|
}
|
|
/**
|
* @Author wh
|
* @Date 2022/5/13 - 15:43
|
* @Description //时间格式转换
|
* 传值时自动调用了toJSON()函数,手动转成你想要的字符串格式
|
**/
|
export function FormatTime(t, date) {
|
var date = new Date(date)
|
var o = {
|
'M+': date.getMonth() + 1, // 月份
|
'd+': date.getDate(), // 日
|
'h+': date.getHours(), // 小时
|
'm+': date.getMinutes(), // 分
|
's+': date.getSeconds(), // 秒
|
'q+': Math.floor((date.getMonth() + 3) / 3), // 季度
|
'S': date.getMilliseconds() // 毫秒
|
}
|
if (/(y+)/.test(t)) {
|
t = t.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
|
}
|
for (var k in o) {
|
if (new RegExp('(' + k + ')').test(t)) {
|
t = t.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
|
}
|
}
|
return t
|
}
|