This commit is contained in:
xiaozhiyong
2026-06-11 19:02:18 +08:00
parent 0ae8ba168b
commit b64aa13a65
2 changed files with 47 additions and 1 deletions

38
src/utils/date.js Normal file
View File

@@ -0,0 +1,38 @@
// 对Date的扩展将 Date 转化为指定格式的String
// 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符,
// 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
// (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
// (new Date()).Format("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18
// eslint-disable-next-line no-extend-native
Date.prototype.Format = function (fmt) {
const o = {
'M+': this.getMonth() + 1, // 月份
'd+': this.getDate(), // 日
'h+': this.getHours(), // 小时
'm+': this.getMinutes(), // 分
's+': this.getSeconds(), // 秒
'q+': Math.floor((this.getMonth() + 3) / 3), // 季度
S: this.getMilliseconds() // 毫秒
}
const reg = /(y+)/
if (reg.test(fmt)) {
const t = reg.exec(fmt)[1]
fmt = fmt.replace(t, (this.getFullYear() + '').substring(4 - t.length))
}
for (let k in o) {
const regx = new RegExp('(' + k + ')')
if (regx.test(fmt)) {
const t = regx.exec(fmt)[1]
fmt = fmt.replace(t, t.length === 1 ? o[k] : ('00' + o[k]).substring(('' + o[k]).length))
}
}
return fmt
}
export function formatTimeToStr(times, pattern) {
let d = new Date(times).Format('yyyy-MM-dd hh:mm:ss')
if (pattern) {
d = new Date(times).Format(pattern)
}
return d.toLocaleString()
}

View File

@@ -1,3 +1,4 @@
import { formatTimeToStr } from '@/utils/date'
import { getDict } from '@/utils/dictionary'
import { ref } from 'vue'
@@ -8,7 +9,14 @@ export const formatBoolean = (bool) => {
return ''
}
}
export const formatDate = (time) => {
if (time !== null && time !== '') {
var date = new Date(time)
return formatTimeToStr(date, 'yyyy-MM-dd hh:mm:ss')
} else {
return ''
}
}
export const filterDict = (value, options) => {
// 递归查找函数
const findInOptions = (opts, targetValue) => {