import { Injectable } from '@angular/core' ; import { format as dateFnsFormat , parse as dateFnsParse } from 'date-fns' ; import { NglDateAdapterBase } from './adapter' ; const PATTERNS = { 'big-endian' : 'yyyy/MM/dd' , 'little-endian' : 'dd/MM/yyyy' , 'middle-endian' : 'MM/dd/yyyy' , } ; @ Injectable ( ) export class NglDateAdapter extends NglDateAdapterBase { parse ( value , format ) { const date = dateFnsParse ( value , format , new Date ( ) ) ; return this . isValidDate ( date ) ? date : null ; } format ( date , format ) { return dateFnsFormat ( date , format ) ; } pattern ( name , delimiter ) { const pattern = PATTERNS [ name ] ; return ( delimiter && delimiter !== '/' ) ? pattern . replace ( /\//g , delimiter ) : pattern ; } private isValidDate ( value ) { const dateWrapper = new Date ( value ) ; return ! isNaN ( dateWrapper . getDate ( ) ) ; } }