How is local determined in ToLocalTime()

前端 未结 4 609
天命终不由人
天命终不由人 2021-02-02 09:29

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.

相关标签:
4条回答
  • 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.
    0 讨论(0)
  • 2021-02-02 10:00

    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.

    0 讨论(0)
  • 2021-02-02 10:02

    It is the local time on the server.

    0 讨论(0)
  • 2021-02-02 10:11

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

    0 讨论(0)
提交回复
热议问题