Home >Backend Development >C++ >How Do I Parse DateTime Strings with Time Zones like PST, CEST, and UTC?

How Do I Parse DateTime Strings with Time Zones like PST, CEST, and UTC?

Susan Sarandon
Susan SarandonOriginal
2025-01-29 11:06:111015browse

DateTime string containing time zone information (such as PST, CEST, and UTC) string

Question

When trying to analyze the international date time strings including time zone information (such as CEST, PST, UTC, etc.), you may be difficult to find a suitable format string to process the time zone abbreviation.

Solution

Although the standard date time format string element does not include a specific mode of time zone abbreviation, the system recognition time zone offset. By replacing the abbreviation to the corresponding offset, analysis is possible. For example:

In the above code fragment, the original time zone abbreviation "CEST" is replaced with the corresponding offset "2", "02" and "02:00". Format string "Z", "ZZ" and "ZZZ" represent the time zone abbreviation of a letter, the time zone abbreviation of the two letters, and the number of symbol numbers with a optional minute decimal.
<code class="language-csharp">CultureInfo culture = CultureInfo.CreateSpecificCulture("nl-BE");

DateTime dt1 = DateTime.ParseExact("24-okt-08 21:09:06 CEST".Replace("CEST", "+2"), "dd-MMM-yy HH:mm:ss z", culture);
DateTime dt2 = DateTime.ParseExact("24-okt-08 21:09:06 CEST".Replace("CEST", "+02"), "dd-MMM-yy HH:mm:ss zz", culture);
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 Do I Parse DateTime Strings with Time Zones like PST, CEST, and UTC?. 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