Search code examples
c#jqueryajaxdatewebmethod

Date returning from c# web method - /Date(xxxxxxx)/


I am using jquery ajax to request a list of objects from a database via a c# web method:

[WebMethod]
public static List<SessionQueue> GetActiveIssues()
{           
  try
  {
    return MyContext.SessionQueues.Where(x => !x.Resolved).OrderBy(d => d.SubmittedTime).ToList();
  }
  catch (Exception ex)
  {
    throw new Exception(ex.ToString()); // catch in jquery
  }   

}

Each SessionQueue object has a property:

public System.DateTime SubmittedTime

When I receive the SubmittedTime in jquery, the date looks like this:

/Date(1445975227197)/

How do I parse this to a valid date string? e.e mm/dd/yy xx:xx am

I have tried:

function formatLongDate(date) {

    var nd = Date.parse(date);
    var dt = new Date(nd);
    var dtStr = $.datepicker.formatDate("mm/dd/yy", dt) + ' ' + getTime(date)
    return dtStr;
}

but /Date(1445975227197)/ is not a valid date ticks


Solution

  • Try using String.prototype.match() , Array.protottpe.map()

    new Date("/Date(1445975227197)/".match(/\d+/).map(Number)[0])