Skip to content

Commit

Permalink
[AAE-7072] - added utc time for date time widget to avoid changing ti… (
Browse files Browse the repository at this point in the history
#7469)

* [AAE-7072] - added utc time for date time widget to avoid changing time on different timezones

* [AAE-7072] - fixed parsing when override is not added
  • Loading branch information
VitoAlbano committed Jan 26, 2022
1 parent 3d544be commit 8a9a9a6
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 11 deletions.
Expand Up @@ -305,7 +305,7 @@ describe('FormFieldModel', () => {
});

const currentDateTime = moment(new Date());
const expectedDateTime = moment(currentDateTime).format('YYYY-MM-DD HH:mm');
const expectedDateTime = moment.utc(currentDateTime).format('YYYY-MM-DD HH:mm');
const expectedDateTimeFormat = `${currentDateTime.utc().format('YYYY-MM-DDTHH:mm:00')}.000Z`;

expect(field.value).toBe(expectedDateTime);
Expand Down
10 changes: 5 additions & 5 deletions lib/core/form/components/widgets/core/form-field.model.ts
Expand Up @@ -335,10 +335,10 @@ export class FormFieldModel extends FormWidgetModel {
if (NumberFieldValidator.isNumber(value)) {
dateValue = moment(value);
} else {
dateValue = this.isDateTimeField(json) ? moment(value, 'YYYY-MM-DD hh:mm A') : moment(value.split('T')[0], 'YYYY-M-D');
dateValue = this.isDateTimeField(json) ? moment.utc(value, 'YYYY-MM-DD hh:mm A') : moment.utc(value.split('T')[0], 'YYYY-M-D');
}
if (dateValue && dateValue.isValid()) {
value = dateValue.format(this.dateDisplayFormat);
value = dateValue.utc().format(this.dateDisplayFormat);
}
}
}
Expand Down Expand Up @@ -415,13 +415,13 @@ export class FormFieldModel extends FormWidgetModel {
break;
case FormFieldTypes.DATETIME:
if (typeof this.value === 'string' && this.value === 'now') {
this.value = moment(new Date()).format(this.dateDisplayFormat);
this.value = moment(new Date()).utc().format(this.dateDisplayFormat);
}

const dateTimeValue = moment(this.value, this.dateDisplayFormat, true).utc();
const dateTimeValue = moment.utc(this.value, this.dateDisplayFormat, true);
if (dateTimeValue && dateTimeValue.isValid()) {
/* cspell:disable-next-line */
this.form.values[this.id] = `${dateTimeValue.format('YYYY-MM-DDTHH:mm:ss')}.000Z`;
this.form.values[this.id] = `${dateTimeValue.utc().format('YYYY-MM-DDTHH:mm:ss')}.000Z`;
} else {
this.form.values[this.id] = null;
this._value = this.value;
Expand Down
Expand Up @@ -67,11 +67,11 @@ export class DateTimeWidgetComponent extends WidgetComponent implements OnInit,

if (this.field) {
if (this.field.minValue) {
this.minDate = moment(this.field.minValue, 'YYYY-MM-DDTHH:mm:ssZ');
this.minDate = moment.utc(this.field.minValue, 'YYYY-MM-DDTHH:mm:ssZ');
}

if (this.field.maxValue) {
this.maxDate = moment(this.field.maxValue, 'YYYY-MM-DDTHH:mm:ssZ');
this.maxDate = moment.utc(this.field.maxValue, 'YYYY-MM-DDTHH:mm:ssZ');
}
}
}
Expand All @@ -84,7 +84,7 @@ export class DateTimeWidgetComponent extends WidgetComponent implements OnInit,
onDateChanged(newDateValue) {
const date = moment(newDateValue, this.field.dateDisplayFormat, true);
if (date.isValid()) {
this.field.value = date.format(this.field.dateDisplayFormat);
this.field.value = moment(date).utc().local().format(this.field.dateDisplayFormat);
} else {
this.field.value = newDateValue;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/core/utils/moment-date-adapter.ts
Expand Up @@ -136,7 +136,7 @@ export class MomentDateAdapter extends DateAdapter<Moment> {
displayFormat = this.overrideDisplayFormat ? this.overrideDisplayFormat : displayFormat;

if (date && date.format) {
return date.format(displayFormat);
return date.utc().local().format(displayFormat);
} else {
return '';
}
Expand Down
Expand Up @@ -44,7 +44,6 @@
<mat-form-field fxFlex>
<input matInput
[matDatepicker]="taskDatePicker"
(keydown)="true"
(focusout)="onDateChanged($any($event).srcElement.value)"
placeholder="{{'ADF_CLOUD_TASK_LIST.START_TASK.FORM.LABEL.DATE'|translate}}"
[(ngModel)]="dueDate"
Expand Down

0 comments on commit 8a9a9a6

Please sign in to comment.