0

I have a list coming from service, and I need the list to be sorted by date in that list. So I have done following.

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;`

When I debug the list is getting sorted and Date field is good too. But When I display the date through Jquery, The date is showing up like this "Date(1386658800000)" Jquery Code:

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

Please help. And I cant convert the datetime to string in C# code, as I need the sort functionality.

maccettura
  • 10,514
  • 3
  • 28
  • 35
Techno
  • 142
  • 4
  • 23
  • What is your intention with this line: `programInformationItem.EffectiveDate = Convert.ToDateTime(item.EffectiveDate.ToShortDateString());` Maybe I am missing something but this seems really bad haha – maccettura Sep 20 '17 at 20:44
  • Is it possible to edit programInformation? Such as add a property? – ahmet Sep 20 '17 at 20:44

1 Answers1

2

I think the value you are getting is /Date(1386658800000)/

That is the Epoch representation of the DateTime value you had. The json serializer converted the date time value to it's corresponding unix epoch time (the number of seconds that have elapsed since 00:00:00 Coordinated Universal Time (UTC), Thursday, 1 January 1970).

You can use a little javascript to get a Date object from that

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

The statement prgvalue.EffectiveDate.substr(6) will return a string value like 1386658800000)/ and passing this to parseInt method returns the number 1386658800000 which can be safely passed to the Date constructor.

You can call the relevant methods on the Date object now

"<tr><td>" + dateObj.toDateString()+"</td></tr>"
Shyju
  • 214,206
  • 104
  • 411
  • 497