Moment.js Adapter
The Moment.js adapter allows you to use the Moment.js library with Angular Material date components like Datepicker and Timepicker. Source:/home/daytona/workspace/source/src/material-moment-adapter/adapter/moment-date-adapter.ts
Important Note
Moment.js is now in maintenance mode. The Moment.js team recommends using alternatives like Luxon or date-fns for new projects. However, the adapter is still maintained for existing projects. From the Moment.js documentation:We now generally consider Moment to be a legacy project in maintenance mode. It is not dead, but it is indeed done.
Installation
Setup
Standalone Components
Provide the adapter in your component or at the application level:NgModule
Provide the adapter in your module:Adapter Options
Configure the adapter usingMAT_MOMENT_DATE_ADAPTER_OPTIONS:
Option Properties
Source:/home/daytona/workspace/source/src/material-moment-adapter/adapter/moment-date-adapter.ts:22
strict(boolean) - Enable strict parsing mode. Default:falseuseUtc(boolean) - Use UTC dates instead of local time. Default:false
Localization
Moment.js has built-in locales. Set the locale usingMAT_DATE_LOCALE:
Strict Parsing Mode
Strict mode requires dates to exactly match the format:UTC Mode
Work with UTC dates:Custom Date Formats
MomentDateAdapter API
TheMomentDateAdapter class extends Angular Material’s DateAdapter.
Key Methods
Implemented from/home/daytona/workspace/source/src/material-moment-adapter/adapter/moment-date-adapter.ts:58:
getYear(date: Moment): number- Get the yeargetMonth(date: Moment): number- Get the month (0-11)getDate(date: Moment): number- Get the day of monthgetDayOfWeek(date: Moment): number- Get day of weekgetMonthNames(style)- Get month namesgetDateNames()- Get array of date namesgetDayOfWeekNames(style)- Get day namesgetYearName(date: Moment): string- Get year as stringgetFirstDayOfWeek(): number- Get first day of weekgetNumDaysInMonth(date: Moment): number- Get days in monthclone(date: Moment): Moment- Clone a date (always clones with correct locale)createDate(year, month, date): Moment- Create a new datetoday(): Moment- Get today’s dateparse(value, parseFormat)- Parse a date stringformat(date, displayFormat): string- Format a dateaddCalendarYears(date, years): Moment- Add yearsaddCalendarMonths(date, months): Moment- Add monthsaddCalendarDays(date, days): Moment- Add daystoIso8601(date): string- Convert to ISO 8601 stringisDateInstance(obj): boolean- Check if value is a MomentisValid(date): boolean- Check if date is validinvalid(): Moment- Create an invalid date
Time Support
setTime(target, hours, minutes, seconds): MomentgetHours(date): numbergetMinutes(date): numbergetSeconds(date): numberparseTime(value, parseFormat)addSeconds(date, amount): Moment
Moment.js Format Tokens
Common Moment.js format tokens:M- Month number (1-12)MM- Month number with leading zero (01-12)MMM- Short month name (Jan-Dec)MMMM- Full month name (January-December)D- Day of month (1-31)DD- Day of month with leading zero (01-31)d- Day of week (0-6)dd- Min day name (Su-Sa)ddd- Short day name (Sun-Sat)dddd- Full day name (Sunday-Saturday)YY- 2-digit yearYYYY- 4-digit yearh- Hour 12-hour (1-12)hh- Hour 12-hour with leading zeroH- Hour 24-hour (0-23)HH- Hour 24-hour with leading zerom- Minutes (0-59)mm- Minutes with leading zeros- Seconds (0-59)ss- Seconds with leading zeroA- AM/PMa- am/pm
Example with Datepicker
Migration Path
If you’re using Moment.js and want to migrate:To Luxon
To date-fns
Why Moment.js is in Maintenance Mode
From the Moment.js team:- Large bundle size - Moment.js is quite large and not tree-shakeable
- Mutable API - Moment objects are mutable, leading to bugs
- Better alternatives - Modern libraries like Luxon and date-fns exist
- Native improvements - JavaScript’s
IntlandTemporalAPIs are improving
When to Use Moment.js Adapter
- Existing projects - You’re already using Moment.js extensively
- Legacy code - Migrating would be too costly
- Temporary solution - You plan to migrate eventually