format.ts
1 import dayjs from 'dayjs'; 2 3 /** 4 * Format bytes to human-readable GB string 5 */ 6 export const formatBytes = (bytes: number): string => { 7 if (!bytes) return '-'; 8 const gb = bytes / (1024 * 1024 * 1024); 9 return `${gb.toFixed(1)} GB`; 10 }; 11 12 /** 13 * Format bytes with more precision (2 decimal places) 14 */ 15 export const formatBytesPrecise = (bytes: number): string => { 16 if (!bytes) return '-'; 17 const gb = bytes / (1024 * 1024 * 1024); 18 return `${gb.toFixed(2)} GB`; 19 }; 20 21 /** 22 * Format ISO time string to locale string (zh-CN) 23 */ 24 export const formatTime = (time: string | null): string => { 25 if (!time) return '-'; 26 return new Date(time).toLocaleString('zh-CN'); 27 }; 28 29 /** 30 * Format ISO time string to YYYY-MM-DD HH:mm:ss 31 */ 32 export const formatDateTime = (time: string | null | undefined): string => { 33 if (!time) return '-'; 34 return dayjs(time).format('YYYY-MM-DD HH:mm:ss'); 35 }; 36 37 /** 38 * Format ISO time string to MM-DD HH:mm:ss (shorter) 39 */ 40 export const formatDateTimeShort = (time: string | null | undefined): string => { 41 if (!time) return '-'; 42 return dayjs(time).format('MM-DD HH:mm:ss'); 43 }; 44 45 /** 46 * Format ISO time string to HH:mm:ss (time only) 47 */ 48 export const formatTimeOnly = (time: string | null | undefined): string => { 49 if (!time) return '-'; 50 return dayjs(time).format('HH:mm:ss'); 51 }; 52 53 /** 54 * Format ISO time string to YYYY-MM-DD HH:mm (no seconds) 55 */ 56 export const formatDateMinute = (time: string | null | undefined): string => { 57 if (!time) return '-'; 58 return dayjs(time).format('YYYY-MM-DD HH:mm'); 59 };