4

I was having a hard time getting my list items to POST back to the SharePoint list when I discovered if I delete the Modified and Created properties from the JSON it would work. The JSON that is being retrieved from the server looks like the following:

"Created": "/Date(1455633698000)/",

Modified looks basically the same with a different number. Is there a way I can convert this so I can submit it with my POST? If I leave either one in the JSON I get a 400 error when I try to POST and if I delete both properties then everything works as intended. Thanks for the help.

1

I suggest that you use MomentJS.

var created = moment("/Date(1455633698000)/");

var postBackData = {

   Title: "I'm a title",
   Created: created.toJSON()

};

You can also use MomentJS to format you dates very flexibly in the UI.

created.format('MMMM Do YYYY, h:mm:ss a'); //February 16th 2016, 9:41:38 am
created.format('MM/DD/YYYY'); //02/16/2016
created.fromNow(); //10 hours ago
0

As per this MSDN post DateTime object returned by SP Rest queries are:

represented in JSON as "/Date(number of ticks)/". The number of ticks is a positive or negative long value that indicates the number of ticks (milliseconds) that have elapsed since midnight 01 January, 1970 UTC. The maximum supported date value is MaxValue (12/31/9999 11:59:59 PM) and the minimum supported date value is MinValue (1/1/0001 12:00:00 AM).

You can use below JS function to convert JSON DateTime object into native JS DateTime:

function parseDateFromJSON(jsonDateValue)
{
    return new Date(parseInt(json.replace(/\/Date\((\d+)\)\//gi, "$1")));
}

Usage:

var createdDate = parseDateFromJSON('/Date(1455633698000)/');
0

Since dates are always integers you can extract the datevalue as string with

"/Date(1455633698000)/".match(/\d+/)[0]

if you need the integer value cast it to a Number with +

+"/Date(1455633698000)/".match(/\d+/)[0]

SharePoint adds a .format prototype to the String object, so some of MomentJS good stuff (formatting dates) can be done without loading an extra library:

var myDate=new Date( +"/Date(1455633698000)/".match(/\d+/)[0] );
String.format( "{0:MM/dd/yy}" , myDate );

MSDN String.format() Documentation

String.format("{0:i}",new Date());  outputs: Wed Oct 07 2015 20:39:54 GMT+0200 (W. Europe Daylight Time)
String.format("{0:F}",new Date());  outputs: Wednesday, 07 October 2015 20:39:54
String.format("{0:f}",new Date());  outputs: Wednesday, 07 October 2015 20:39
String.format("{0:D}",new Date());  outputs: Wednesday, 07 October 2015
String.format("{0:s}",new Date());  outputs: 2015-10-07T20:39:54
String.format("{0:d}",new Date());  outputs: 10/07/2015
String.format("{0:dd}",new Date());  outputs: 07
String.format("{0:ddd}",new Date());  outputs: Wed
String.format("{0:dddd}",new Date());  outputs: Wednesday
String.format("{0:m}",new Date());  outputs: October 07
String.format("{0:M}",new Date());  outputs: October 07
String.format("{0:MM}",new Date());  outputs: 10
String.format("{0:MMM}",new Date());  outputs: Oct
String.format("{0:MMMM}",new Date());  outputs: October
String.format("{0:Y}",new Date());  outputs: 2015 October
String.format("{0:y}",new Date());  outputs: 2015 October
String.format("{0:yy}",new Date());  outputs: 15
String.format("{0:yyyy}",new Date());  outputs: 2015
String.format("{0:gg}",new Date());  outputs: A.D.
String.format("{0:T}",new Date());  outputs: 20:39:54
String.format("{0:t}",new Date());  outputs: 20:39
String.format("{0:HH}",new Date());  outputs: 20
String.format("{0:mm}",new Date());  outputs: 39
String.format("{0:ss}",new Date());  outputs: 54

iDATE iDOC

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.