hiển thị thuyền thông tin tàu
This commit is contained in:
43
services/time_service.tsx
Normal file
43
services/time_service.tsx
Normal file
@@ -0,0 +1,43 @@
|
||||
import dayjs from "dayjs";
|
||||
import "dayjs/locale/vi";
|
||||
import relativeTime from "dayjs/plugin/relativeTime";
|
||||
|
||||
dayjs.extend(relativeTime);
|
||||
dayjs.locale("vi");
|
||||
|
||||
export { dayjs };
|
||||
|
||||
/**
|
||||
* Chuyển đổi unix timestamp thành text thời gian tương đối
|
||||
* @param unixTime - Unix timestamp (seconds hoặc milliseconds)
|
||||
* @returns String mô tả thời gian tương đối (vd: "5 phút trước", "2 giờ trước")
|
||||
*/
|
||||
export function formatRelativeTime(unixTime: number): string {
|
||||
if (!unixTime || unixTime <= 0) return "Không rõ";
|
||||
|
||||
// Xác định đơn vị timestamp (seconds hoặc milliseconds)
|
||||
const timestamp = unixTime < 10000000000 ? unixTime * 1000 : unixTime;
|
||||
|
||||
const updateDate = new Date(timestamp);
|
||||
const now = new Date();
|
||||
const diffMs = now.getTime() - updateDate.getTime();
|
||||
|
||||
// Nếu thời gian trong tương lai
|
||||
if (diffMs < 0) return "Vừa xong";
|
||||
|
||||
const diffSeconds = Math.floor(diffMs / 1000);
|
||||
const diffMins = Math.floor(diffSeconds / 60);
|
||||
const diffHours = Math.floor(diffMins / 60);
|
||||
const diffDays = Math.floor(diffHours / 24);
|
||||
const diffWeeks = Math.floor(diffDays / 7);
|
||||
const diffMonths = Math.floor(diffDays / 30);
|
||||
const diffYears = Math.floor(diffDays / 365);
|
||||
|
||||
if (diffSeconds < 60) return "Vừa xong";
|
||||
if (diffMins < 60) return `${diffMins} phút trước`;
|
||||
if (diffHours < 24) return `${diffHours} giờ trước`;
|
||||
if (diffDays < 7) return `${diffDays} ngày trước`;
|
||||
if (diffWeeks < 4) return `${diffWeeks} tuần trước`;
|
||||
if (diffMonths < 12) return `${diffMonths} tháng trước`;
|
||||
return `${diffYears} năm trước`;
|
||||
}
|
||||
Reference in New Issue
Block a user