Home > Back-end >  Date was not recognized as a valid DateTime
Date was not recognized as a valid DateTime

Time:01-03

kinda got an issue that I cant solve right now.

I've got a discord bot running on my raspberry pi, which has a system for automated messages that are sent after a certain amount of time, or an exact date, has passed.

My code works on Windows when debugging there, but the console throws a warning on Linux when running the published project.

The date is taken from a table in my MySQL database and put into a DataTable. The code that grabs the date from the DataRow is:

DateTime datetime = DateTime.ParseExact(row["datetime"].ToString(), "dd.MM.yyyy HH:mm:ss", CultureInfo.InvariantCulture);

Why is it happening? No matter how I format the string (dots, dashes or slashes), the warning persists. The messages are not sent.

I even tried removing invisible whitespaces with regex, doesnt work either. (The regex in question, though I scrapped it since it yielded no fruit anyways)

Regex.Replace($"{row["datetime"].ToString()}", @"[^\d\s\.:]", string.Empty);

Warning in Raspberry Console

CodePudding user response:

If RDBMS type is DateTime then why should we convert to string and then parse it back to DateTime? Let's do it direct:

 DateTime datetime = Convert.ToDateTime(row["datetime"]);

and let .net convert boxed DateTime (row["datetime"] is of type object) to DateTime

CodePudding user response:

There are a couple issues--at the highest level, your ParseExact method is encountering a Date Time string that does not match the supplied format.

According to the code you posted, the expected format of is dd.MM.yyyy HH:mm:ss, and in your exception exception, shows a Date time string (8/2/2021 2:00:00 PM) that does not match:

  • contains / and your expected format has .
  • dd is a 2-digit day, but the input date time string only has single digit days
  • MM expects a two digit month and the input date time only has a single digit month
  • the string contains AM/PM, and your format neglects to account for that.

Finally it's not clear if your date format is Month Day Year, or Day Month year.

The second issue, is that ParseExact should be enclosed in a try/catch block, so that your code can handle the case when an unexpected formatted date time string is passed in, and not crash.

To solve this, wrap your call into a try/catch, and gracefully handle the FormatException

And then make sure the Format string matches the expected input string.

Here is the .NET reference for the various DateTime format tokens

CodePudding user response:

The error message is letting you know the issue.

You have :

DateTime datetime = DateTime.ParseExact(row["datetime"].ToString(), "dd.MM.yyyy HH:mm:ss", CultureInfo.InvariantCulture);

Notable, you are saying that the date format is going to be "dd.MM.yyyy HH:mm:ss"

Then your error message is saying that you couldn't parse :

8/2/2021 2:00:00 PM

Which is essentially a format of "d/M/yyyy h:mm:ss tt" (Assuming that days come before months).

If you change your code to :

DateTime datetime = DateTime.ParseExact(row["datetime"].ToString(), "d/M/yyyy h:mm:ss tt", CultureInfo.InvariantCulture);

You should be good to go. DateTime.ParseExact does what it says on the tin, it parses the date format exactly how you say it should come. If you aren't sure you can use DateTime.Parse() (But you can occassionally run into issues where days/months are around the wrong way).

  • Related