2012-06-14 166 views
-1

嗨,大家好,我如何讓它只顯示日期。 temp.StartDate.Date不工作,它顯示爲2012/3/5 12:00:00只顯示日期

我試圖

temp.StartDate.Date 
temp.StartDate 
temp.StartDate.ToShortDateString 
temp.StartDate.Date.ToShortDateString 

這裏是我的代碼

List<DateTime> dateList = new List<DateTime>(); 
    foreach(sMedication temp in medicationList) 
    { 
     if (dateList.Contains(temp.StartDate.Date)) 
     { 

     } 
     else 
     { 
      dateList.Add((temp.StartDate.Date)); 

     } 
    } 
    lstboxSchedule.ItemsSource = date 

清單;

+2

'ToShortDateString'應該可以工作。你究竟試過了什麼? – Stefan

+0

你如何設置顯示部分?我相信你是DateTime List。 – V4Vendetta

回答

1

您當前的代碼嘗試修改值,但不會將其轉換爲顯示

因此,像這樣將轉換您的字符串顯示:

List<string> dateList = new List<string>(); 
foreach(sMedication temp in medicationList) 
{ 
    if (!dateList.Contains(temp.StartDate.ToShortDateString())) 
    { 
     dateList.Add((temp.StartDate.ToShortDateString())); 
    } 
} 
lstboxSchedule.ItemsSource = date 
+0

謝謝您的主席, (!dateList.Contains(temp.StartDate.ToShortDateString()))真的有幫助> 。< – CodeGuru

0

使用

temp.StartDate.ToString("dd/MM/yyyy"); 
+1

請不要對用戶強加一個日期格式(特別是不要這樣的braindead之一)。他們可以配置他們的手機和應用程序應使用配置的。 – Joey

+2

@Joey:我理解你的理由,但我的「*嘗試使用*」是告訴OP嘗試它是否有效。我沒有強加任何東西,我建議他嘗試......但是好的,我尊重你的觀點。 – Marco

+0

參數1:無法從'字符串'轉換爲'System.DateTime' 嗯,感謝您幫助Marco! – CodeGuru