license import { DateService , TranslationWidth , } from '@ui-kitten/components' ; import moment , { Moment } from 'moment' ; export class MomentDateService extends DateService < Moment > { protected localeData : { firstDayOfWeek : number ; defaultFormat : string ; months : { [ key ] : string [ ] ; } ; days : { [ key ] : string [ ] ; } ; } ; constructor ( locale = 'en' ) { super ( ) ; this . setLocale ( locale ) ; } public setLocale ( locale ) { super . setLocale ( locale ) ; this . setMomentLocaleData ( locale ) ; } public addDay ( date , days ) { return this . clone ( date ) . add ( { days } ) ; } public addMonth ( date , months ) { return this . clone ( date ) . add ( { months } ) ; } public addYear ( date , years ) { return this . clone ( date ) . add ( { years } ) ; } public clone ( date ) { return date . clone ( ) . locale ( this . locale ) ; } public compareDates ( date1 , date2 ) { return this . getYear ( date1 ) - this . getYear ( date2 ) || this . getMonth ( date1 ) - this . getMonth ( date2 ) || this . getDate ( date1 ) - this . getDate ( date2 ) ; } public createDate ( year , month , date ) { return moment . utc ( [ year , month , date ] ) ; } public format ( date , format ) { if ( date ) { return date . format ( format || this . localeData . defaultFormat ) ; } return '' ; } public getDate ( date ) { return this . clone ( date ) . date ( ) ; } public getDayOfWeek ( date ) { return this . clone ( date ) . day ( ) ; } public getDayOfWeekNames ( style = TranslationWidth . SHORT ) { return this . localeData . days [ style ] ; } public getFirstDayOfWeek ( ) { return this . localeData . firstDayOfWeek ; } public getMonth ( date ) { return this . clone ( date ) . month ( ) ; } public getMonthEnd ( date ) { return this . clone ( date ) . endOf ( 'month' ) ; } public getMonthName ( date , style = TranslationWidth . SHORT ) { const month = this . getMonth ( date ) ; return this . getMonthNameByIndex ( month , style ) ; } public getMonthNameByIndex ( month , style = TranslationWidth . SHORT ) { return this . localeData . months [ style ] [ month ] ; } public getMonthStart ( date ) { return this . clone ( date ) . startOf ( 'month' ) ; } public getNumberOfDaysInMonth ( date ) { return this . clone ( date ) . daysInMonth ( ) ; } public getYear ( date ) { return this . clone ( date ) . year ( ) ; } public getYearEnd ( date ) { return this . clone ( date ) . endOf ( 'year' ) ; } public getYearStart ( date ) { return this . clone ( date ) . startOf ( 'year' ) ; } public isSameDay ( date1 , date2 ) { return this . isSameMonth ( date1 , date2 ) && this . getDate ( date1 ) === this . getDate ( date2 ) ; } public isSameMonth ( date1 , date2 ) { return this . isSameYear ( date1 , date2 ) && this . getMonth ( date1 ) === this . getMonth ( date2 ) ; } public isSameYear ( date1 , date2 ) { return this . getYear ( date1 ) === this . getYear ( date2 ) ; } public isValidDateString ( date , format ) { return moment ( date , format ) . isValid ( ) ; } public parse ( date , format ) { return moment ( date , format ) ; } public today ( ) { return moment ( ) ; } public getId ( ) { return 'moment' ; } protected setMomentLocaleData ( locale ) { const momentLocaleData = moment . localeData ( locale ) ; this . localeData = { firstDayOfWeek : momentLocaleData . firstDayOfWeek ( ) , defaultFormat : momentLocaleData . longDateFormat ( 'L' ) , months : { [ TranslationWidth . SHORT ] : momentLocaleData . monthsShort ( ) , [ TranslationWidth . LONG ] : momentLocaleData . months ( ) , } , days : { [ TranslationWidth . SHORT ] : momentLocaleData . weekdaysShort ( ) , [ TranslationWidth . LONG ] : momentLocaleData . weekdays ( ) , } , } ; } }