diff --git a/src/other_pages/comment_reply/index.tsx b/src/other_pages/comment_reply/index.tsx index e98661c..268a46b 100644 --- a/src/other_pages/comment_reply/index.tsx +++ b/src/other_pages/comment_reply/index.tsx @@ -3,6 +3,7 @@ import { View, Text, ScrollView, Image, Input } from "@tarojs/components"; import { Avatar } from "@nutui/nutui-react-taro"; import { withAuth, EmptyState } from "@/components"; import commentService, { CommentActivity } from "@/services/commentService"; +import { formatShortRelativeTime } from "@/utils/timeUtils"; import Taro from "@tarojs/taro"; import "./index.scss"; @@ -78,31 +79,6 @@ const CommentReply = () => { } }; - // 格式化时间显示 - const formatTime = (timeStr: string) => { - if (!timeStr) return ""; - - const date = new Date(timeStr); - const now = new Date(); - const diff = now.getTime() - date.getTime(); - const minutes = Math.floor(diff / (1000 * 60)); - const hours = Math.floor(diff / (1000 * 60 * 60)); - const days = Math.floor(diff / (1000 * 60 * 60 * 24)); - - if (minutes < 60) { - return `${minutes}分钟前`; - } else if (hours < 24) { - return `${hours}小时前`; - } else if (days === 1) { - return "1天前"; - } else if (days < 7) { - return `${days}天前`; - } else { - const month = date.getMonth() + 1; - const day = date.getDate(); - return `${month}月${day}日`; - } - }; // 处理回复 const handleReply = (e: any, item: CommentReplyItem) => { @@ -266,7 +242,7 @@ const CommentReply = () => { {actionText} - {formatTime(item.time)} + {formatShortRelativeTime(item.time)} {item.content} diff --git a/src/other_pages/message/index.tsx b/src/other_pages/message/index.tsx index 1f9dd92..00b704e 100644 --- a/src/other_pages/message/index.tsx +++ b/src/other_pages/message/index.tsx @@ -5,6 +5,7 @@ import GuideBar from "@/components/GuideBar"; import { withAuth, EmptyState } from "@/components"; import noticeService from "@/services/noticeService"; import { useUserInfo } from "@/store/userStore"; +import { formatRelativeTime } from "@/utils/timeUtils"; import Taro from "@tarojs/taro"; import "./index.scss"; @@ -130,33 +131,6 @@ const Message = () => { } }; - // 格式化时间显示 - const formatTime = (timeStr: string) => { - if (!timeStr) return ""; - const date = new Date(timeStr); - const now = new Date(); - const diff = now.getTime() - date.getTime(); - const days = Math.floor(diff / (1000 * 60 * 60 * 24)); - - if (days === 0) { - const hours = date.getHours(); - const minutes = date.getMinutes(); - return `今天 ${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}`; - } else if (days === 1) { - const hours = date.getHours(); - const minutes = date.getMinutes(); - return `昨天 ${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}`; - } else if (days < 3) { - return `${days}天前`; - } else { - const year = date.getFullYear(); - const month = date.getMonth() + 1; - const day = date.getDate(); - const hours = date.getHours(); - const minutes = date.getMinutes(); - return `${year}-${month.toString().padStart(2, '0')}-${day.toString().padStart(2, '0')} ${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}`; - } - }; return ( @@ -219,7 +193,7 @@ const Message = () => { {message.title} - {formatTime(message.create_time)} + {formatRelativeTime(message.create_time)} {message.content} diff --git a/src/other_pages/new_follow/index.tsx b/src/other_pages/new_follow/index.tsx index c7d722c..a86ea5a 100644 --- a/src/other_pages/new_follow/index.tsx +++ b/src/other_pages/new_follow/index.tsx @@ -3,6 +3,7 @@ import { View, Text, ScrollView } from "@tarojs/components"; import { Avatar } from "@nutui/nutui-react-taro"; import { withAuth, EmptyState } from "@/components"; import FollowService from "@/services/followService"; +import { formatShortRelativeTime } from "@/utils/timeUtils"; import Taro from "@tarojs/taro"; import "./index.scss"; @@ -63,33 +64,6 @@ const NewFollow = () => { } }; - // 格式化时间显示 - const formatTime = (timeStr: string) => { - if (!timeStr) return ""; - - const date = new Date(timeStr); - const now = new Date(); - const diff = now.getTime() - date.getTime(); - const minutes = Math.floor(diff / (1000 * 60)); - const hours = Math.floor(diff / (1000 * 60 * 60)); - const days = Math.floor(diff / (1000 * 60 * 60 * 24)); - - if (minutes < 1) { - return "刚刚"; - } else if (minutes < 60) { - return `${minutes}分钟前`; - } else if (hours < 24) { - return `${hours}小时前`; - } else if (days === 1) { - return "1天前"; - } else if (days < 7) { - return `${days}天前`; - } else { - const month = date.getMonth() + 1; - const day = date.getDate(); - return `${month}月${day}日`; - } - }; // 处理回关/取消关注 const handleFollowBack = async (item: FollowItem) => { @@ -202,7 +176,7 @@ const NewFollow = () => { {item.user_signature} ) : ( - {formatTime(item.time)}关注了你 + {formatShortRelativeTime(item.time)}关注了你 )} diff --git a/src/user_pages/queryTransactions/index.tsx b/src/user_pages/queryTransactions/index.tsx index 3703ae2..4be7cfe 100644 --- a/src/user_pages/queryTransactions/index.tsx +++ b/src/user_pages/queryTransactions/index.tsx @@ -185,6 +185,7 @@ const QueryTransactions = () => { const format_time = (time: string) => { time = time.replace(/-/g, "/"); const date = new Date(time); + const year = String(date.getFullYear()); const month = String(date.getMonth() + 1).padStart(2, "0"); const day = String(date.getDate()).padStart(2, "0"); const hours = String(date.getHours()).padStart(2, "0"); @@ -192,7 +193,7 @@ const QueryTransactions = () => { const seconds = String(date.getSeconds()).padStart(2, "0"); return { - date: `2025-${month}-${day}`, + date: `${year}-${month}-${day}`, time: `${hours}:${minutes}:${seconds}`, }; }; diff --git a/src/user_pages/wallet/index.tsx b/src/user_pages/wallet/index.tsx index 9a0fa74..8a28538 100644 --- a/src/user_pages/wallet/index.tsx +++ b/src/user_pages/wallet/index.tsx @@ -377,6 +377,7 @@ const WalletPage: React.FC = () => { const format_time = (time: string) => { time = time.replace(/-/g, "/"); const date = new Date(time); + const year = String(date.getFullYear()); const month = String(date.getMonth() + 1).padStart(2, "0"); const day = String(date.getDate()).padStart(2, "0"); const hours = String(date.getHours()).padStart(2, "0"); @@ -384,7 +385,7 @@ const WalletPage: React.FC = () => { const seconds = String(date.getSeconds()).padStart(2, "0"); return { - date: `2025-${month}-${day}`, + date: `${year}-${month}-${day}`, time: `${hours}:${minutes}:${seconds}`, }; }; diff --git a/src/utils/timeUtils.ts b/src/utils/timeUtils.ts index 86438f8..d75c37f 100644 --- a/src/utils/timeUtils.ts +++ b/src/utils/timeUtils.ts @@ -78,4 +78,108 @@ export const getTime = (time: string): string => { const minuteStr = minute.toString().padStart(2, '0') return `${hour12}:${minuteStr} ${period}` +} + +/** + * 格式化时间显示(相对时间) + * @param timeStr 时间字符串 + * @returns 格式化后的时间字符串 + */ +export const formatRelativeTime = (timeStr: string): string => { + if (!timeStr) return ""; + + const date = new Date(timeStr); + const now = new Date(); + + // 获取日期部分(年-月-日),忽略时间 + const getDateString = (d: Date) => { + const year = d.getFullYear(); + const month = d.getMonth() + 1; + const day = d.getDate(); + return `${year}-${month.toString().padStart(2, '0')}-${day.toString().padStart(2, '0')}`; + }; + + const dateStr = getDateString(date); + const nowStr = getDateString(now); + + // 计算日期差 + const dateObj = new Date(dateStr); + const nowObj = new Date(nowStr); + const diffTime = nowObj.getTime() - dateObj.getTime(); + const diffDays = Math.floor(diffTime / (1000 * 60 * 60 * 24)); + + if (diffDays === 0) { + // 今天 + const hours = date.getHours(); + const minutes = date.getMinutes(); + return `今天 ${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}`; + } else if (diffDays === 1) { + // 昨天 + const hours = date.getHours(); + const minutes = date.getMinutes(); + return `昨天 ${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}`; + } else if (diffDays < 7) { + // 一周内显示天数 + return `${diffDays}天前`; + } else { + // 超过一周显示完整日期 + const year = date.getFullYear(); + const month = date.getMonth() + 1; + const day = date.getDate(); + const hours = date.getHours(); + const minutes = date.getMinutes(); + return `${year}-${month.toString().padStart(2, '0')}-${day.toString().padStart(2, '0')} ${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}`; + } +} + +/** + * 格式化时间显示(简短相对时间) + * @param timeStr 时间字符串 + * @returns 格式化后的时间字符串 + */ +export const formatShortRelativeTime = (timeStr: string): string => { + if (!timeStr) return ""; + + const date = new Date(timeStr); + const now = new Date(); + + // 获取日期部分(年-月-日),忽略时间 + const getDateString = (d: Date) => { + const year = d.getFullYear(); + const month = d.getMonth() + 1; + const day = d.getDate(); + return `${year}-${month.toString().padStart(2, '0')}-${day.toString().padStart(2, '0')}`; + }; + + const dateStr = getDateString(date); + const nowStr = getDateString(now); + + // 计算日期差 + const dateObj = new Date(dateStr); + const nowObj = new Date(nowStr); + const diffTime = nowObj.getTime() - dateObj.getTime(); + const diffDays = Math.floor(diffTime / (1000 * 60 * 60 * 24)); + + // 如果是今天,显示具体时间 + if (diffDays === 0) { + const diff = now.getTime() - date.getTime(); + const minutes = Math.floor(diff / (1000 * 60)); + const hours = Math.floor(diff / (1000 * 60 * 60)); + + if (minutes < 1) { + return "刚刚"; + } else if (minutes < 60) { + return `${minutes}分钟前`; + } else { + return `${hours}小时前`; + } + } else if (diffDays === 1) { + return "1天前"; + } else if (diffDays < 7) { + return `${diffDays}天前`; + } else { + const month = date.getMonth() + 1; + const day = date.getDate(); + return `${month}月${day}日`; + } } \ No newline at end of file