'c# parse date time with timezone

How can we parse date time with time zone.

<TIMESTAMP_UTC>20180523160000</TIMESTAMP_UTC>
<TIMEZONE>UTC+8</TIMEZONE>

this should convert as in 2018-05-24 00:00:00. I tried couple of things but could not succeed. I tried the below command but it throws an error.

DateTime.ParseExact("20180523160000+08:00", "yyyyMMddHHmmssZhhmm", System.Globalization.CultureInfo.InvariantCulture)

Do you know how we can parse this with DateTime Parse methods.



Solution 1:[1]

You need to use DateTimeOffset.ParseExact

var date = DateTimeOffset.ParseExact("20180523160000+08:00", "yyyyMMddHHmmsszzz", CultureInfo.InvariantCulture);

Solution 2:[2]

Try using DateTimeOffset.Parse() instead of DateTime.Parse() as DateTimeOffset stores timezone info.

You can refer to the following MSDN link for more details:

https://msdn.microsoft.com/en-us/library/bb351654(v=vs.110).aspx

Solution 3:[3]

You could invert the sign of your timezone and then parse it with

var dateTime = DateTime.ParseExact(
    "20180523160000UTC-8",
    "yyyyMMddHHmmssUTCz",
    CultureInfo.InvariantCulture);

But if this is a good approach is probably questionable.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 davmos
Solution 2 Rudresha Parameshappa
Solution 3