Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why DateTime.Now and DateTime.UtcNow Isn't My Current Local DateTime?

Tags:

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

like image 794
Mohammad Avatar asked Apr 14 '15 12:04

Mohammad


People also ask

What is difference between DateTime UtcNow and DateTime now?

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.

Does DateTime now return local time?

The Now property returns a DateTime value that represents the current date and time on the local computer.

What timezone does DateTime now return?

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".

Is DateTime UTC by default?

If Server sends Date in UTC format it is automatically default to local time. That is DateTime.


1 Answers

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

like image 163
Amr Elgarhy Avatar answered Sep 20 '22 06:09

Amr Elgarhy