BENN1TH
BENN1TH

Reputation: 2023

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)

EDIT/UPDATE

See below working code based of the advice from BOB!

                #region try set Date from selected background populated Month
                try
                {
                    //param/arg from backgroundWorker
                    string selectedDate = filterByMonthComboBoxParam;

                    //Could also be from direct combobox.selecteditem with system string removed
                    //string selectedDate = filterByMonthComboBox.SelectedItem.ToString().Replace("System.Windows.Controls.ComboBoxItem:", "");

                    for (int ifilterByMonthComboBox = 0; ifilterByMonthComboBox < filterByMonthComboBox.Items.Count; ifilterByMonthComboBox++)
                    {

                        string _filterByMonthComboBox = filterByMonthComboBox.Items[ifilterByMonthComboBox].ToString();
                        if (_filterByMonthComboBox.Contains(selectedDate)){

                            DateTime dtX;
                            if (DateTime.TryParseExact(selectedDate, "MMMM yyyy", null, DateTimeStyles.AllowWhiteSpaces, out dtX))
                            {
                                // Parse success
                                Console.WriteLine(dtX);
                                checkMinDate = dtX.ToString();
                                checkMaxDate = nowTime.ToString();
                                Console.WriteLine("Date Filter is:");
                                Console.WriteLine("Min: " + checkMinDate);
                                Console.WriteLine("Max: " + checkMaxDate);
                            }
                            else
                            {
                                // parse failed
                                Console.WriteLine("Failed");
                            }

                        }


                    }

                }catch(Exception dateError){

                    Console.WriteLine(dateError);

                }
                #endregion try set Date from selected background populated Month

Upvotes: 0

Views: 382

Answers (1)

Bob
Bob

Reputation: 555

DateTime dt;
if (DateTime.TryParseExact("July 2016", "MMMM yyyy", null, DateTimeStyles.None, out dt))
{
    // Parse success
    Console.WriteLine(dt);
}
else
{
    // parse failed
    Console.WriteLine("Failed");
}

Checkout DateTime.TryParseExact() and the formats

Edit:

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

Upvotes: 1

Related Questions