2012-01-12 67 views
0

我有以下變量來存儲列表項目的值用於顯示。然而,如果日期時間字段和列表中的字段之一,我收到錯誤。我試圖將它轉換爲一個字符串,但還沒有設法讓它正確顯示。顯示和轉換共享點中的日期時間列表字段

foreach (SPListItem oListItem in listItemCollection) 

awardYear = oListItem["Year"].ToString(); 
awardCategory = oListItem["Category"].ToString(); 
awardOrganiser = oListItem["Organiser"].ToString(); 
awardNomWon = oListItem["Nominated/Won"].ToString(); 

//converting the date time field to a string 
DateTime convertedaValue = DateTime.Parse(awardYear); 

string awardYearConverted = convertedaValue.ToString(); 

PlaceHolder1.Controls.Add(new LiteralControl(awardYearConverted + " " + "|" + " " + 
awardCategory + " " + "|" + " " + awardOrganiser + "<br/><br/>")); 

//PlaceHolder2.Controls.Add(new LiteralControl(awardOrganiser)); 


} 

問題是日期時間字段值沒有得到轉換。我是否以正確的方式談論這個問題?

任何suggetstion將不勝感激!!非常感謝

+0

,你能告訴我們'awardYear'的價值?我懷疑它可能與'DateTime'的任何字符串表示沒有任何關係。 – StaWho 2012-01-12 14:27:45

+0

這裏有很多處理正在進行。首先,您將一個Date對象轉換爲一個字符串,然後解析該字符串以將其轉換回Date對象,然後再次轉換爲字符串並複製到另一個字符串。只需使用文字控制中的第一次轉換 - 即LiteralControl(oListItem [「Year」]。ToString().... – 2012-01-12 14:53:22

+0

Hi「StarWho」獎勵的價值是01/09/2010。 – 2012-01-12 17:08:21

回答

1

試試這個

SPUtility.FormatDate(web, DateTime.Parse(oListItem["Year"].ToString()), SPDateFormat.DateTime).ToString(); 
相關問題