Skip to content

Commit f45f5c3

Browse files
feat(core): add input to customize the datetime format of specific datetime-picker component (#12273)
* feat(core): add feature to customize the datetime format of specific datetime-picker component With this feature a custom format can be applied to any date-picker or datetime-picker component. Currently, date-picker or datetime-picker component datetime format depends on the DATE_TIME_FORMATS value which applies to all the date-picker component. Closes #12251 * test(e2e): fix the date time picker in reactive form test Change the scroll view index to focus on correct date time picker in reactive form component. --------- Co-authored-by: Chandan Kumar Shaw <[email protected]>
1 parent c3fa12f commit f45f5c3

File tree

10 files changed

+58
-19
lines changed

10 files changed

+58
-19
lines changed

libs/core/date-picker/date-picker.component.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,10 @@ export class DatePickerComponent<D>
143143
@Input()
144144
required = false;
145145

146+
/** Define a custom datetime format. Can be a string for DayjsDatetimeAdapter or object for FdDatetimeAdapter */
147+
@Input()
148+
customDateTimeFormat: unknown;
149+
146150
/** Text displayed in message */
147151
@Input()
148152
set message(message: string) {
@@ -947,6 +951,9 @@ export class DatePickerComponent<D>
947951

948952
/** @hidden */
949953
private _formatDate(date: Nullable<D>): string {
954+
if (this.customDateTimeFormat) {
955+
return this._dateTimeAdapter.format(date, this.customDateTimeFormat);
956+
}
950957
return this._dateTimeAdapter.format(date, this._dateTimeFormats.display.dateInput);
951958
}
952959

libs/core/datetime-picker/datetime-picker.component.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,10 @@ export class DatetimePickerComponent<D>
140140
@Input()
141141
required = false;
142142

143+
/** Define a custom datetime format. Can be a string for DayjsDatetimeAdapter or object for FdDatetimeAdapter */
144+
@Input()
145+
customDateTimeFormat: unknown;
146+
143147
/**
144148
* Whether the time component should be meridian (am/pm).
145149
* Default value is based on the current locale format option
@@ -813,10 +817,9 @@ export class DatetimePickerComponent<D>
813817
* Format date time entity.
814818
*/
815819
private _formatDateTime(dateTime: D): string {
816-
const formattedDate: string = this._dateTimeAdapter.format(
817-
dateTime,
818-
this._dateTimeFormats.display.dateTimeInput
819-
);
820+
const formattedDate: string = this.customDateTimeFormat
821+
? this._dateTimeAdapter.format(dateTime, this.customDateTimeFormat)
822+
: this._dateTimeAdapter.format(dateTime, this._dateTimeFormats.display.dateTimeInput);
820823
return formattedDate || '';
821824
}
822825

libs/docs/core/date-picker/date-picker-docs.component.html

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,12 @@
6767

6868
<fd-docs-section-title id="formatting" componentName="date-picker"> Formatting </fd-docs-section-title>
6969
<description>
70-
Providing a custom format for the dates is possible through providing <code>DATE_TIME_FORMATS</code> config. Note
71-
that when providing a custom format, the <code>[placeholder]</code> input should be set to match the new format as
72-
well. The <code>dateFormat</code> pipe can also be utilized to convert the date into a string according to the
73-
provided format.
70+
Providing a custom format for the dates is possible through providing <code>DATE_TIME_FORMATS</code> config, this
71+
format applies to all the date-picker component. It is possible to override the custom format for any specific
72+
date-picker component through providing <code>[customDateTimeFormat]</code> input. Note that when providing a custom
73+
format, the <code>[placeholder]</code> input should be set to match the new format as well. The
74+
<code>dateFormat</code> pipe can also be utilized to convert the date into a string according to the provided
75+
format.
7476
</description>
7577
<component-example>
7678
<fd-date-picker-format-example></fd-date-picker-format-example>

libs/docs/core/date-picker/examples/date-picker-format-example.component.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ export const CUSTOM_FD_DATETIME_FORMATS: DateTimeFormats = {
3939
<br />
4040
<div>Selected Date: {{ date | dateFormat }}</div>
4141
<br />
42+
<fd-date-picker
43+
[(ngModel)]="date"
44+
[customDateTimeFormat]="componentSpecificDateTimeFormat"
45+
placeholder="dd-mm-yyyy"
46+
></fd-date-picker>
47+
<br />
48+
<div>Selected Date: {{ date | dateFormat }}</div>
49+
<br />
4250
<fd-date-picker placeholder="mm/dd/yy to mm/dd/yy" type="range" [(ngModel)]="selectedRange"></fd-date-picker>
4351
<br />
4452
<div>Selected First Date: {{ selectedRange?.start | dateFormat }}</div>
@@ -60,6 +68,11 @@ export const CUSTOM_FD_DATETIME_FORMATS: DateTimeFormats = {
6068
export class DatePickerFormatExampleComponent {
6169
date: FdDate;
6270
selectedRange: Nullable<DateRange<FdDate>>;
71+
componentSpecificDateTimeFormat = {
72+
year: '2-digit',
73+
month: 'long',
74+
day: '2-digit'
75+
};
6376

6477
constructor(private datetimeAdapter: DatetimeAdapter<FdDate>) {
6578
const today = this.datetimeAdapter.today();

libs/docs/core/datetime-picker/datetime-picker-docs.component.html

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,12 @@
4848
<separator></separator>
4949
<fd-docs-section-title id="formatting" componentName="datetime-picker"> Formatting </fd-docs-section-title>
5050
<description>
51-
Providing a custom format for the dates is possible through providing <code>DATE_TIME_FORMATS</code> config. Note
52-
that when providing a custom format, the <code>[placeholder]</code> input should be set to match the new format as
53-
well. The <code>dateTimeFormat</code> pipe can also be utilized to convert the date into a string according to the
54-
provided format.
51+
Providing a custom format for the dates is possible through providing <code>DATE_TIME_FORMATS</code> config, this
52+
format applies to all the datetime-picker component. It is possible to override the custom format for any specific
53+
datetime-picker component through providing <code>[customDateTimeFormat]</code> input. Note that when providing a
54+
custom format, the <code>[placeholder]</code> input should be set to match the new format as well. The
55+
<code>dateTimeFormat</code> pipe can also be utilized to convert the date into a string according to the provided
56+
format.
5557
</description>
5658
<component-example>
5759
<fd-datetime-format-example></fd-datetime-format-example>

libs/docs/core/datetime-picker/e2e/date-time-picker.e2e-spec.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -293,26 +293,26 @@ describe('Datetime picker suite', () => {
293293
// issue with timeouts
294294
return;
295295
}
296-
await scrollIntoView(datePickerButton, 5);
297-
await click(datePickerButton, 5);
296+
await scrollIntoView(datePickerButton, 6);
297+
await click(datePickerButton, 6);
298298
await clickDayInCalendarButtonByValue(currentDay);
299299
await selectHoursMinutesAndPeriod();
300300
await click(okButton);
301-
await expect(await getValue(datePickerInput, 5)).toEqual(date2);
301+
await expect(await getValue(datePickerInput, 6)).toEqual(date2);
302302
});
303303

304304
it('verify date time picker i18n example', async () => {
305-
await scrollIntoView(datePickerButton, 7);
305+
await scrollIntoView(datePickerButton, 8);
306306
for (let i = 0; i < i18n.length; i++) {
307307
// skipped due to https://github.com/SAP/fundamental-ngx/issues/6304
308308
if (!browserIsFirefox && i !== 4) {
309309
await click(optionButton);
310310
await click(await getOptionById(i18n[i]));
311-
await click(datePickerButton, 7);
311+
await click(datePickerButton, 8);
312312
await waitForElDisplayed(calendarExpanded);
313313
await selectHoursAndMinutes();
314314
await click(okButton);
315-
await expect(await getValue(datePickerInput, 7)).toContain(dates[i]);
315+
await expect(await getValue(datePickerInput, 8)).toContain(dates[i]);
316316
}
317317
}
318318
});
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
<fd-datetime-picker placeholder="MMM DD, YYYY, h:mm a" [(ngModel)]="date"></fd-datetime-picker>
22
<br />
3+
Selected: {{ date | dateTimeFormat: 'null' }}
34
<br />
45
<br />
5-
Selected: {{ date | dateTimeFormat: 'null' }}
6+
<fd-datetime-picker [customDateTimeFormat]="specificDateTimeFormat" [(ngModel)]="date"></fd-datetime-picker>
7+
<br />
8+
Selected: {{ date }}

libs/docs/core/datetime-picker/examples/datetime-format-example/datetime-format-example.component.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,11 @@ export const CUSTOM_FD_DATETIME_FORMATS: DateTimeFormats = {
4949
})
5050
export class DatetimeFormatExampleComponent {
5151
date = FdDate.getNow();
52+
specificDateTimeFormat = {
53+
year: 'numeric',
54+
month: 'long',
55+
day: 'numeric',
56+
hour: '2-digit',
57+
minute: '2-digit'
58+
};
5259
}

libs/docs/core/dayjs-datetime-adapter/dayjs-datetime-adapter-docs.component.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
The <code>DATE_TIME_FORMATS</code> object is a collection of formats that components such as datepicker, timepicker,
3939
etc. uses when parsing and displaying dates. These formats are passed through to the <code>DatetimeAdapter</code>.
4040
The <code>DayjsDatetimeAdapter</code> has <code>DAYJS_DATETIME_FORMATS</code>, provided in the code of the example.
41+
To customize a datetime format for a specific datetime component use <code>[customDateTimeFormat]</code> input.
4142
<br />
4243
<br />
4344
These formats could be customized, but be sure they are compatible with the DatetimeAdapter.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
<fd-datetime-picker [(ngModel)]="date"></fd-datetime-picker>
2+
<fd-datetime-picker [(ngModel)]="date" [customDateTimeFormat]="'YYYY-MM-DD h:mm A'"></fd-datetime-picker>

0 commit comments

Comments
 (0)