Home > Backend Development > C++ > How Can I Parse DateTime Strings Containing Time Zone Abbreviations like CEST, PST, and UTC in C#?

How Can I Parse DateTime Strings Containing Time Zone Abbreviations like CEST, PST, and UTC in C#?

Susan Sarandon
Release: 2025-01-29 11:11:08
Original
137 people have browsed it

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>
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template