In. NET Core, use TimeZone to convert the client time to the local time of the server, but the compilation prompt is "expired". coretimezone

Source: Internet
Author: User
Tags time zones

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.