1

I have a date string in the following format:

var dateString = Fri Jun 26 2020 00:00:00 GMT+0100 (British Summer Time)

How can I convert this to a DateTime in C# such as 26/06/2020 00:00:00

I have tried:

  DateTime.Parse(dateString);

And: DateTime.ParseExact(dateString);

And I get:

 System.FormatException: 'String was not recognized as a valid DateTime.'
mimumimu
  • 23
  • 3
  • 1
    Note that a `DateTime` does not have a format. It is a `struct` with fields for Year, Month, Day, etc. – Heretic Monkey Jun 22 '20 at 11:55
  • 1
    Does this answer your question? [Converting a String to DateTime](https://stackoverflow.com/questions/919244/converting-a-string-to-datetime) – Heretic Monkey Jun 22 '20 at 11:56
  • You need to use DateTime.ParseExact with the format you have. You could read more about formats in the link provided in the comments to answer – Roman Kalinchuk Jun 22 '20 at 12:01
  • No, I get: 'String was not recognized as a valid DateTime.' – mimumimu Jun 22 '20 at 12:02
  • You can try this format: `"ddd MMM dd yyyy HH:mm:ss 'GMT'K '(British Summer Time)'"` Unfortunately there is no format specifier for using the thing in the parenthesis to detect time zone or similar, so you can't generalize this to handle `(... whatever ...)` – Lasse V. Karlsen Jun 22 '20 at 12:09
  • Read those answers again. The highest-voted answer says to use `ParseExact`, not `Parse`. And you must provide a format that matches the format you are using. The format strings are easily searched for on learn.microsoft.com, but I happen to have them bookmarked: https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings – Heretic Monkey Jun 22 '20 at 12:12

3 Answers3

1

You can accomplish this by using DateTime.ParseExact and providing a custom date time format. However, this will only work if you first modify the input string to be able to fit the custom date and time format strings that are included in .net.

CultureInfo provider = CultureInfo.InvariantCulture;

var input = "Fri Jun 26 2020 00:00:00 GMT+0100 (British Summer Time)";

// set up a regex that will match the text starting with GMT, and extract just the timezone offset 
// (the description of the timezone is irrelevant here)
var r = new Regex(@"GMT([+-]\d\d\d\d) \([\w\s]*\)");

// this will remove the extra text: "Fri Jun 26 2020 00:00:00 +0100"
// now we can match it in our format string
var s = r.Replace(input, "$1");

var f = "ddd MMM dd yyyy hh:mm:ss zzz"; // matches the s variable
var d = DateTime.ParseExact(s, f, provider); // you now have parsed your date

This will include the timezone offset in the DateTime object. If you just want it to be set to "26/06/2020 00:00:00" and to ignore the datetime offset, then just change the regex replace above to replace with String.Empty instead of $1.

Simona
  • 279
  • 1
  • 8
  • I think this is almost there. My date shows as Wed Jun 24 2020 00:00:00+0100 but still a format exception – mimumimu Jun 22 '20 at 12:45
  • That is a different format than what you had in the question. To handle that, the `GMT` needs to be removed from the regex and the end of the format string should be `hh:mm:sszzz` (since you lost the space there). – Simona Jun 22 '20 at 12:48
  • This is the format after running it through the regex you provided – mimumimu Jun 22 '20 at 12:58
  • Try it now please – Simona Jun 22 '20 at 13:11
0

This will solve your problem.

var dateString = "Fri Jun 26 2020 00:00:00 GMT + 0100(British Summer Time)"; Console.WriteLine(DateTime.Parse(dateString.Substring(4, 11)));

-2

Hello so what you can do is you can take advantage of "datetime" class and just write this:

 DateTime.Now.ToString("MM/dd/yyyy HH:mm");

edit: sorry i forgot to supply the link haha https://www.c-sharpcorner.com/blogs/date-and-time-format-in-c-sharp-programming1