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?
Now has an approximate resolution of 10 milliseconds on all NT operating systems. The actual precision is hardware dependent.
Now and Culture/Timezone specific and C#: Making sure DateTime. Now returns a GMT + 1 time which seem relevant, but no pointers as to the cause of "DateTime. Now not corresponding to machine settings".
DateTime values lack any knowledge of time zone, or lack thereof. If you need to know when things actually occurred, with more precision than just the approximate date, and you can't be 100% sure that your dates are ALWAYS stored in UTC, then you should consider using DateTimeOffset to represent your datetime values.
UtcNow tells you the date and time as it would be in Coordinated Universal Time, which is also called the Greenwich Mean Time time zone - basically like it would be if you were in London England, but not during the summer. DateTime. Now gives the date and time as it would appear to someone in your current locale.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With