Paulie22
Paulie22

Reputation: 111

DateTime.Now giving incorrect time

I'm developing an application in Visual Studio using C#. When I create a new instance of an object I want to attach the current date and time to it. Using DateTime.Now the time is off by one hour.

I presume its a time-zone or daylight saving issue. I tried calling System.Globalization.CultureInfo.CurrentCulture.ClearCachedData() right before DateTime.Now but got the same result. How do I resolve this?

Upvotes: 8

Views: 27579

Answers (1)

gbellmann
gbellmann

Reputation: 1951

You should use DateTime.UtcNow instead of DateTime.Now to avoid time zone issues (DateTime.Now uses the time zone of the server), then you can convert the UTC value to the user's time zone.

Upvotes: 12

Related Questions