BENN1TH BENN1TH - 1 month ago 10
C# Question

Convert string to dd/mm/yyyy in C# WPF

I have a value (as a date) that gets returned from the selected combobox item. I wish to convert this value to a proper DateTime string. Problem is the string from the combobox.selectedItem to convert is;

July 2016

and I want to convert it to (start of month always);

01/07/2016 12:00AM

Is there away of doing the in C#?

As the only way around it is to do I can think of;

if(combobox.selectedItem.Contains("July") && combobox.selectedItem.Contains("2016")) {

//startDate = Set date to 01/07/2016..
endDate = DateTime.Now().ToString();


which is not ideal at all...especially if I want do past 24+ months (as combobox is populated with each month between two dates from an xml file)

Bob Bob
DateTime dt;
if (DateTime.TryParseExact("July 2016", "MMMM yyyy", null, DateTimeStyles.None, out dt))
    // Parse success
    // parse failed

Checkout DateTime.TryParseExact() and the formats


If the date have white space, either use string.Trim() on it or change DateTimeStyles.None to DateTimeStyles.AllowWhiteSpaces