1

I am working on a program that gets a list of file names from a directory, then parses the file names into individual variables (strings) and converts it into something that can be submitted to a mysql database that will the later be queued for searches.

In those file names contain a 4 digit year, 2 digit day, 2 digit month, 2 digit hour, and 4 digit minute (##AM/PM), followed by 2 numbers that can be between 3 and 11 digits.

I have parsed the filename and formatted the date and time info into the following string: YYYY/DD/MM HH:MMAM or YYYY/DD/MM HH:MMPM (only "AM" and "PM" changes on minutes).

EX: 2014/24/12 02:50PM

How can I convert the string into DateTime to submit into a MySql database.

2 Answers 2

2

The built-in STR_TO_DATE() function does what you need.

STR_TO_DATE('2014/24/12 02:50PM', '%Y/%d/%m %h:%i%p') 

gives the result you need. You can look up the format codes here.

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

Comments

1

Using DateTime.ParseExact will do the trick to get it into .net's DateTime object:

        string s = "2014/24/12 02:50PM";
        DateTime dt;
        DateTime.TryParseExact(s, "yyyy/dd/MM hh:mmtt", new CultureInfo("en-US"), DateTimeStyles.None, out dt) ;
        Console.WriteLine(dt.ToString());

2 Comments

when I submit the dt to the database, it submits as: 0000-00-00 00:00:00
What is the date format your mySQL database accepts? You can convert it as needed in the ToString method's parameters, ex: dt.ToString("yyyy-MM-dd HH:mm")

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.