Hey I am receiving dates from a database which saves these dates as UTC. When I print one of the dates with console.log it looks like this 2021-04-07T07:00:00.000Z
.
Now I want to convert it into Europe/Berlin
time. But not as a String, I want it as Javascript Date.
What i tried to do
I installed date-fns
and date-fns-tz
and I tried to use multiple functions from these packages but nothing seems to work properly or returns the correct date but again as with 'Z' suffix.
Example where testDate is the Date mentioned before
// using utcToZonedTime from date-fns-tz
console.log(utcToZonedTime(testDate, 'Europe/Berlin'));
// output: 2021-04-07T07:00:00.000Z
// using getTimezoneOffset from date-fns-tz
console.log(getTimezoneOffset('Europe/Berlin', testDate) / 1000 / 60)
// output: 120
// using addMinutes from date-fns to add the Offset manually
console.log(
addMinutes(
testDate,
getTimezoneOffset('Europe/Berlin', testDate) / 1000 / 60,
),
);
// output: 2021-04-07T09:00:00.000Z
Only the last case output the correct Date but still in wrong format. How can I get a normal date in my time zone.