Untitled
unknown
plain_text
a year ago
4.2 kB
10
Indexable
/**
* Date and time utility functions
*/
/**
* Formats a date string to time with various format options
* @param date - ISO date string or Date object
* @param format - Format type: 'short' (14:30), 'long' (14:30:45), 'iso' (14:30:45.123Z)
* @param locale - Locale for formatting (default: 'sl')
* @param use24Hour - Whether to use 24-hour format (default: true)
* @returns Formatted time string
*/
export function formatTime(
date: string | Date,
format: 'short' | 'long' | 'iso' = 'short',
locale: string = 'sl',
use24Hour: boolean = true
): string {
const dateObj = typeof date === 'string' ? new Date(date) : date;
let timeString: string;
switch (format) {
case 'long':
timeString = new Intl.DateTimeFormat(locale, {
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
hour12: !use24Hour,
}).format(dateObj);
break;
case 'iso':
// Return ISO time format (HH:mm:ss.sssZ or HH:mm:ss.sss)
timeString = dateObj.toISOString().split('T')[1] || '';
break;
case 'short':
default:
timeString = new Intl.DateTimeFormat(locale, {
hour: '2-digit',
minute: '2-digit',
hour12: !use24Hour,
}).format(dateObj);
break;
}
return timeString;
}
/**
* Calculates and formats the duration between two dates
* @param startDate - Start date (ISO string or Date object)
* @param endDate - End date (ISO string or Date object)
* @param format - Format type: 'short' (1:30) or 'long' (1 min 30s)
* @returns Formatted duration string
*/
export function formatDuration(
startDate: string | Date,
endDate: string | Date,
format: 'short' | 'long' = 'short'
): string {
const start = typeof startDate === 'string' ? new Date(startDate) : startDate;
const end = typeof endDate === 'string' ? new Date(endDate) : endDate;
const durationInSeconds = Math.round((end.getTime() - start.getTime()) / 1000);
const hours = Math.floor(durationInSeconds / 3600);
const minutes = Math.floor((durationInSeconds % 3600) / 60);
const seconds = durationInSeconds % 60;
if (format === 'long') {
const parts: string[] = [];
if (hours > 0) parts.push(`${hours}h`);
if (minutes > 0) parts.push(`${minutes} min`);
if (seconds > 0 && hours === 0) parts.push(`${seconds}s`);
return parts.join(' ') || '0s';
}
// Short format
if (hours > 0) {
return `${hours}:${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`;
}
if (minutes === 0) {
return `${seconds}s`;
}
return `${minutes}:${seconds.toString().padStart(2, '0')}`;
}
/**
* Formats a date for display with relative terms (Today, Yesterday) when applicable
* @param date - Date to format (ISO string or Date object)
* @param locale - Locale for formatting (default: 'sl')
* @param options - Additional formatting options
* @returns Formatted date string with relative terms
*/
export function formatDateWithRelative(
date: string | Date,
locale: string = 'sl',
options: {
showYear?: boolean;
todayLabel?: string;
yesterdayLabel?: string;
} = {}
): string {
const {
showYear = true,
todayLabel = 'Danes',
yesterdayLabel = 'Včeraj'
} = options;
const dateObj = typeof date === 'string' ? new Date(date) : date;
const today = new Date();
const yesterday = new Date(today);
yesterday.setDate(yesterday.getDate() - 1);
// Format the base date part
const dateFormatOptions: Intl.DateTimeFormatOptions = {
day: 'numeric',
month: 'short',
...(showYear && { year: 'numeric' })
};
const dateString = new Intl.DateTimeFormat(locale, dateFormatOptions).format(dateObj);
// Check if it's today
if (dateObj.toDateString() === today.toDateString()) {
return `${todayLabel}, ${dateString}`;
}
// Check if it's yesterday
if (dateObj.toDateString() === yesterday.toDateString()) {
return `${yesterdayLabel}, ${dateString}`;
}
// Format other dates with weekday
return new Intl.DateTimeFormat(locale, {
weekday: 'long',
day: 'numeric',
month: 'long',
...(showYear && { year: 'numeric' })
}).format(dateObj);
}Editor is loading...
Leave a Comment