import { Component } from "@angular/core" ; import { IDateParams } from "ag-grid/main" ; import { IDateAngularComp } from "ag-grid-angular/main" ; @ Component ( { selector : 'ag-full-width-grid' , templateUrl : 'date.component.html' , styleUrls : [ 'date.component.css' ] , } ) export class DateComponent implements IDateAngularComp { private date : Date ; private params : IDateParams ; public dd : string = '' ; public mm : string = '' ; public yyyy : string = '' ; agInit ( params ) { this . params = params ; } ngOnDestroy ( ) { console . log ( `Destroying DateComponent` ) ; } onResetDate ( ) { this . dd = '' ; this . mm = '' ; this . yyyy = '' ; this . date = null ; this . params . onDateChanged ( ) ; } onDateChanged ( on , newValue ) { this . date = this . parseDate ( on === 'dd' ? newValue : this . dd , on === 'mm' ? newValue : this . mm , on === 'yyyy' ? newValue : this . yyyy ) ; this . params . onDateChanged ( ) ; } getDate ( ) { return this . date ; } setDate ( date ) { this . dd = date . getDate ( ) + '' ; this . mm = ( date . getMonth ( ) + 1 ) + '' ; this . yyyy = date . getFullYear ( ) + '' ; this . date = date ; this . params . onDateChanged ( ) ; } parseDate ( dd , mm , yyyy ) { if ( dd . trim ( ) === '' || mm . trim ( ) === '' || yyyy . trim ( ) === '' ) { return null ; } let day = Number ( dd ) ; let month = Number ( mm ) ; let year = Number ( yyyy ) ; let date = new Date ( year , month - 1 , day ) ; if ( isNaN ( date . getTime ( ) ) ) { return null ; } if ( date . getDate ( ) != day || date . getMonth ( ) + 1 != month || date . getFullYear ( ) != year ) { return null ; } return date ; } }