From b64aa13a65fad3bd2835a713d44c467be6df03e9 Mon Sep 17 00:00:00 2001 From: xiaozhiyong Date: Thu, 11 Jun 2026 19:02:18 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/date.js | 38 ++++++++++++++++++++++++++++++++++++++ src/utils/format.js | 10 +++++++++- 2 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 src/utils/date.js diff --git a/src/utils/date.js b/src/utils/date.js new file mode 100644 index 0000000..0c0159c --- /dev/null +++ b/src/utils/date.js @@ -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() +} diff --git a/src/utils/format.js b/src/utils/format.js index 78b84e0..1792379 100644 --- a/src/utils/format.js +++ b/src/utils/format.js @@ -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) => {