jstuardo
jstuardo

Reputation: 4383

What is explanation of this time difference?

I have developed a Windows Form application in C#. That application shows the date and time.

To get the date and time I am using this property:

DateTime.Now

However, the time gotten in the application is 1 hour ahead of the time displayed in the Windows taskbar.

Please look at this screenshot:

Time shown in application versus time shown in Windows

You can see the time difference there.

The real time is the time shown by the application, that is, 08:38.

I know that I have problems with the time in Windows. Maybe something happened after an electricity blackout, but I am curious about that difference. I can change TimeZone settings so that Windows can show the right time, however, if I do that, always the application shows 1 hour less.

Any advice, please?

Upvotes: 0

Views: 182

Answers (3)

Simon
Simon

Reputation: 1737

Try using

DateTimeOffset.Now

From the MS docs: You can also use the DateTimeOffset.Now property to retrieve the current local date and time. It allows a local time to be expressed unambiguously as a single point in time, which in turn makes that time value portable across computers.

Upvotes: 0

al2
al2

Reputation: 159

I have the same problem, but in my case it is because the dual boot:

https://help.ubuntu.com/community/UbuntuTime#Multiple_Boot_Systems_Time_Conflicts

Upvotes: 0

Banks
Banks

Reputation: 175

This is probably because of daylight savings, your current time zone is probably cached. A few recommendations:

Try restating the system or restarting the application.

If it still doesn't work call System.Globalization.CultureInfo.ClearCachedData() to reset the cached value. After this you should get the correct time.

Your clock went backward on Sunday, 7 April 2019, 12:00:00 midnight (Chile), that is why the one hour lag.

Upvotes: 1

Related Questions