Updated
This commit is contained in:
39
src/lib/formatTime.ts
Normal file
39
src/lib/formatTime.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
/**
|
||||
* Formats milliseconds into a readable time string with full precision and rounded version.
|
||||
* @param ms - Time in milliseconds
|
||||
* @returns Formatted string like "01:23:45 (1h 24m)" or "00:05:23 (5m)"
|
||||
*/
|
||||
export function formatDuration(ms: number): string {
|
||||
const totalSeconds = Math.floor(ms / 1000);
|
||||
const hours = Math.floor(totalSeconds / 3600);
|
||||
const minutes = Math.floor((totalSeconds % 3600) / 60);
|
||||
const seconds = totalSeconds % 60;
|
||||
|
||||
const timeStr = `${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`;
|
||||
|
||||
// Calculate rounded version for easy reading
|
||||
const totalMinutes = Math.round(ms / 1000 / 60);
|
||||
const roundedHours = Math.floor(totalMinutes / 60);
|
||||
const roundedMinutes = totalMinutes % 60;
|
||||
|
||||
let roundedStr = '';
|
||||
if (roundedHours > 0) {
|
||||
roundedStr = roundedMinutes > 0 ? `${roundedHours}h ${roundedMinutes}m` : `${roundedHours}h`;
|
||||
} else {
|
||||
roundedStr = `${roundedMinutes}m`;
|
||||
}
|
||||
|
||||
return `${timeStr} (${roundedStr})`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats a time range between two dates.
|
||||
* @param start - Start date
|
||||
* @param end - End date (null if still running)
|
||||
* @returns Formatted duration string or "Running..."
|
||||
*/
|
||||
export function formatTimeRange(start: Date, end: Date | null): string {
|
||||
if (!end) return 'Running...';
|
||||
const ms = end.getTime() - start.getTime();
|
||||
return formatDuration(ms);
|
||||
}
|
||||
Reference in New Issue
Block a user