Search code examples
c#date-parsing

How to parse a string with GMT into datetime in c#


I have this inputDateString: 15/03/2023 15:13:37 GMT and I want to convert it to datetime.

I am doing this: DateTime outputDate = DateTime.ParseExact(inputDateString, "MM/dd/yyyy:hhmmss \"GMT\"", CultureInfo.InvariantCulture);

I get this error: c# String '10/05/2023 18:19:27 GMT' was not recognized as a valid DateTime.

How can I convert this date string to a datetime?


Solution

  • your format date incorrect ,This code is correct,better but you must specify the timezone

    DateTime outputDate = DateTime.ParseExact("15/03/2023 15:13:37 GMT", "dd/MM/yyyy HH:mm:ss GMT", CultureInfo.InvariantCulture);