0

I have here a little problem and would like to know where is my mistake and how to correct it.

string preConvDATE = monthCombobox2.Text + " " + datecombobox2.Text + ","+ "0000";
//lets say the comboboxes contain something like this "January 1";

DateTime DT = DateTime.ParseExact(preConvDATE, "MMMM d, yyyy 00:00:00", System.Globalization.CultureInfo.InvariantCulture);
string strDate = DT.ToString("yyyy-mm-dd");
    try
    {
       //code that inserts strDate to a column in Mysql DB
    }
    catch
    {
      //msg
    }

why "0000" for the year? because i was really planning to store just the month and date, but realized i could just store it as a datetime format with a year, and then at viewing the table i'd just use the Mysql MONTH() and DATE() function concatenated to view the Month and Date i stored from the monthCombobox2 and datecombobox2.

I also tried:

Convert.ToDateTime()

But still won't work.

How do i properly parse MMMM d,yyyy date format so that i could convert it to yyyy-mm-dd again and store it as datetime format in the database? Thank you so much :)

8
  • Why do you need to format it as a string at all to insert it into MySql? You are using parameterized SQL queries, right? (You should be) Commented Mar 16, 2019 at 5:10
  • Your main mistake is that you're building a string from user selections (when you could just construct a DateTime object using its constructor), and then trying to parse that string, and reformat it as another string. This is a very strange thing to do. Commented Mar 16, 2019 at 5:12
  • new DateTime(year, month, day)? Commented Mar 16, 2019 at 5:13
  • yes, im trying to store date events on the database. Just the Month and Day. Commented Mar 16, 2019 at 5:13
  • im really sorry, i actually dont know what im doing, im new to programming, and is trying to learn on my own, any correction is highly appreciated. Commented Mar 16, 2019 at 5:17

1 Answer 1

2

As I can see, you are using text boxes to catch the value of month and date, instead of that you can use dropdowns which will display the month name but return the values like 01 for January and so on. It will be easy for you to format date formats. You don't want to save year that's okay, instead of setting 0000 you can set the current year. Now the problem with that you will not able to figure out the what exactly day was it (e.g Sunday, Monday and so on).

string preConvDATE = monthCombobox2.Text + "/" + datecombobox2.Text+"/" + DateTime.Now.Year.ToString();
DateTime DT = DateTime.ParseExact(preConvDATE, "MM/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture);

string strDate = DT.ToString("yyyy-MM-dd");

I hope this will solve your problem. Happy Coding!

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

1 Comment

yeah that's what i did :), and i did not parse the text anymore i just used Mysql Monthname() function to view the month number as month name. thank you for taking ur time to answer my qustion :)

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.