File

projects/angular-calendar/src/modules/common/calendar-native-date-formatter/calendar-native-date-formatter.provider.ts

Description

This will use Intl API to do all date formatting.

You will need to include a polyfill for older browsers.

Index

Methods

Constructor

constructor(dateAdapter: DateAdapter)
Parameters :
Name Type Optional
dateAdapter DateAdapter No

Methods

Public dayViewHour
dayViewHour(undefined: DateFormatterParams)

The time formatting down the left hand side of the day view

Parameters :
Name Type Optional
DateFormatterParams No
Returns : string
Public dayViewTitle
dayViewTitle(undefined: DateFormatterParams)

The day view title

Parameters :
Name Type Optional
DateFormatterParams No
Returns : string
Public monthViewColumnHeader
monthViewColumnHeader(undefined: DateFormatterParams)

The month view header week day labels

Parameters :
Name Type Optional
DateFormatterParams No
Returns : string
Public monthViewDayNumber
monthViewDayNumber(undefined: DateFormatterParams)

The month view cell day number

Parameters :
Name Type Optional
DateFormatterParams No
Returns : string
Public monthViewTitle
monthViewTitle(undefined: DateFormatterParams)

The month view title

Parameters :
Name Type Optional
DateFormatterParams No
Returns : string
Public weekViewColumnHeader
weekViewColumnHeader(undefined: DateFormatterParams)

The week view header week day labels

Parameters :
Name Type Optional
DateFormatterParams No
Returns : string
Public weekViewColumnSubHeader
weekViewColumnSubHeader(undefined: DateFormatterParams)

The week view sub header day and month labels

Parameters :
Name Type Optional
DateFormatterParams No
Returns : string
Public weekViewHour
weekViewHour(undefined: DateFormatterParams)

The time formatting down the left hand side of the week view

Parameters :
Name Type Optional
DateFormatterParams No
Returns : string
Public weekViewTitle
weekViewTitle(undefined: DateFormatterParams)

The week view title

Parameters :
Name Type Optional
DateFormatterParams No
Returns : string
import {
  CalendarDateFormatterInterface,
  DateFormatterParams,
} from '../calendar-date-formatter/calendar-date-formatter.interface';
import { Injectable } from '@angular/core';
import { DateAdapter } from '../../../date-adapters/date-adapter';
import { getWeekViewPeriod } from '../util/util';

/**
 * 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.
 *
 * You will need to include a <a href="https://github.com/andyearnshaw/Intl.js/">polyfill</a> for older browsers.
 */
@Injectable()
export class CalendarNativeDateFormatter
  implements CalendarDateFormatterInterface
{
  constructor(protected dateAdapter: DateAdapter) {}

  /**
   * The month view header week day labels
   */
  public monthViewColumnHeader({ date, locale }: DateFormatterParams): string {
    return new Intl.DateTimeFormat(locale, { weekday: 'long' }).format(date);
  }

  /**
   * The month view cell day number
   */
  public monthViewDayNumber({ date, locale }: DateFormatterParams): string {
    return new Intl.DateTimeFormat(locale, { day: 'numeric' }).format(date);
  }

  /**
   * The month view title
   */
  public monthViewTitle({ date, locale }: DateFormatterParams): string {
    return new Intl.DateTimeFormat(locale, {
      year: 'numeric',
      month: 'long',
    }).format(date);
  }

  /**
   * The week view header week day labels
   */
  public weekViewColumnHeader({ date, locale }: DateFormatterParams): string {
    return new Intl.DateTimeFormat(locale, { weekday: 'long' }).format(date);
  }

  /**
   * The week view sub header day and month labels
   */
  public weekViewColumnSubHeader({
    date,
    locale,
  }: DateFormatterParams): string {
    return new Intl.DateTimeFormat(locale, {
      day: 'numeric',
      month: 'short',
    }).format(date);
  }

  /**
   * The week view title
   */
  public weekViewTitle({
    date,
    locale,
    weekStartsOn,
    excludeDays,
    daysInWeek,
  }: DateFormatterParams): string {
    const { viewStart, viewEnd } = getWeekViewPeriod(
      this.dateAdapter,
      date,
      weekStartsOn,
      excludeDays,
      daysInWeek
    );

    const format = (dateToFormat: Date, showYear: boolean) =>
      new Intl.DateTimeFormat(locale, {
        day: 'numeric',
        month: 'short',
        year: showYear ? 'numeric' : undefined,
      }).format(dateToFormat);

    return `${format(
      viewStart,
      viewStart.getUTCFullYear() !== viewEnd.getUTCFullYear()
    )} - ${format(viewEnd, true)}`;
  }

  /**
   * The time formatting down the left hand side of the week view
   */
  public weekViewHour({ date, locale }: DateFormatterParams): string {
    return new Intl.DateTimeFormat(locale, { hour: 'numeric' }).format(date);
  }

  /**
   * The time formatting down the left hand side of the day view
   */
  public dayViewHour({ date, locale }: DateFormatterParams): string {
    return new Intl.DateTimeFormat(locale, { hour: 'numeric' }).format(date);
  }

  /**
   * The day view title
   */
  public dayViewTitle({ date, locale }: DateFormatterParams): string {
    return new Intl.DateTimeFormat(locale, {
      day: 'numeric',
      month: 'long',
      year: 'numeric',
      weekday: 'long',
    }).format(date);
  }
}

results matching ""

    No results matching ""