66 * found in the LICENSE file at https://angular.io/license
77 */
88
9- import { Inject , LOCALE_ID , Pipe , PipeTransform } from '@angular/core' ;
9+ import { Inject , InjectionToken , LOCALE_ID , Optional , Pipe , PipeTransform } from '@angular/core' ;
1010import { formatDate } from '../i18n/format_date' ;
1111import { invalidPipeArgumentError } from './invalid_pipe_argument_error' ;
1212
13+ /**
14+ * Optionally-provided default timezone to use for all instances of `DatePipe` (such as `'+0430'`).
15+ * If the value isn't provided, the `DatePipe` will use the end-user's local system timezone.
16+ */
17+ export const DATE_PIPE_DEFAULT_TIMEZONE = new InjectionToken < string > ( 'DATE_PIPE_DEFAULT_TIMEZONE' ) ;
18+
1319// clang-format off
1420/**
1521 * @ngModule CommonModule
1622 * @description
1723 *
1824 * Formats a date value according to locale rules.
19- *
25+ *
2026 * `DatePipe` is executed only when it detects a pure change to the input value.
2127 * A pure change is either a change to a primitive input value
2228 * (such as `String`, `Number`, `Boolean`, or `Symbol`),
@@ -29,6 +35,11 @@ import {invalidPipeArgumentError} from './invalid_pipe_argument_error';
2935 * in another language, you must import the corresponding locale data.
3036 * See the [I18n guide](guide/i18n-common-format-data-locale) for more information.
3137 *
38+ * The time zone of the formatted value can be specified either by passing it in as the second
39+ * parameter of the pipe, or by setting the default through the `DATE_PIPE_DEFAULT_TIMEZONE`
40+ * injection token. The value that is passed in as the second parameter takes precedence over
41+ * the one defined using the injection token.
42+ *
3243 * @see `formatDate()`
3344 *
3445 *
@@ -166,16 +177,18 @@ import {invalidPipeArgumentError} from './invalid_pipe_argument_error';
166177// clang-format on
167178@Pipe ( { name : 'date' , pure : true } )
168179export class DatePipe implements PipeTransform {
169- constructor ( @Inject ( LOCALE_ID ) private locale : string ) { }
180+ constructor (
181+ @Inject ( LOCALE_ID ) private locale : string ,
182+ @Inject ( DATE_PIPE_DEFAULT_TIMEZONE ) @Optional ( ) private defaultTimezone ?: string | null ) { }
170183
171184 /**
172185 * @param value The date expression: a `Date` object, a number
173186 * (milliseconds since UTC epoch), or an ISO string (https://www.w3.org/TR/NOTE-datetime).
174187 * @param format The date/time components to include, using predefined options or a
175188 * custom format string.
176- * @param timezone A timezone offset (such as `'+0430'`), or a standard
177- * UTC/GMT or continental US timezone abbreviation.
178- * When not supplied, uses the end-user's local system timezone.
189+ * @param timezone A timezone offset (such as `'+0430'`), or a standard UTC/GMT, or continental US
190+ * timezone abbreviation. When not supplied, either the value of the `DATE_PIPE_DEFAULT_TIMEZONE`
191+ * injection token is used or the end-user's local system timezone.
179192 * @param locale A locale code for the locale format rules to use.
180193 * When not supplied, uses the value of `LOCALE_ID`, which is `en-US` by default.
181194 * See [Setting your app locale](guide/i18n-common-locale-id).
@@ -193,7 +206,8 @@ export class DatePipe implements PipeTransform {
193206 if ( value == null || value === '' || value !== value ) return null ;
194207
195208 try {
196- return formatDate ( value , format , locale || this . locale , timezone ) ;
209+ return formatDate (
210+ value , format , locale || this . locale , timezone ?? this . defaultTimezone ?? undefined ) ;
197211 } catch ( error ) {
198212 throw invalidPipeArgumentError ( DatePipe , error . message ) ;
199213 }
0 commit comments