In. NET Core, use TimeZone to convert the client time to the local time of the server, but the compilation prompt is "expired". coretimezone
When our project is international, we need to deal with time zone issues.
Before. NET Core, we can use the following code to convert the client time to the server time:
DateTime serverTime = TimeZone.CurrentTimeZone.ToLocalTime(clientTime);
In. NET Core, the TimeZone class has been marked as expired. How can we use the non-expired API to convert the time zone?
DateTime serverTime = TimeZoneInfo.ConvertTime(clientTime, TimeZoneInfo.Local);
In the DateTimeExtensions class written by the bloggers themselves, the following is an extension method for converting to the server time:
Public static class DateTimeExtensions {// <summary> // convert the client time to the local server time /// </summary> /// <param name = "clientTime"> client time </param> /// <returns> returns the local time of the server </returns> public static DateTime ToServerLocalTime (this DateTime clientTime) {// DateTime serverTime1 = TimeZone. currentTimeZone. toLocalTime (clientTime); // In.. NET Core mark the format of expired TimeZone class DateTime serverTime2 = TimeZoneInfo. convertTime (clientTime, TimeZoneInfo. local); // return serverTime2 ;}}
For the difference/comparison between the TimeZone and TimeZoneInfo classes, see:
Https://www.cnblogs.com/dongjh/archive/2012/08/30/2664676.html
For the conversion time between different time zones, refer to the MSDN case:
Https://msdn.microsoft.com/zh-cn/library/bb397769.aspx