import * as jstz from 'jstz'; import { Injectable, Inject, LOCALE_ID } from '@angular/core'; import { formatDate, getLocaleFirstDayOfWeek, WeekDay } from '@angular/common'; @Injectable() export class MwCoreDateTimeService { static getFirstDayOfWeek(firstDayOfWeek: WeekDay, date?: Date): Date { const result = new Date(+(date || new Date())); let subDays = firstDayOfWeek - result.getDay(); subDays = subDays > 0 ? -6 : subDays; result.setHours(subDays * 24, 0, 0, 0); return result; } constructor(@Inject(LOCALE_ID) private localeId: string) {} copyUtcTimeToLocalTime(date: Date): Date { const result = new Date(date); result.setHours(result.getUTCHours()); return result; } getLocalOlsonTimeZone(): string { return jstz.determine().name(); } areDatesSame(d1: Date, d2: Date): boolean { return +d1 === +d2; } mergeDateTime(date: Date, time: Date): Date { if (date && !Object.is(+date, NaN) && time && !Object.is(+time, NaN)) { const result = new Date( date.getFullYear(), date.getMonth(), date.getDate() ); result.setHours(time.getHours()); result.setMinutes(time.getMinutes()); return result; } return new Date(NaN); } formatTime(value: Date, format?: string): string { return value && formatDate(value, format || 'shortTime', this.localeId); } formatDate(value: Date, format?: string): string { return value && formatDate(value, format || 'shortDate', this.localeId); } formatDateTime(value: Date, format?: string): string { return value && formatDate(value, format || 'short', this.localeId); } getLocalDateFormat(): string { const date = new Date(1950, 11, 13); const formattedDate = formatDate(date, 'short', this.localeId); return formattedDate .replace('1950', 'yyyy') .replace('12', 'MM') .replace('13', 'dd'); } getLocalDateSafeCharsPattern(): string { return ( '^[0-9\\' + this.getLocalDateFormat() .replace(/[y|M|d]/g, '') .replace(/(.*)/g, '$1')[0] + '-]$' ); } getLocalDatePattern(): string { return ( '^' + this.getLocalDateFormat() .replace(/y/g, 'd') .replace(/[M|d]/g, 'd??') .replace(/([^.?])/g, '\\$1') + '$' ); } getStartOfWeek(date?: Date): Date { const firstDayOfWeek = getLocaleFirstDayOfWeek(this.localeId); return MwCoreDateTimeService.getFirstDayOfWeek(firstDayOfWeek, date); } getWeekRange(date?: Date): { start: Date; end: Date; } { const start = new Date(+this.getStartOfWeek(date)); const end = new Date(+start); end.setHours(7 * 24, 0, 0, -1); return { start, end, }; } getWorkWeekRange(date?: Date): { start: Date; end: Date; } { const start = new Date(+this.getStartOfWeek(date)); const end = new Date(+start); end.setHours(5 * 24, 0, 0, -1); return { start, end, }; } getDayRange(date?: Date): { start: Date; end: Date } { const start = new Date(+(date || new Date())); const end = new Date(+start); start.setHours(0, 0, 0, 0); end.setHours(24, 0, 0, -1); return { start, end, }; } getTwoWeeksRange(date?: Date): { start: Date; end: Date; } { const start = new Date(+this.getStartOfWeek(date)); const end = new Date(+start); end.setHours(14 * 24, 0, 0, -1); return { start, end, }; } getMonthRange(date?: Date): { start: Date; end: Date } { const localDate = new Date(+(date || new Date())); const start = new Date(localDate.getFullYear(), localDate.getMonth(), 1); const end = new Date( localDate.getFullYear(), localDate.getMonth() + 1, 1, 0, 0, 0, -1 ); return { start, end, }; } isInRange( value: Date, range: { start: Date; end: Date }, isIncludeStart?: boolean ): boolean { if (typeof value === 'string') { value = new Date(value); } return ( ((range.start === null || value > range.start || (isIncludeStart && value >= range.start)) && (range.end === null || value <= range.end)) === true ); } equalTime(t1: Date, t2: Date, includeSeconds = false): boolean { return ( t1.getUTCHours() === t2.getUTCHours() && t1.getUTCMinutes() === t2.getUTCMinutes() && (includeSeconds ? t1.getUTCSeconds() === t2.getUTCSeconds() : true) ); } equalDate(t1: Date, t2: Date): boolean { return ( t1.getFullYear() === t2.getFullYear() && t1.getMonth() === t2.getMonth() && t1.getDate() === t2.getDate() ); } isToday(date: Date): boolean { const now = new Date(); return this.equalDate(date, now); } }