Skip to content

Commit

Permalink
fix(common): CHECKOUT-4606 Set right timezone in custom date field
Browse files Browse the repository at this point in the history
  • Loading branch information
Luis Sanchez committed Dec 30, 2019
1 parent 4d02de8 commit 10cb6cf
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions src/app/address/mapAddressFromFormValues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,25 @@ export default function mapAddressFromFormValues(formValues: AddressFormValues):
const { customFields: customFieldsObject, ...address } = formValues;
const customFields: Array<{fieldId: string; fieldValue: string}> = [];

forIn(customFieldsObject, (value, key) =>
forIn(customFieldsObject, (value, key) => {
let fieldValue: string;

if (isDate(value)) {
const dateValue = new Date(value);
// We want the field value to match the exact date that the user entered.
// However, when we call toISOString, it will converted to UTC and the user timezone could affect the
// UTC representation by ±1 day. To avoid this, subtract the time offset from the date.
dateValue.setMinutes(dateValue.getMinutes() - dateValue.getTimezoneOffset());
fieldValue = dateValue.toISOString().slice(0, 10);
} else {
fieldValue = value;
}

customFields.push({
fieldId: key,
fieldValue: isDate(value) ? value.toISOString().slice(0, 10) : value,
})
);
fieldValue,
});
});

return {
...address,
Expand Down

0 comments on commit 10cb6cf

Please sign in to comment.