修复 时间 格式化为题

This commit is contained in:
张成
2025-10-18 17:47:09 +08:00
parent de52dc4762
commit 62bc0d9c24
6 changed files with 114 additions and 84 deletions

View File

@@ -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 = () => {
<View className="action-row">
<Text className="action-text">{actionText}</Text>
<Text className="time-text">{formatTime(item.time)}</Text>
<Text className="time-text">{formatShortRelativeTime(item.time)}</Text>
</View>
<Text className="comment-text">{item.content}</Text>

View File

@@ -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 (
<View className="message-container">
@@ -219,7 +193,7 @@ const Message = () => {
<Text className="card-title">{message.title}</Text>
</View>
<View className="card-time-row">
<Text className="card-time">{formatTime(message.create_time)}</Text>
<Text className="card-time">{formatRelativeTime(message.create_time)}</Text>
</View>
<View className="card-content-row">
<Text className="card-content">{message.content}</Text>

View File

@@ -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 = () => {
<Text className="user-signature">{item.user_signature}</Text>
) : (
<View className="action-row">
<Text className="action-text">{formatTime(item.time)}</Text>
<Text className="action-text">{formatShortRelativeTime(item.time)}</Text>
</View>
)}
</View>