import { CalendarDateFormatter , DateFormatterParams , DateAdapter } from 'angular-calendar' ; import { DAYS_IN_WEEK } from '../utils/calendar-scheduler-utils' ; import { Injectable } from '@angular/core' ; export interface SchedulerDateFormatterParams extends DateFormatterParams { dateAdapter : DateAdapter ; startsWithToday : boolean ; } @ Injectable ( ) export class SchedulerDateFormatter extends CalendarDateFormatter { constructor ( dateAdapter ) { super ( dateAdapter ) ; } /**
     * The time formatting down the left hand side of the day view
     */ public dayViewHour ( { date , locale } ) { return new Intl . DateTimeFormat ( locale , { hour : 'numeric' , minute : 'numeric' } ) . format ( date ) ; } public weekViewTitle ( { dateAdapter , date , locale , weekStartsOn , excludeDays , daysInWeek , startsWithToday } ) { const year = new Intl . DateTimeFormat ( locale , { year : 'numeric' } ) . format ( date ) ; const month = new Intl . DateTimeFormat ( locale , { month : 'short' } ) . format ( date ) ; const dateInner = startsWithToday || daysInWeek < DAYS_IN_WEEK ? date : dateAdapter . startOfWeek ( date , { weekStartsOn : weekStartsOn } ) ; let firstDay = dateInner . getDate ( ) ; while ( excludeDays . includes ( firstDay ) ) { firstDay += 1 ; } let lastDay = firstDay + ( daysInWeek - 1 ) ; while ( excludeDays . includes ( lastDay ) ) { lastDay += 1 ; } let firstDayMonth = month ; let lastDayMonth = month ; let firstDayYear = year ; let lastDayYear = year ; if ( firstDay < 1 ) { const prevMonthDate = new Date ( dateInner . getFullYear ( ) , dateInner . getMonth ( ) - 1 ) ; firstDayMonth = new Intl . DateTimeFormat ( locale , { month : 'short' } ) . format ( prevMonthDate ) ; const daysInPrevMonth = this . daysInMonth ( prevMonthDate ) ; let i = 0 ; let prevMonthDay = daysInPrevMonth ; for ( i = 0 ; i < Math . abs ( firstDay ) ; i ++ ) { prevMonthDay -- ; } firstDay = prevMonthDay ; const prevMonthYear = new Intl . DateTimeFormat ( locale , { year : 'numeric' } ) . format ( prevMonthDate ) ; if ( Number ( prevMonthYear ) < Number ( year ) ) { firstDayYear = prevMonthYear ; } } const daysInMonth = this . daysInMonth ( dateInner ) ; if ( lastDay > daysInMonth ) { const nextMonthDate = new Date ( dateInner . getFullYear ( ) , dateInner . getMonth ( ) + 1 ) ; lastDayMonth = new Intl . DateTimeFormat ( locale , { month : 'short' } ) . format ( nextMonthDate ) ; let i = 0 ; let nextMonthDay = 0 ; for ( i = 0 ; i < ( lastDay - daysInMonth ) ; i ++ ) { nextMonthDay ++ ; } lastDay = nextMonthDay ; const nextMonthYear = new Intl . DateTimeFormat ( locale , { year : 'numeric' } ) . format ( nextMonthDate ) ; if ( Number ( nextMonthYear ) > Number ( year ) ) { lastDayYear = nextMonthYear ; } } return `${ firstDay }` + ( firstDayMonth !== lastDayMonth || lastDay === firstDay ? ' ' + firstDayMonth : '' ) + ( firstDayYear !== lastDayYear ? ' ' + firstDayYear : '' ) + ( lastDay === firstDay ? '' : ` - ${ lastDay } ${ lastDayMonth } ${ lastDayYear }` ) ; } private daysInMonth ( anyDateInMonth ) { return new Date ( anyDateInMonth . getFullYear ( ) , anyDateInMonth . getMonth ( ) + 1 , 0 ) . getDate ( ) ; } }