Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is local determined in ToLocalTime()

When using ToLocalTime(), how is local time determined? Is local time from the server or the client? My assumption would be the server running the application.

like image 835
drobison Avatar asked May 10 '13 20:05

drobison


People also ask

How does ToLocalTime work?

The ToLocalTime method converts a DateTime value from UTC to local time. To convert the time in any designated time zone to local time, use the TimeZoneInfo. ConvertTime method. The value returned by the conversion is a DateTime whose Kind property always returns Local.

How to convert server time to local time in c#?

You can get the time off the server and do this. DateTime myTimeGMT = ServerTime. ToUniversalTime();

How do I convert DateTimeOffset to local time?

In performing the conversion to local time, the method first converts the current DateTimeOffset object's date and time to Coordinated Universal Time (UTC) by subtracting the offset from the time. It then converts the UTC date and time to local time by adding the local time zone offset.

How do you convert UTC time to local time?

Add the local time offset to the UTC time. For example, if your local time offset is -5:00, and if the UTC time is shown as 11:00, add -5 to 11. The time setting when adjusted for offset is 06:00 (6:00 A.M.).


4 Answers

It is the local time zone of the computer that code is running on. In an ASP.Net application, the code runs on the server - so that's the time zone that it will return.

The behavior of this function is actually dependent on the .Kind property of the source value. From the MSDN link you gave:

  • Utc - This instance of DateTime is converted to local time.
  • Local - No conversion is performed.
  • Unspecified -This instance of DateTime is assumed to be a UTC time, and the conversion is performed as if Kind were Utc.

This is non-obvious behavior. You can read other related problems with the .net DateTime class here and here.

A few other points:

  • If you follow best practices, you will set the server's time zone to UTC.
  • If you are trying to display time in the user's timezone, you'll have to use one of these strategies.
like image 147
Matt Johnson-Pint Avatar answered Oct 27 '22 13:10

Matt Johnson-Pint


ToLocalTime(), in this case, executes on the server. Therefore the time is evaluated on the server, and it'll return the server time to the client.

like image 30
KenD Avatar answered Oct 27 '22 13:10

KenD


It is the local time on the server.

like image 27
Dave Zych Avatar answered Oct 27 '22 13:10

Dave Zych


Local here is the timezone of the machine that the function executes on.

like image 35
500 - Internal Server Error Avatar answered Oct 27 '22 13:10

500 - Internal Server Error