.NET DateTime.Now returns incorrect time when time zone is changed

前端 未结 4 1143
抹茶落季
抹茶落季 2020-11-29 02:39

This problem occurred during daylight saving time change. After the change occurred, we\'ve noticed that our server application started writing into the log incorrect time -

4条回答
  •  一个人的身影
    2020-11-29 03:09

    The fully qualified class above was slightly off, but perhaps it changed in .NET 3.5.

    System.Globalization.CultureInfo.CurrentCulture.ClearCachedData()
    

    also don't forget to include (in C#.NET) or import (with VB.NET) the library reference System.Globalization.CultureInfo

    Call it just before using DateTime.Now. Although it's probably best to call it in the startup event of your Global.asax file.

    ========== Also make sure you're checking the timezone on Windows Server itself, or your local machine depending on where the IIS web server is running.

提交回复
热议问题