Created
September 5, 2017 13:50
-
-
Save egavard/2c30dec7a81c08b32a0c211badad0e22 to your computer and use it in GitHub Desktop.
moment-date-adapter.ts A DateAdapter fully working with Angular material 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {DateAdapter} from '@angular/material'; | |
import * as moment from 'moment'; | |
import {Inject, LOCALE_ID} from '@angular/core'; | |
export class MomentDateAdapter extends DateAdapter<moment.Moment> { | |
constructor(@Inject(LOCALE_ID)locale: any) { | |
super(); | |
this.setLocale(locale); | |
moment.locale(locale); | |
} | |
getYear(date: moment.Moment): number { | |
return date.year(); | |
} | |
getMonth(date: moment.Moment): number { | |
return date.month(); | |
} | |
getDate(date: moment.Moment): number { | |
return date.date(); | |
} | |
getDayOfWeek(date: moment.Moment): number { | |
return date.get('weekYear'); | |
} | |
getMonthNames(style): string[] { | |
if ('long' === style) { | |
return moment.months(); | |
} else if ('narrow' === style || 'short' === style) { | |
return moment.monthsShort(); | |
} | |
} | |
getDateNames(): string[] { | |
return [ | |
'1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', | |
'18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31' | |
]; | |
} | |
getDayOfWeekNames(style): string[] { | |
if ('long' === style) { | |
return moment.weekdays(); | |
} else if ('narrow' === style) { | |
return moment.weekdaysMin(); | |
} else if ('short' === style) { | |
return moment.weekdaysShort(); | |
} | |
} | |
getYearName(date: moment.Moment): string { | |
return '' + date.year(); | |
} | |
getFirstDayOfWeek(): number { | |
return 1; | |
} | |
getNumDaysInMonth(date: moment.Moment): number { | |
return date.daysInMonth(); | |
} | |
clone(date: moment.Moment): moment.Moment { | |
return date.clone(); | |
} | |
createDate(year: number, month: number, date: number): moment.Moment { | |
return moment.utc([year, month, date]); | |
} | |
today(): moment.Moment { | |
return moment().utc().startOf('day'); | |
} | |
parse(value: any, parseFormat: any): any | moment.Moment { | |
let m = moment.utc(value, moment.localeData().longDateFormat('L')); | |
if (!m.isValid()) { | |
m = moment.utc(value, parseFormat, true); | |
} | |
if (!m.isValid()) { | |
m = moment.utc(value); | |
} | |
if (m.isValid()) { | |
return m; | |
} else { | |
return null; | |
} | |
} | |
format(date: moment.Moment, displayFormat: any): string { | |
if (!displayFormat.day) { | |
return date ? moment.utc(date).format('MMM YYYY') : ''; | |
} else { | |
return date ? moment.utc(date).format('L') : ''; | |
} | |
} | |
addCalendarYears(date: moment.Moment, years: number): moment.Moment { | |
return date.clone().add(years, 'years'); | |
} | |
addCalendarMonths(date: moment.Moment, months: number): moment.Moment { | |
return date.clone().add(months, 'months'); | |
} | |
addCalendarDays(date: moment.Moment, days: number): moment.Moment { | |
return date.clone().add(days, 'days'); | |
} | |
getISODateString(date: moment.Moment): string { | |
return date.toISOString(); | |
} | |
isDateInstance(obj: any): boolean { | |
return moment.isMoment(obj) || null === obj; | |
} | |
isValid(date: moment.Moment): boolean { | |
if (date) { | |
return date.isValid(); | |
} | |
return false; | |
} | |
setLocale(locale: any): void { | |
this.locale = locale; | |
} | |
compareDate(first: moment.Moment, second: moment.Moment): number { | |
return first.diff(second); | |
} | |
sameDate(first: any | moment.Moment, second: any | moment.Moment): boolean { | |
return first && second && (typeof first === typeof second) && (first as moment.Moment).isSame((second as moment.Moment)); | |
} | |
clampDate(date: moment.Moment, min?: any | moment.Moment, max?: any | moment.Moment): moment.Moment { | |
if (min && date.isBefore(min)) { | |
return min; | |
} else if (max && date.isAfter(max)) { | |
return max; | |
} else { | |
return date; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment