Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DateTime format for 9/14/2016 19:31

I am doing an F# course by Richard Broida. In one lecture I am supposed to parse a CSV document. Everything works fine except for parsing datetime. My local culture settings are Slovak (sk-SK) and I am trying to parse the following datetime string:

9/14/2016 19:31

I have read through the CSV file and found out that months, days and hours can all have both one or two digits. Years always have 4 digits and minutes always have 2 digits. So, the datetime 10/7/2016 9:01 is also a valid one.

I have created an adapter function:

let dateTimeParseAdapter format provider date = 
    DateTime.ParseExact(date, format, provider)

and then called

dateTimeParseAdapter "M/d/yyyy h:mm" CultureInfo.InvariantCulture "9/14/2016 19:31"

but I got an exception message saying

System.FormatException: String was not recognized as a valid DateTime.

I tried adjusting the format string to exactly match the number of digits in my single date. However, even the following code resulted in the same error message.

dateTimeParseAdapter "M/dd/yyyy hh:mm" CultureInfo.InvariantCulture "9/14/2016 19:31"

Why am I getting the exception message? Should I change the CultureInfo settings? Or is there a pre-defined datetime format to parse my datetimes?

like image 634
Storm Avatar asked Sep 06 '25 08:09

Storm


1 Answers

When working with 24 hour format you should use H (HH) not h hh (which is for 12 hour format) pattern:

 format = @"M/d/yyyy H:mm";
like image 91
Dmitry Bychenko Avatar answered Sep 09 '25 17:09

Dmitry Bychenko