'How work with Javascript Date timezone showing less a day

The application is based in Portugal whose timezone is UTC+00:00 but now is daylight saving.

Here I received a range of date values from the Datepicker component, which was '01/04/2022 - 01/05/2022' and saved it in a state

 const handleCreationDateChange = (dateRange: {
    startDate: Date;
    endDate: Date;
  }) => {
    if (dateRange) {
      setCreationDate(dateRange);
    } else {
      setCreationDate(null);
    }
  };

at this point, the date range object is correct, like the image below

const payload = {
    creationDateFrom: dateRange.creationDateFrom,
    creationDateTo: dateRange.creationDateTo
}

date range object

but the payload object which was sent to the server shows less a day date range object

for a user in Brasil, it shows the correct date but takes a different timezone enter image description here

I've tried converting to .toIsoString() then converting back to Date but it didn't work

this needs to work for users from different timezones



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source