New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ability to customize the default format for DatePipe #44456
Comments
One could easily extend this:
export const DATE_PIPE_DEFAULT_FORMAT = new InjectionToken<string>('DATE_PIPE_DEFAULT_FORMAT');
@Pipe({name: 'date', pure: true})
export class CustomDatePipe extends DatePipe {
constructor(
@Inject(LOCALE_ID) locale: string,
@Inject(DATE_PIPE_DEFAULT_FORMAT) @Optional() private defaultFormat: string
) {
super(locale)
}
transform(value: Date|string|number, format?: string, timezone?: string, locale?: string): string
|null;
transform(value: null|undefined, format?: string, timezone?: string, locale?: string): null;
transform(
value: Date|string|number|null|undefined, format?: string, timezone?: string,
locale?: string): string|null;
transform(
value: Date|string|number|null|undefined, format, timezone?: string,
locale?: string): string|null {
return super.transform(value, format || this.defaultFormat || 'mediumDate', timezone,locale);
}
} Pass to
providers: [
{provide: DatePipe, useClass: CustomDatePipe},
{provide: DATE_PIPE_DEFAULT_FORMAT, useValue: 'shortDate'}
]
Stackblitz example: https://stackblitz.com/edit/angular-ivy-gmtee5?file=src/app/app.module.ts cheers |
@flash-me |
Hey @flash-me, thanks for your input. As you may have noticed in the OP (Alternatives considered), we're aware of the option of creating our own pipe, but the feature request is to include the It sounds a bit strange to ask for every app to create their own |
This feature request is now candidate for our backlog! In the next phase, the community has 60 days to upvote. If the request receives more than 20 upvotes, we'll move it to our consideration list. You can find more details about the feature request process in our documentation. |
Given that we can use an injection token to customize the global timezone, this implementation should not be hard! |
This has been implemented with the |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
Which @angular/* package(s) are relevant/releated to the feature request?
common
Description
Currently the
DatePipe
defaults the format tomediumDate
. It would be great if we could provide anInjectionToken
to customize it, like we already do fortimezone
andlocale
.Proposed solution
Like the
DATE_PIPE_DEFAULT_TIMEZONE
, we could have something likeDATE_PIPE_DEFAULT_FORMAT
, so we can avoid to pass the most common format used in app (in my caseshortDate
) for every| date
or even create our own pipe to override the default one.Alternatives considered
Create our own pipe, but I'd prefer to avoid this... imagine if every app has its own
DatePipe
? Also that sounds a bit weird to haveInjectionToken
fortimezone
andlocale
, but not for the format.The text was updated successfully, but these errors were encountered: