import * as moment from 'moment' ; import 'moment-timezone' ; import { NgbDateAdapter , NgbDateParserFormatter , NgbDateStruct } from '@ng-bootstrap/ng-bootstrap' ; import { Injectable } from '@angular/core' ; import { DateSettingsService } from '../services/date-settings.service' ; /**
 * This Service handles how the date is represented in scripts i.e. ngModel.
 */ @ Injectable ( ) export class NgbStringAdapter extends NgbDateAdapter < Date > { fromModel ( date ) { if ( typeof date === 'string' ) { const newDate = new Date ( date ) ; return newDate ? { day : newDate . getDate ( ) , month : newDate . getMonth ( ) + 1 , year : newDate . getFullYear ( ) , } : null ; } else if ( date != null ) { date = new Date ( date . $date ) ; return date ? { day : date . getDate ( ) , month : date . getMonth ( ) + 1 , year : date . getFullYear ( ) } : null ; } return null ; } param date toModel ( date ) { if ( date != null ) { const d = new Date ( date . year , date . month - 1 , date . day ) ; d . setMinutes ( d . getMinutes ( ) - d . getTimezoneOffset ( ) ) ; return { $date : d . getTime ( ) } ; } return null ; } } /**
 * This Service handles how the date is rendered and parsed from keyboard i.e. in the bound input field.
 */ @ Injectable ( ) export class CustomDateParserFormatter extends NgbDateParserFormatter { constructor ( private dateSettingsService ) { super ( ) ; } parse ( value ) { return null ; } param date format ( date ) { const { timezone , date_format } = this . dateSettingsService . currentDateSettings ; return date ? moment . tz ( moment ( new Date ( Date . UTC ( date . year , date . month - 1 , date . day ) ) ) , timezone ) . format ( date_format ) : null ; } }