Home >Backend Development >C++ >How Can I Parse DateTime Strings Containing Time Zone Abbreviations like CEST, PST, and UTC in C#?
Analysis of the Datetime string containing the time zone
When analyzing the international date time string, you often encounter various formats, such as PST, CEST, UTC, etc. This article introduces how to analyze the date and time string containing such time zone abbreviations.
Try to use the
Date -time string containing CEST abbreviation. However, the available format string provided in the MSDN document does not include a specific representation of the resolution of the parsing time zone.
DateTime.ParseExact
The solution is to replace the abbreviation with the corresponding time zone offset. For example:
By using the corresponding time zone offset, these modifications allow the parsing dates of time string containing PST, CEST and UTC format time zone abbreviations.
<code class="language-csharp">// 将CEST替换为+2(相当于UTC提前2小时) DateTime dt1 = DateTime.ParseExact("24-okt-08 21:09:06 CEST".Replace("CEST", "+2"), "dd-MMM-yy HH:mm:ss z", culture); // 将CEST替换为+02(相当于UTC提前2小时) DateTime dt2 = DateTime.ParseExact("24-okt-08 21:09:06 CEST".Replace("CEST", "+02"), "dd-MMM-yy HH:mm:ss zz", culture); // 将CEST替换为+02:00(相当于UTC提前2小时) DateTime dt3 = DateTime.ParseExact("24-okt-08 21:09:06 CEST".Replace("CEST", "+02:00"), "dd-MMM-yy HH:mm:ss zzz", culture);</code>
The above is the detailed content of How Can I Parse DateTime Strings Containing Time Zone Abbreviations like CEST, PST, and UTC in C#?. For more information, please follow other related articles on the PHP Chinese website!