Home > Blockchain >  addMonths fns is not returning correct UTC time
addMonths fns is not returning correct UTC time

Time:03-06

I'm currently based in Central European Standard Time and I'm expecting to send all dates to the server converted back to UTC.

I have tried simply using addMonths from date-fns library:

import addMonths from "date-fns/addMonths";

const now = new Date();
// Sat Mar 05 2022 13:56:04 GMT 0100 (Central European Standard Time)
console.log(now);
const oneMonthFromNow = addMonths(now, 1);
// 2022-04-05T11:56:04.189Z
console.log(oneMonthFromNow.toISOString());

The difference between CEST and UTC is simply one hour. I'm expecting to have an hour and one month difference between now and oneMonthFromNOw but it ends up having one month and two hours difference.

What I would expect:

import addMonths from "date-fns/addMonths";

const now = new Date();
// Sat Mar 05 2022 13:56:04 GMT 0100 (Central European Standard Time)
console.log(now);
const oneMonthFromNow = addMonths(now, 1);
// Since CEST time is 13:56:04 I would expect to get 12:56:04 (1 hour difference only)
// But I end up getting 2022-04-05T12:56:04.189Z
console.log(oneMonthFromNow.toISOString());

Am I missunderstanding something here? Shouldn't I get one month and one hour difference between now and oneMonthFromNow?

CodePudding user response:

The difference between CEST and UTC is simply one hour.


The difference is two hours: Central European Summer Time (CEST, UTC 02:00).


You went from CET time (one hour ahead) on Mar 05 2022 to CEST summer time (two hours ahead) on 2022-04-05.


Central European Time (CET) is a standard time which is 1 hour ahead of Coordinated Universal Time (UTC).

As of 2011, all member states of the European Union observe summer time (daylight saving time), from the last Sunday in March to the last Sunday in October. States within the CET area switch to Central European Summer Time (CEST, UTC 02:00) for the summer.

Wikipedia: Central European Time

Wikipedia: Central European Summer Time

  • Related