Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

(.net) Unexpected offest when call DateTime.Today.AddDays

I have a test that started to fail today,
after a little investigation I found that the returned date differ by 1 hour, more precisely it results to have 1 hour offset (the current offset I have in my location now [correction: today offset is still 00:00]).
The test was fine yesterday. The same operation returned a date without that extra offset.

Today is 26 March 2022.
Tonight the offset time change in my location,
At midnight we move the time 1 hour forward. ([correction] the change is done at 02:00 moving time to 03:00)
The date that result wrong is the date obtained by DateTime.Today.AddDays(2).
Note that if you add 1 day it seems ok.

let today = DateTime.Today
let tomorrow = DateTime.Today.AddDays(1.)
let nextDay = DateTime.Today.AddDays(2.)

Console.WriteLine($"today:    {today:o} {today.Kind}")
Console.WriteLine($"tomorrow: {tomorrow:o} {tomorrow.Kind}")
Console.WriteLine($"nextDay:  {nextDay:o} {nextDay.Kind}")

Output:
enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The nextDay date seems wrong to me. Why its offset is 01:00 ?
Because it is adding 48 hours and because of the time-change it also shows the "new" local offset?.
So, why it does not happen when I add 2 day ?
27 March (tomorrow) is already with 1 hour offset.
I’m missing something or that date is wrong as I think?

>Solution :

27 March (tomorrow) is already with 1 hour offset.

Not exactly. In most of Europe (including the UK, which seems to be your current location) daylight savings time starts at 27 March 01:00 UTC. At 27 March 00:00, you still have "winter time" in the UK with offset +00:00.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading