2

I am accessing the SharePoint 2010 List WCF data service via DataJS and getting back date fields as /Date(1363708765000)/ does anyone have any idea how I can process this to display a proper date?

Note: I am posing here as I suspect this is a DataJS question more than a peculiarity of the ListData.svc in SharePoint.

Robert Kaucher
  • 1,841
  • 3
  • 22
  • 44

1 Answers1

3

What you're receiving back is a Unix epoch, which represents the number of milliseconds that have elapsed since Jan. 01 1970. Fortunately, JavaScript also uses this as its epoch, meaning you can create a new Date object using elapsed milliseconds as your input parameter:

var myDate = new Date(1363708765000);
console.log("UTC:" + myDate.toUTCString());
// outputs UTC: Tue, 19 Mar 2013 15:59:25 GMT

Of course, you'll have to parse out the integer portion of the date value you're getting back before you can use it to initialize a date. A reusable function might look like this:

function parseJsonDate( sDate ) {
    var b, e, i;
    b = sDate.indexOf('(');
    e = sDate.indexOf(')');
    i = sDate.substring(b+1,e);

    if (isNaN(i)) { return null };
    return new Date(parseInt(i));
}
CBono
  • 3,803
  • 1
  • 32
  • 40