Search code examples
javascriptjquerydatesharepoint-2010

How to parse date so timezone is not included?


Parsing info from a list into Javascript

Here is a line of data:

{"__metadata":{"uri":"BASE_URL/_vti_bin/listdata.svc/RFCExtract(1)","etag":"W/\"3\"","type":"Microsoft.SharePoint.DataService.RFCExtractItem"},"Title":"Test New","Class":1,"Status":"Closed","Date":"/Date(1292112000000)/","RFCNumber":1}

NOte that the date field is like so : "Date":"/Date(1292112000000)/

The database list displays the date as 12/12/2010

The technique below has the date set to one day earlier due to the timezone. How can I ignore this timezone adjustment, so the date replicates what ever the database displays?

var date=parseJsonDate(item.Date); 
console.log(date);  //Sat Dec 11 2010 19:00:00 GMT-0500 (Eastern Standard Time)
dateField=date.getFullYear()+"-"+(date.getMonth()+1)+"-"+date.getDate();
console.log(dateField); //2010-12-11

This should be a straightforward simple fix, I want this result to be the same value as reported by the database, JavaScript should not try to offer another view.

Update Just tried jQUery

var date=$.parseJSON(item.Date);
blows up due to unexpected token /

Solution

  • It is assuming the date supplied is in UTC and doing the conversion. Try below.

    var date=parseJsonDate(item.Date); 
    console.log(date.toUTCString());  //Sun, 12 Dec 2010 00:00:00 GMT
    dateField=date.getUTCFullYear()+"-"+(date.getUTCMonth()+1)+"-"+date.getUTCDate();
    console.log(dateField); //2010-12-12