Spaces:
Sleeping
Sleeping
File size: 2,929 Bytes
1fff71f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
// Date/time formatting utilities
/**
* Format ISO 8601 timestamp to user-friendly format
* @param isoString ISO 8601 timestamp string
* @returns Formatted date string like "Nov 10, 2025 3:45 PM"
*/
export function formatDateTime(isoString: string): string {
const date = new Date(isoString);
return date.toLocaleString('en-US', {
month: 'short',
day: 'numeric',
year: 'numeric',
hour: 'numeric',
minute: '2-digit',
hour12: true
});
}
/**
* Format ISO 8601 timestamp to short format
* @param isoString ISO 8601 timestamp string
* @returns Formatted date string like "Nov 10, 3:45 PM"
*/
export function formatDateTimeShort(isoString: string): string {
const date = new Date(isoString);
return date.toLocaleString('en-US', {
month: 'short',
day: 'numeric',
hour: 'numeric',
minute: '2-digit',
hour12: true
});
}
/**
* Format ISO 8601 timestamp to just date
* @param isoString ISO 8601 timestamp string
* @returns Formatted date string like "November 10, 2025"
*/
export function formatDate(isoString: string): string {
const date = new Date(isoString);
return date.toLocaleDateString('en-US', {
month: 'long',
day: 'numeric',
year: 'numeric'
});
}
/**
* Format ISO 8601 timestamp to just time
* @param isoString ISO 8601 timestamp string
* @returns Formatted time string like "3:45 PM"
*/
export function formatTime(isoString: string): string {
const date = new Date(isoString);
return date.toLocaleTimeString('en-US', {
hour: 'numeric',
minute: '2-digit',
hour12: true
});
}
/**
* Format ISO 8601 timestamp to relative time
* @param isoString ISO 8601 timestamp string
* @returns Relative time string like "2 hours ago" or "just now"
*/
export function formatRelativeTime(isoString: string): string {
const date = new Date(isoString);
const now = new Date();
const diffMs = now.getTime() - date.getTime();
const diffSec = Math.floor(diffMs / 1000);
const diffMin = Math.floor(diffSec / 60);
const diffHour = Math.floor(diffMin / 60);
const diffDay = Math.floor(diffHour / 24);
if (diffSec < 60) {
return 'just now';
} else if (diffMin < 60) {
return `${diffMin} minute${diffMin !== 1 ? 's' : ''} ago`;
} else if (diffHour < 24) {
return `${diffHour} hour${diffHour !== 1 ? 's' : ''} ago`;
} else if (diffDay < 7) {
return `${diffDay} day${diffDay !== 1 ? 's' : ''} ago`;
} else {
return formatDate(isoString);
}
}
/**
* Convert Unix seconds timestamp to ISO 8601 string
* @param unixSeconds Unix timestamp in seconds
* @returns ISO 8601 timestamp string
*/
export function unixToISO(unixSeconds: number): string {
return new Date(unixSeconds * 1000).toISOString();
}
/**
* Convert ISO 8601 string to Unix seconds timestamp
* @param isoString ISO 8601 timestamp string
* @returns Unix timestamp in seconds
*/
export function isoToUnix(isoString: string): number {
return Math.floor(new Date(isoString).getTime() / 1000);
}
|