ayilmaz ayilmaz - 1 month ago 5
C# Question

Convert timezone specific datetime string to datetime in C#

How can I convert

Tue, 01 Nov 2016 02:00 PM EET
datetime string to
DateTime
in C#? What is a good practice to do it?

Answer

By replacing timezone abbreviation with zone offset you can convert using DateTime.ParseExact

string date = "Tue, 01 Nov 2016 02:00 PM EET";
DateTime dt = DateTime.ParseExact(date.Replace("EET", "+2"), "ddd, dd MMM yyyy hh:mm tt z", CultureInfo.InvariantCulture);

and if you want more safer way by checking exception then you can using DateTime.TryParseExact method

Comments