From 07824a089b53c580191e3b9979c317fbf17bbbef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?21=E4=B8=96=E7=BA=AA=E5=B0=8F=E5=85=AB=E8=B7=AF?= <2529156631@qq.com> Date: Sun, 20 Oct 2024 17:27:07 +0800 Subject: [PATCH] =?UTF-8?q?uniapp=E3=80=90=E4=BF=AE=E5=A4=8D=E3=80=91?= =?UTF-8?q?=EF=BC=9A=20=E4=BF=AE=E5=A4=8D=E4=B8=AA=E4=BA=BA=E4=B8=AD?= =?UTF-8?q?=E5=BF=83=E7=94=9F=E6=97=A5=E6=97=B6=E9=97=B4=E9=80=89=E6=8B=A9?= =?UTF-8?q?1970-4-1=E5=90=8E=E5=B1=95=E7=A4=BA=E9=94=99=E8=AF=AF=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CoreShop/core/utils/index.ts | 3 +- .../CoreShop/core/utils/time-format.ts | 51 +++++++++++++++++++ .../member/set/userInfo/userInfo.vue | 4 +- 3 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 CoreCms.Net.Uni-App/CoreShop/core/utils/time-format.ts diff --git a/CoreCms.Net.Uni-App/CoreShop/core/utils/index.ts b/CoreCms.Net.Uni-App/CoreShop/core/utils/index.ts index f3825656..8eb5f04f 100644 --- a/CoreCms.Net.Uni-App/CoreShop/core/utils/index.ts +++ b/CoreCms.Net.Uni-App/CoreShop/core/utils/index.ts @@ -22,4 +22,5 @@ export * from './handle-toast'; /** 处理广告位详情 */ export * from './handle-advertise-detail'; -export * from './uni-promise'; \ No newline at end of file +export * from './uni-promise'; +export * from './time-format'; \ No newline at end of file diff --git a/CoreCms.Net.Uni-App/CoreShop/core/utils/time-format.ts b/CoreCms.Net.Uni-App/CoreShop/core/utils/time-format.ts new file mode 100644 index 00000000..cf23fad3 --- /dev/null +++ b/CoreCms.Net.Uni-App/CoreShop/core/utils/time-format.ts @@ -0,0 +1,51 @@ +/** + * @description 格式化时间 + * @param { String | Number } dateTime 需要格式化的时间戳 + * @param { String } fmt 格式化规则 yyyy: mm: dd | yyyy: mm | yyyy年mm月dd日 | yyyy年mm月dd日 hh时MM分等, 可自定义组合 默认yyyy - mm - dd + * @returns { string } 返回格式化后的字符串 +*/ +export const timeFormat = (dateTime = null, formatStr = 'yyyy-mm-dd') => { + let date + // 若传入时间为假值,则取当前时间 + if (!dateTime) { + date = new Date() + } + // 若为unix秒时间戳,则转为毫秒时间戳(逻辑有点奇怪,但不敢改,以保证历史兼容) + // else if (/^\d{10}$/.test(dateTime?.toString().trim())) { + // date = new Date(dateTime * 1000) + // } + // 若用户传入字符串格式时间戳,new Date无法解析,需做兼容 + else if (typeof dateTime === 'string' && /^\d+$/.test(dateTime.trim())) { + date = new Date(Number(dateTime)) + } + // 处理平台性差异,在Safari/Webkit中,new Date仅支持/作为分割符的字符串时间 + // 处理 '2022-07-10 01:02:03',跳过 '2022-07-10T01:02:03' + else if (typeof dateTime === 'string' && dateTime.includes('-') && !dateTime.includes('T')) { + date = new Date(dateTime.replace(/-/g, '/')) + } + // 其他都认为符合 RFC 2822 规范 + else { + date = new Date(dateTime) + } + + const timeSource = { + 'y': date.getFullYear().toString(), // 年 + 'm': (date.getMonth() + 1).toString().padStart(2, '0'), // 月 + 'd': date.getDate().toString().padStart(2, '0'), // 日 + 'h': date.getHours().toString().padStart(2, '0'), // 时 + 'M': date.getMinutes().toString().padStart(2, '0'), // 分 + 's': date.getSeconds().toString().padStart(2, '0') // 秒 + // 有其他格式化字符需求可以继续添加,必须转化成字符串 + } + + for (const key in timeSource) { + const [ret] = new RegExp(`${key}+`).exec(formatStr) || [] + if (ret) { + // 年可能只需展示两位 + const beginIndex = key === 'y' && ret.length === 2 ? 2 : 0 + formatStr = formatStr.replace(ret, timeSource[key].slice(beginIndex)) + } + } + + return formatStr +} \ No newline at end of file diff --git a/CoreCms.Net.Uni-App/CoreShop/pages/subpackage/member/set/userInfo/userInfo.vue b/CoreCms.Net.Uni-App/CoreShop/pages/subpackage/member/set/userInfo/userInfo.vue index 3fa18f04..62d231e0 100644 --- a/CoreCms.Net.Uni-App/CoreShop/pages/subpackage/member/set/userInfo/userInfo.vue +++ b/CoreCms.Net.Uni-App/CoreShop/pages/subpackage/member/set/userInfo/userInfo.vue @@ -14,7 +14,7 @@ - --> + @@ -54,7 +54,7 @@