We use DateTime.Now
, but the time is not equal with our server time!
When I run my project, these are the DateTime
property values:
DateTime.Now = {15/14/04 05:20:18 AM}
DateTime.UtcNow = {15/14/04 12:20:18 PM}
But my current local system time is:
15/14/04 04:50:18 AM
My time zone is Tehran (UTC+03:30).
This is the first time I've see this behavior! Why doesn't DateTime.Now
equal my computer's time?
The property Now of the DateTime class returns the current date and time of the machine running the code, expressed in the computer's local time. The property UtcNow of the DateTime class returns the current date and time of the machine running the code, expressed in UTC format.
The Now property returns a DateTime value that represents the current date and time on the local computer.
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".
If Server sends Date in UTC format it is automatically default to local time. That is DateTime.
Make sure you are not manipulating the timezone somewhere in code, or playing with System.Globalization.CultureInfo
Try to search in all your source code for System.Globalization.CultureInfo may be it is somewhere, also may be the time zone is cached somewhere so try to call System.Globalization.CultureInfo.CurrentCulture.ClearCachedData()
before DateTime.Now
.NET DateTime.Now returns incorrect time when time zone is changed
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