21

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?

enter image description here

Cœur
  • 37,241
  • 25
  • 195
  • 267
Mohammad
  • 1,197
  • 2
  • 13
  • 30

2 Answers2

10

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

Community
  • 1
  • 1
Amr Elgarhy
  • 66,568
  • 69
  • 184
  • 301
  • 1
    Again, as I wrote in another comment: Culture and time zones are not related so fiddling with `CultureInfo` does not fix time zone issues. – Martin Liversage Apr 14 '15 at 13:20
  • 1
    thanks my friend . i put 'System.Globalization.CultureInfo.CurrentCulture.ClearCachedData() ' befor datetime.now and my problem has been resoled :) So thansks – Mohammad Apr 14 '15 at 13:22
  • 2
    I stand corrected. Even though culture and time zones are not related there is a link as can be seen in the [source code for `CultureInfo.ClearCachedData`](http://referencesource.microsoft.com/#mscorlib/system/globalization/cultureinfo.cs,1373): at line 1373 there is a call to `TimeZone.ResetTimeZone()` so resetting the current culture **also** reset the curent time zone information. – Martin Liversage Apr 14 '15 at 13:30
  • your link is so amazing ! so you said isn't it a good solution ? – Mohammad Apr 14 '15 at 13:35
5

Your timezone is UTC +03:30, but because of daylight saving it's +4:30. This means that when DateTime.UtcNow = {15/14/04 12:20:18 PM}, it's 04:50:18 AM in your timezone, which your clock confirms.

So your DateTime.Now doesn't confirm to your machine's datetime settings, making it return a value that doesn't match your clock.

As of why the accepted answer works can be explained by DateTime.Now using the TimeZoneInfo class to calculate the local time from UTC (using timezone and daylight saving), but I don't know why that can run out of sync. Do you never recycle your application pools and never shutdown your machine? ;)

System.Globalization.CultureInfo.CurrentCulture.ClearCachedData() works because it calls TimeZone.ResetTimeZone() and TimeZoneInfo.ClearCachedData().

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • 3
    Culture is not related to time zones. Culture determines how to format date, time and numbers based on culture. Time zones determine the time based on your location on the earth and the same time zone can have many different cultures and vice versa. – Martin Liversage Apr 14 '15 at 13:18
  • @Martin you're totally right. What do you suggest, apart from removing this answer? I've found [DateTime.Now and Culture/Timezone specific](http://stackoverflow.com/questions/20776093/datetime-now-and-culture-timezone-specific) and [C#: Making sure DateTime.Now returns a GMT + 1 time](http://stackoverflow.com/questions/1108644/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". – CodeCaster Apr 14 '15 at 13:21
  • I have no good answer to the question. Perhaps there is some something we do not know but that is very important in understanding the question but the deluge of comments to the questions I do not think I have anything to add. – Martin Liversage Apr 14 '15 at 13:22
  • thanks friends for your help . i ran the first answer and it worked :) – Mohammad Apr 14 '15 at 13:26