|
| 1 | +import { |
| 2 | + CalendarDateFormatterInterface, |
| 3 | + DateFormatterParams |
| 4 | +} from './calendar-date-formatter.interface'; |
| 5 | +import getISOWeek from 'date-fns/get_iso_week'; |
| 6 | +import { DatePipe, VERSION } from '@angular/common'; |
| 7 | + |
| 8 | +/** |
| 9 | + * This will use <a href="https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Intl" target="_blank">Intl</a> API to do all date formatting. It is the default date formatter used by the calendar. |
| 10 | + * |
| 11 | + * You will need to include a <a href="https://github.com/andyearnshaw/Intl.js/">polyfill</a> for older browsers. |
| 12 | + */ |
| 13 | +export class CalendarAngularDateFormatter |
| 14 | + implements CalendarDateFormatterInterface { |
| 15 | + /** |
| 16 | + * The month view header week day labels |
| 17 | + */ |
| 18 | + public monthViewColumnHeader({ date, locale }: DateFormatterParams): string { |
| 19 | + return new DatePipe(locale).transform(date, 'EEEE', locale); |
| 20 | + } |
| 21 | + |
| 22 | + /** |
| 23 | + * The month view cell day number |
| 24 | + */ |
| 25 | + public monthViewDayNumber({ date, locale }: DateFormatterParams): string { |
| 26 | + return new DatePipe(locale).transform(date, 'd', locale); |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * The month view title |
| 31 | + */ |
| 32 | + public monthViewTitle({ date, locale }: DateFormatterParams): string { |
| 33 | + return new DatePipe(locale).transform(date, 'MMMM y', locale); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * The week view header week day labels |
| 38 | + */ |
| 39 | + public weekViewColumnHeader({ date, locale }: DateFormatterParams): string { |
| 40 | + return new DatePipe(locale).transform(date, 'EEEE', locale); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * The week view sub header day and month labels |
| 45 | + */ |
| 46 | + public weekViewColumnSubHeader({ |
| 47 | + date, |
| 48 | + locale |
| 49 | + }: DateFormatterParams): string { |
| 50 | + return new DatePipe(locale).transform(date, 'MMM d', locale); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * The week view title |
| 55 | + */ |
| 56 | + public weekViewTitle({ date, locale }: DateFormatterParams): string { |
| 57 | + const year: string = new DatePipe(locale).transform(date, 'y', locale); |
| 58 | + const weekNumber: number = getISOWeek(date); |
| 59 | + return `Week ${weekNumber} of ${year}`; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * The time formatting down the left hand side of the day view |
| 64 | + */ |
| 65 | + public dayViewHour({ date, locale }: DateFormatterParams): string { |
| 66 | + const format = +VERSION.major === 4 ? 'j' : 'h a'; |
| 67 | + return new DatePipe(locale).transform(date, format, locale); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * The day view title |
| 72 | + */ |
| 73 | + public dayViewTitle({ date, locale }: DateFormatterParams): string { |
| 74 | + return new DatePipe(locale).transform(date, 'EEEE, MMMM d, y', locale); |
| 75 | + } |
| 76 | +} |
0 commit comments