【问题标题】:Convert string to dd/mm/yyyy in C# WPF在 C# WPF 中将字符串转换为 dd/mm/yyyy
【发布时间】:2016-09-11 03:13:36
【问题描述】:

我有一个从所选组合框项返回的值(作为日期)。我希望将此值转换为正确的 DateTime 字符串。问题是要转换的combobox.selectedItem 中的字符串是;

July 2016

我想把它转换成(总是月初);

01/07/2016 12:00AM

有没有在 C# 中做的事情?

我能想到的唯一方法就是这样做;

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

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

}

这根本不理想...特别是如果我想过去 24 个月以上(因为组合框每个月都会填充来自 xml 文件的两个日期之间)

编辑/更新

根据 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

【问题讨论】:

  • 不是它的 combobox.selectedItem 内容,使用 combobox.selectedItem.ToString().Replace("System.Windows.Controls.ComboBoxItem:" 删除了 "System.Windows.Controls.ComboBoxItem:" , "");....组合框在后台使用 foreach month 在两个日期之间填充...

标签: c# wpf date datetime combobox


【解决方案1】:
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");
}

结帐DateTime.TryParseExact()formats

编辑:

如果日期有空格,则在其上使用string.Trim() 或将DateTimeStyles.None 更改为DateTimeStyles.AllowWhiteSpaces

【讨论】:

  • 这会起作用,但是剥离的字符串在“ July 2016 ”中有空格,导致它失败..
  • 该死,这很好!,DateTimeStyles.AllowWhiteSpaces 就像一个魅力..thx +1
猜你喜欢
  • 1970-01-01
  • 2017-05-30
  • 1970-01-01
  • 2013-06-30
  • 2018-05-19
  • 2019-01-26
  • 1970-01-01
  • 1970-01-01
  • 2014-04-24
相关资源
最近更新 更多