1

First of all I am new in C#.

I have to convert a string like

  "Fri, 30 Jul 2021 11:57:58 (UTC)" 

into a DateTime. I've tried serveral format strings (like "ddd, dd MMM yyyy HH:mm:ss", "r", "R"). But I always get the error message

String was not recognized as a valid DateTime.

Here is my last code for this:

CultureInfo enUS = new CultureInfo("en-US");
string timeStampFormat = "ddd, dd MMM yyyy HH:mm:ss";
DateTime myDateTime;

myDateTime = DateTime.ParseExact(
  stringToFormat, 
  timeStampFormat, 
  enUS, 
  DateTimeStyles.AssumeUniversal);

Thanks for your support.

Best regards Andreas## Heading ##

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
Andreas
  • 23
  • 2
  • Seems to be similar/partial dup of: https://stackoverflow.com/questions/1756639/why-cant-datetime-parse-parse-utc-date. Replace " (UTC)" with "Z" and try again. – Bryan Lewis Aug 02 '21 at 18:14
  • 1
    Does this answer your question? [Parse DateTime with time zone of form PST/CEST/UTC/etc](https://stackoverflow.com/questions/241789/parse-datetime-with-time-zone-of-form-pst-cest-utc-etc) – Charlieface Aug 02 '21 at 19:46

2 Answers2

1

It is the (UTC) which is causing the string to not be recognized as a timestamp. You could account for the (UTC) by removing it, I have provided one method to accomplish this here.

        string stringToFormat = "Fri, 30 Jul 2021 11:57:58 (UTC)";

        string[] SubString = stringToFormat.Split('(');
        stringToFormat = SubString[0].Trim();

        string timeStampFormat = "ddd, dd MMM yyyy HH:mm:ss";
        DateTime mydateTime = DateTime.ParseExact(stringToFormat, timeStampFormat, CultureInfo.GetCultureInfo("en-US"), DateTimeStyles.AssumeUniversal);
        

Or replace the (UTC) with the offset from UTC, which in this case would be 0, and add (z) to timeStampFormat.

        string stringToFormat = "Fri, 30 Jul 2021 11:57:58 (UTC)";
        stringToFormat = stringToFormat.Replace("(UTC)", "(-0)");

        string timeStampFormat = "ddd, dd MMM yyyy HH:mm:ss (z)";
        DateTime mydateTime = DateTime.ParseExact(stringToFormat, timeStampFormat, CultureInfo.GetCultureInfo("en-US"), DateTimeStyles.AssumeUniversal);
        

https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings

Moir
  • 379
  • 4
  • 14
1

Assuming that you can have not only (UTC), but (UTC+4), (UTC-5) and alike suffixes, I suggest escaping (UTC and ):

  string stringToFormat = "Fri, 30 Jul 2021 11:57:58 (UTC)";

  ...

  DateTime myDateTime = DateTime.ParseExact(
    stringToFormat, 
    new string[] { 
      "ddd, d MMM yyyy H:m:s '(UTC)'",
      "ddd, d MMM yyyy H:m:s '(UTC'z')'",
    },
    CultureInfo.GetCultureInfo("en-US"),
    DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeUniversal);

Demo:

  DateTime demo(string text) => DateTime.ParseExact(
    text, 
    new string[] { 
      "ddd, d MMM yyyy H:m:s '(UTC)'",
      "ddd, d MMM yyyy H:m:s '(UTC'z')'",
    },
    CultureInfo.GetCultureInfo("en-US"),
    DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeUniversal);

  string[] tests = new string[] {
    "Fri, 30 Jul 2021 11:57:58 (UTC)",
    "Fri, 30 Jul 2021 11:57:58 (UTC-1)",
    "Fri, 30 Jul 2021 11:57:58 (UTC+1)",
    "Fri, 30 Jul 2021 11:57:58 (UTC-14)",
  };

  string report = string.Join(Environment.NewLine, tests
    .Select(test => $"{test,-40} => {demo(test):dd.MM.yyyy HH:mm:ss}"));

  Console.Write(report);

Outcome:

Fri, 30 Jul 2021 11:57:58 (UTC)          => 30.07.2021 11:57:58
Fri, 30 Jul 2021 11:57:58 (UTC-1)        => 30.07.2021 12:57:58
Fri, 30 Jul 2021 11:57:58 (UTC+1)        => 30.07.2021 10:57:58
Fri, 30 Jul 2021 11:57:58 (UTC-14)       => 31.07.2021 01:57:58
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215