2025-12-07 14:31:16 +01:00
|
|
|
import dayjs from 'dayjs';
|
2025-12-10 00:27:19 +01:00
|
|
|
import utc from 'dayjs/plugin/utc';
|
|
|
|
|
import timezone from 'dayjs/plugin/timezone';
|
|
|
|
|
import isoWeek from 'dayjs/plugin/isoWeek';
|
2025-12-07 14:31:16 +01:00
|
|
|
import { ITimeFormatConfig } from './ITimeFormatConfig';
|
|
|
|
|
|
2025-12-10 00:27:19 +01:00
|
|
|
// Enable dayjs plugins
|
|
|
|
|
dayjs.extend(utc);
|
|
|
|
|
dayjs.extend(timezone);
|
|
|
|
|
dayjs.extend(isoWeek);
|
|
|
|
|
|
2025-12-07 14:31:16 +01:00
|
|
|
export class DateService {
|
2025-12-10 00:27:19 +01:00
|
|
|
private timezone: string;
|
|
|
|
|
|
|
|
|
|
constructor(private config: ITimeFormatConfig) {
|
|
|
|
|
this.timezone = config.timezone;
|
|
|
|
|
}
|
2025-12-07 14:31:16 +01:00
|
|
|
|
|
|
|
|
parseISO(isoString: string): Date {
|
|
|
|
|
return dayjs(isoString).toDate();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getDayName(date: Date, format: 'short' | 'long' = 'short'): string {
|
|
|
|
|
return new Intl.DateTimeFormat(this.config.locale, { weekday: format }).format(date);
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-07 21:54:12 +01:00
|
|
|
getWeekDates(offset = 0, days = 7): string[] {
|
2025-12-07 14:31:16 +01:00
|
|
|
const monday = dayjs().startOf('week').add(1, 'day').add(offset, 'week');
|
2025-12-07 21:54:12 +01:00
|
|
|
return Array.from({ length: days }, (_, i) =>
|
2025-12-07 14:31:16 +01:00
|
|
|
monday.add(i, 'day').format('YYYY-MM-DD')
|
|
|
|
|
);
|
|
|
|
|
}
|
2025-12-10 00:27:19 +01:00
|
|
|
|
|
|
|
|
// ============================================
|
|
|
|
|
// FORMATTING
|
|
|
|
|
// ============================================
|
|
|
|
|
|
|
|
|
|
formatTime(date: Date, showSeconds = false): string {
|
|
|
|
|
const pattern = showSeconds ? 'HH:mm:ss' : 'HH:mm';
|
|
|
|
|
return dayjs(date).format(pattern);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
formatTimeRange(start: Date, end: Date): string {
|
|
|
|
|
return `${this.formatTime(start)} - ${this.formatTime(end)}`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
formatDate(date: Date): string {
|
|
|
|
|
return dayjs(date).format('YYYY-MM-DD');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getDateKey(date: Date): string {
|
|
|
|
|
return this.formatDate(date);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ============================================
|
|
|
|
|
// TIME CALCULATIONS
|
|
|
|
|
// ============================================
|
|
|
|
|
|
|
|
|
|
timeToMinutes(timeString: string): number {
|
|
|
|
|
const parts = timeString.split(':').map(Number);
|
|
|
|
|
const hours = parts[0] || 0;
|
|
|
|
|
const minutes = parts[1] || 0;
|
|
|
|
|
return hours * 60 + minutes;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
minutesToTime(totalMinutes: number): string {
|
|
|
|
|
const hours = Math.floor(totalMinutes / 60);
|
|
|
|
|
const minutes = totalMinutes % 60;
|
|
|
|
|
return dayjs().hour(hours).minute(minutes).format('HH:mm');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getMinutesSinceMidnight(date: Date): number {
|
|
|
|
|
const d = dayjs(date);
|
|
|
|
|
return d.hour() * 60 + d.minute();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ============================================
|
|
|
|
|
// UTC CONVERSIONS
|
|
|
|
|
// ============================================
|
|
|
|
|
|
|
|
|
|
toUTC(localDate: Date): string {
|
|
|
|
|
return dayjs.tz(localDate, this.timezone).utc().toISOString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fromUTC(utcString: string): Date {
|
|
|
|
|
return dayjs.utc(utcString).tz(this.timezone).toDate();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ============================================
|
|
|
|
|
// DATE CREATION
|
|
|
|
|
// ============================================
|
|
|
|
|
|
|
|
|
|
createDateAtTime(baseDate: Date | string, timeString: string): Date {
|
|
|
|
|
const totalMinutes = this.timeToMinutes(timeString);
|
|
|
|
|
const hours = Math.floor(totalMinutes / 60);
|
|
|
|
|
const minutes = totalMinutes % 60;
|
|
|
|
|
return dayjs(baseDate).startOf('day').hour(hours).minute(minutes).toDate();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getISOWeekDay(date: Date | string): number {
|
|
|
|
|
return dayjs(date).isoWeekday(); // 1=Monday, 7=Sunday
|
|
|
|
|
}
|
2025-12-07 14:31:16 +01:00
|
|
|
}
|