From c4938f6f4e839890b032c75c7a57333a6a9157a9 Mon Sep 17 00:00:00 2001
From: peng <peng.com>
Date: 星期四, 06 十一月 2025 17:06:10 +0800
Subject: [PATCH] 添加新闻功能
---
wx/lib/utils.js | 33 ++++++++++++++++++++++++++++-----
1 files changed, 28 insertions(+), 5 deletions(-)
diff --git a/wx/lib/utils.js b/wx/lib/utils.js
index eccf0a6..d27c9a4 100644
--- a/wx/lib/utils.js
+++ b/wx/lib/utils.js
@@ -7,18 +7,41 @@
* @returns {string} 鏍煎紡鍖栧悗鐨勬棩鏈熷瓧绗︿覆
*/
function formatDate(date, format = 'YYYY-MM-DD HH:mm:ss') {
- if (!date) return ''
-
- const d = new Date(date)
+ if (date === null || date === undefined || date === '') return ''
+ let d
+
+ // 鏁板瓧锛氬彲鑳芥槸鏃堕棿鎴筹紙绉�/姣锛�
+ if (typeof date === 'number') {
+ // 灏忎簬 1e12 瑙嗕负绉掞紝杞崲涓烘绉�
+ d = new Date(date < 1e12 ? date * 1000 : date)
+ } else if (typeof date === 'string') {
+ let s = date.trim()
+ if (!s) return ''
+ // 澶勭悊 ISO 鍜屽父瑙佸悗绔牸寮�
+ // 1) 鍚� 'T' 鐨� ISO 瀛楃涓诧紝鍘绘帀姣涓� Z 鐨勫奖鍝嶏紙淇濇寔鏈湴鏃堕棿锛�
+ s = s.replace('T', ' ').replace(/\.000Z?$/i, '')
+ // 2) YYYY-MM-DD -> 寰俊 JSCore 瀵硅繛瀛楃瑙f瀽涓嶇ǔ瀹氾紝杞枩鏉�
+ if (/^\d{4}-\d{2}-\d{2}(?:\s+\d{2}:\d{2}:\d{2})?$/.test(s)) {
+ s = s.replace(/-/g, '/')
+ // 浠呮棩鏈熻ˉ鏃堕棿锛岄伩鍏� NaN
+ if (s.length === 10) s += ' 00:00:00'
+ }
+ d = new Date(s)
+ } else if (date instanceof Date) {
+ d = date
+ } else {
+ return ''
+ }
+
if (isNaN(d.getTime())) return ''
-
+
const year = d.getFullYear()
const month = String(d.getMonth() + 1).padStart(2, '0')
const day = String(d.getDate()).padStart(2, '0')
const hours = String(d.getHours()).padStart(2, '0')
const minutes = String(d.getMinutes()).padStart(2, '0')
const seconds = String(d.getSeconds()).padStart(2, '0')
-
+
return format
.replace('YYYY', year)
.replace('MM', month)
--
Gitblit v1.8.0