Quantcast
Viewing all articles
Browse latest Browse all 16

Answer by Sky Sanders for OBSOLETE - Soapi.JS : fluent JavaScript client library for the Stack Exchange API

Date Handling

As of RC2, Soapi.js is Dates in, Dates out. No more dealing with unix timestamps.

This includes input parameters, todate, fromdate, min and max (when applicable) and all response object date properties e.g. creation_date is converted to a JavaScript date object before it is passed to your success function.

todate, fromdate may also be specified using string, e.g. "1 jun 2010 00:00:00".

min and max are special 'variant' parameters and when dates are appropriate you must explicitly set a Date object.

Example

<script src="scripts/Soapi.js" type="text/javascript"></script><script type="text/javascript">    // demonstrate dates in / dates out    // no need to deal with unix timestamps in Soapi.js    // here are some date objects    var fromDate = new Date("Tue, 1 Jun 2010 00:00:00");    var toDate = new Date("Tue, 1 Jun 2010 01:00:00");    // get some questions    // use min/max parameters with date objects    // dates are converted to unix timestamps to build the url    Soapi.RouteFactory("api.stackoverflow.com", "").Questions({        min: fromDate,        max: toDate,        sort: Soapi.Domain.PostSort.creation,        pagesize: 1    })    .getResponse(function(data) {        // unix timestamps are converted to Date in response before you get them        alert("min/max\r\nThis date should be between Tue, 1 Jun 2010 00:00:00 and Tue, 1 Jun 2010 01:00:00 \r\n\r\n"+ data.items[0].creation_date +"\r\n\r\n Dates In - Dates Out.\r\nNeat, huh?");    }, function(error) { alert(error.message); });    // use fromdate/todate parameters with date objects    // dates are converted to unix timestamps to build the url    Soapi.RouteFactory("api.stackoverflow.com", "").Questions({        fromdate: fromDate,        todate: toDate,        sort: Soapi.Domain.PostSort.creation,        pagesize: 1    })    .getResponse(function(data) {        // unix timestamps are converted to Date in response before you get them        alert("fromdate/todate\r\nThis date should be between Tue, 1 Jun 2010 00:00:00 and Tue, 1 Jun 2010 01:00:00 \r\n\r\n"+ data.items[0].creation_date +"\r\n\r\n Dates In - Dates Out.\r\nNeat, huh?");    }, function(error) { alert(error.message); });        </script>

Next: Building Soapi.js


Viewing all articles
Browse latest Browse all 16

Trending Articles