date-fns Adapter
The date-fns adapter allows you to use the date-fns library with Angular Material date components like Datepicker and Timepicker. Source:/home/daytona/workspace/source/src/material-date-fns-adapter/adapter/date-fns-adapter.ts
Installation
Setup
Standalone Components
Provide the adapter in your component or at the application level:NgModule
Provide the adapter in your module:Localization
Provide a locale from date-fns:Custom Date Formats
Customize how dates are displayed and parsed:DateFnsAdapter API
TheDateFnsAdapter class extends Angular Material’s DateAdapter and implements all required methods.
Key Methods
Implemented from/home/daytona/workspace/source/src/material-date-fns-adapter/adapter/date-fns-adapter.ts:58:
getYear(date: Date): number- Get the yeargetMonth(date: Date): number- Get the month (0-11)getDate(date: Date): number- Get the day of monthgetDayOfWeek(date: Date): number- Get day of week (0-6)getMonthNames(style)- Get month names in given stylegetDateNames()- Get array of date namesgetDayOfWeekNames(style)- Get day names in given stylegetYearName(date: Date): string- Get year as stringgetFirstDayOfWeek(): number- Get first day of week (locale-dependent)getNumDaysInMonth(date: Date): number- Get days in monthclone(date: Date): Date- Clone a datecreateDate(year, month, date): Date- Create a new datetoday(): Date- Get today’s dateparse(value, parseFormat)- Parse a date stringformat(date, displayFormat): string- Format a dateaddCalendarYears(date, years): Date- Add yearsaddCalendarMonths(date, months): Date- Add monthsaddCalendarDays(date, days): Date- Add daystoIso8601(date): string- Convert to ISO 8601 stringisDateInstance(obj): boolean- Check if value is a DateisValid(date): boolean- Check if date is validinvalid(): Date- Create an invalid date
Time Support
The adapter also supports time operations:setTime(target, hours, minutes, seconds): DategetHours(date): numbergetMinutes(date): numbergetSeconds(date): numberparseTime(value, parseFormat)addSeconds(date, amount): Date
Date Format Tokens
date-fns uses specific format tokens. Common ones:P- Short date (e.g., 04/29/2023)PP- Medium date (e.g., Apr 29, 2023)PPP- Long date (e.g., April 29th, 2023)PPPP- Full date (e.g., Saturday, April 29th, 2023)p- Short time (e.g., 12:00 AM)pp- Medium time (e.g., 12:00:00 AM)yyyy- 4-digit yearMM- 2-digit monthdd- 2-digit dayMMMM- Full month nameMMM- Short month nameEEEE- Full day nameEEE- Short day name
Example with Datepicker
Example with Timepicker
Why Use date-fns?
- Lightweight: Modular and tree-shakeable
- Immutable: All functions return new dates
- Type-safe: Full TypeScript support
- i18n: Support for 100+ locales
- Modern: Works with ES modules
- Comprehensive: 200+ date utility functions
Comparison with Other Adapters
| Feature | date-fns | Moment.js | Luxon |
|---|---|---|---|
| Bundle size | Small | Large | Medium |
| Tree-shaking | Yes | No | Yes |
| Immutable | Yes | No* | Yes |
| Active development | Yes | Deprecated | Yes |
| TypeScript | Native | Via @types | Native |