Home >Backend Development >C++ >How to Accurately Parse DateTime Strings with Time Zone Abbreviations in C#?

How to Accurately Parse DateTime Strings with Time Zone Abbreviations in C#?

DDD
DDDOriginal
2025-01-29 11:16:111048browse

How to Accurately Parse DateTime Strings with Time Zone Abbreviations in C#?

Analyze the datetime

When trying to analyze the international date time strings including time zone abbreviations (such as "PST" or "UTC"),

may be difficult to identify abbreviations. To overcome this problem, consider replacing the abbreviation to the corresponding offset.

DateTime.ParseExact Using , you can specify regional, which will affect the analysis of the date and time value. For example:

CultureInfo Format string is critical to correctly analyze the date of time. The "custom date and time format string" document does not clearly mention the format description symbol in the time zone in the form of "PST/CEST/GMT/UTC".

<code class="language-csharp">CultureInfo culture = CultureInfo.CreateSpecificCulture("nl-BE");</code>
You can use the time zone to replace the abbreviation:

or, you can use the following format string:

<code class="language-csharp">DateTime dt1 = DateTime.ParseExact("24-okt-08 21:09:06 CEST".Replace("CEST", "+2"), "dd-MMM-yy HH:mm:ss z", culture);</code>

"ZZ" for two -digit offset:
  • <code class="language-csharp">  DateTime dt2 = DateTime.ParseExact("24-okt-08 21:09:06 CEST".Replace("CEST", "+02"), "dd-MMM-yy HH:mm:ss zz", culture);</code>
    "ZZZ" for three -digit offset:

The above is the detailed content of How to Accurately Parse DateTime Strings with Time Zone Abbreviations in C#?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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