2017-09-20 26 views
0

我有一個來自服務的列表,我需要列表按日期在該列表中排序。所以我做了以下。來自控制器的日期時間,通過jquery顯示

List<ProgramInformation> programInformation = new List<DataAccess.ProgramInformation>(); 
foreach (StatusItem item in getStatusRes.StatusItems) 
{       
    ProgramInformation programInformationItem = new ProgramInformation(); 
    programInformationItem.EffectiveDate = Convert.ToDateTime(item.EffectiveDate.ToShortDateString());     
    programInformationItem.UpdatedBy = item.UpdatedBy; 
    programInformation.Add(programInformationItem); 
} 
programInformation.Sort((x, y) => x.EffectiveDate.CompareTo(y.EffectiveDate));      
return programInformation;` 

當我調試列表得到排序和日期字段也很好。但是當我顯示通過jQuery的日期,日期顯示出來這樣 「日期(1386658800000)」 jQuery代碼:

<tr><td style='color:gray'>" + prgvalue.EffectiveDate 

請幫助。我不能將日期時間轉換爲C#代碼中的字符串,因爲我需要排序功能。

+0

這有什麼行的意圖:'programInformationItem.EffectiveDate = Convert.ToDateTime(item.EffectiveDate.ToShortDateString());'也許我我錯過了一些東西,但這似乎很糟糕哈哈 – maccettura

+0

是否有可能編輯programInformation?比如添加一個屬性? – ahmet

回答

2

,我認爲你所得到的值是/Date(1386658800000)/

也就是說你有DateTime值的時代代表性。 json序列化程序將日期時間值轉換爲相應的unix紀元時間(從1970年1月1日星期四00:00:00協調世界時(UTC)開始經過的秒數)。

您可以使用一些JavaScript得到來自

var dateObj = new Date(parseInt(prgvalue.EffectiveDate.substr(6))); 

聲明prgvalue.EffectiveDate.substr(6) Date對象將返回像1386658800000)/一個字符串值,並將其傳送至parseInt方法返回一個可以安全地傳遞給數1386658800000 Date構造函數。

可以調用Date目標的相關方法現在

"<tr><td>" + dateObj.toDateString()+"</td></tr>"