1

I'm trying to find the most correct way to determine the amount of seconds since an item was created.

My first approach is to simply diff the Created property with the browser time, e.g., moment().diff(moment(item.Created), 'seconds'), this however depends on the browser, and so it can be off with several seconds (saw one over two minutes off).

I don't know any way to query the server for current time, except for the <updated> tag when querying for XML, e.g., on a smaller object: _api/Web/CurrentUser?$select=herpaderp (not present for application/json).

So beside my hack are there any way to get the server current time?
Or any other way to calculate the time difference?
Thanks!

2

You could use the response header from your REST request to figure out what date (and time) the server thinks it is.

You can get at that information with something like:

var req = $.getJSON("/_api/web/", function(data) { console.log(data) }) // doesn't matter, just make a request call...
var serverDate = req.getResponseHeader("Date");

Then you'll just have to convert the created time back to GMT (or the server time to your local time which maybe easier) and diff those. It would be off by as much time as it took the server to return the call and you to run your diff.

  • Not a bad idea at all. Compared to my current hack I guess here you get the date from IIS, while I get it from SharePoint, but that would be the same, and here you have GMT while I get UTC, but that is also basically the same right? – eirikb Jan 29 '15 at 21:16
  • Correct, GMT == UTC (google.com/search?q=GMT+vs+UTC for a mildly comical confirmation) :D – John-M Jan 29 '15 at 21:18
  • I kind of feel I'm trading one hack for another, would be nice to have a proper timestamp somwhere in _api. Oh well, I guess this is the best answer I will get, and the correct one at that. Thanks :) – eirikb Jan 29 '15 at 21:20

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.