1

I have a string in this format:

Tuesday, Sep 01, 2020 04:15
Thursday, Aug 27, 2020 03:56
Friday, Aug 28, 2020 07:30
Tuesday, Aug 04, 2020 08:00
[Route("/test")]
public async void TestParseTime()
{
    string pattern = "dddd, MMM dd, YYYY HH:MM";
    string newsTimeString = "Thursday, Aug 27, 2020 03:56";
    DateTime newsTime = DateTime.ParseExact(newsTimeString, pattern, null);
    Console.WriteLine("newsTime = " + newsTime);
}

But the parsing of the string to datetime fails with the following exception:

enter image description here

How to convert it to a DateTime in C#?

10
  • 2
    Are you sure you mean HH:MM (considering uppercase M is month)? Also, if "Aug 1" is possible, you should use MMM d instead of MMM dd as that will cover "Aug 27" and "Aug 1". Commented Sep 1, 2020 at 8:13
  • I tend minute... Commented Sep 1, 2020 at 8:14
  • 3
    "HH:mm" not "HH:MM", John is absolutely correct. Commented Sep 1, 2020 at 8:16
  • Yes, capital "MM" at the end of the format string you might have used for minutes but "MM" is for the month and lower case "mm" is for minutes. So you need to use string pattern = "dddd, MMM dd, YYYY HH:mm"; hope it should sovle the problem Commented Sep 1, 2020 at 8:18
  • 1
    Also small "y"s : dotnetfiddle.net/fapKUz Small tipp: If I run into Date-Parsing problems, I have a date be formatted by the pattern. That's quite quick to reveal some mishap. Commented Sep 1, 2020 at 8:20

2 Answers 2

4

I would suggest to use the powerfull Convert.ToDateTime method and simply feed the input in there:

string newsTimeString = "Thursday, Aug 27, 2020 03:56";
DateTime newsTime = Convert.ToDateTime(newsTimeString);
Console.WriteLine("newsTime = " + newsTime);

Output:

newsTime = 27.08.2020 03:56:00

Actually the "Thursday" string is superfluous information for the DateTime object. Because it will know the day of week from the calendar. You can print it via the property:

Console.WriteLine("Day = " + newsTime.DayOfWeek);

Output:

Day = Thursday

EDIT:

Apparently: "Convert.ToDateTime is just a call to DateTime.Parse" (comment by Panagiotis Kanavos) So in this case you can also use:

DateTime newsTime = DateTime.Parse(newsTimeString);

and get the same result, without specifying any format.

Disclaimer:

From the Remarks section of the documentation:

"If value is not null, the return value is the result of invoking the
DateTime.Parse method on value using the formatting information in a
DateTimeFormatInfo object that is initialized for the current culture." -

Sign up to request clarification or add additional context in comments.

3 Comments

Convert.ToDateTime is just a call to DateTime.Parse.
"If value is not null, the return value is the result of invoking the DateTime.Parse method on value using the formatting information in a DateTimeFormatInfo object that is initialized for the current culture." - emphasis by me. Just one thing to be aware of, when using Convert.
@Fildor incorporated this also, thank you very much
0

Your pattern is incorrect. patterns are case-sensitive. Use this pattern instead:

dddd, MMM dd, yyyy hh:mm

string pattern = "dddd, MMM dd, yyyy hh:mm";
Console.WriteLine(DateTime.Now.ToString(pattern));

string newsTimeString = "Thursday, Aug 27, 2020 03:56";
DateTime newsTime = DateTime.ParseExact(newsTimeString, pattern, null);
Console.WriteLine("newsTime = " + newsTime);

6 Comments

It's 24-Hour, though "HH".
Well, his sample code uses 03:56 which you cant tell if AM or PM. also, MM does not work
You should add a CultureInfo in ParseExact, as this code will currently fail on systems with a non-english default culture.
Even on english systems I am pretty confident in saying that a missing "AM/PM" hints strongly towards 24-Hour format, even if the examples do not contain one with hour > 12.
@Fildor: Yes, that should be specified. But on my (german) PC I would get an exception because of the Thursday in the input.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.