Ad

Can A JSON Feed Be Filtered To Just Show Time From Timestamp String?

- 1 answer

I am remotely retrieving an external JSON feed via JSONP. The JSON feed contains much data including start_time and end_time variables in an array that have the time data encoded in the "JavaScript built-in JSON object and ISO8601" like this:

2016-01-21T13:00:00+10:00

I want to put the data in a preformatted table (there is a good reason why) and I want to remove the date data and just leave the time on the value (i.e. 13:00).

Is it possible to filter/parse the entire JSON data object and convert all these timestamp data strings to just show time before I start using the data (like a preformat run directly on the API feed)?

Example snippet of the JSON data:

var response={
  "bookings": {
    "group_id": 12306,
    "name": "Public Meeting Rooms",
    "url": "http:theurlfeed.from.libcal",
    "timeslots": [{
      "room_id": "36615",
      "room_name": "Meeting Room 2A",
      "booking_label": "Mahjong",
      "booking_start": "2016-01-20T10:00:00+10:00",
      "booking_end": "2016-01-20T11:00:00+10:00"
    }, {
      "room_id": "36615",
      "room_name": "Meeting Room 2A",
      "booking_label": "Mahjong",
      "booking_start": "2016-01-20T11:00:00+10:00",
      "booking_end": "2016-01-20T12:00:00+10:00"
    }, {
      "room_id": "36615",
      "room_name": "Meeting Room 2A",
      "booking_label": "Mahjong",
      "booking_start": "2016-01-20T12:00:00+10:00",
      "booking_end": "2016-01-20T13:00:00+10:00"
    }, {
      "room_id": "36615",
      "room_name": "Meeting Room 2A",
      "booking_label": "Jo Tindall and Sagarika",
      "booking_start": "2016-01-20T14:00:00+10:00",
      "booking_end": "2016-01-20T15:00:00+10:00"
    }, {
      "room_id": "36615",
      "room_name": "Meeting Room 2A",
      "booking_label": "Jo Tindall and Sagarika",
      "booking_start": "2016-01-20T15:00:00+10:00",
      "booking_end": "2016-01-20T16:00:00+10:00"
    }, {
      "room_id": "36615",
      "room_name": "Meeting Room 2A",
      "booking_label": "James Math",
      "booking_start": "2016-01-20T16:00:00+10:00",
      "booking_end": "2016-01-20T17:00:00+10:00"
    }, {
      "room_id": "36615",
      "room_name": "Meeting Room 2A",
      "booking_label": "James Math",
      "booking_start": "2016-01-20T17:00:00+10:00",
      "booking_end": "2016-01-20T18:00:00+10:00"
    }, {
      "room_id": "36615",
      "room_name": "Meeting Room 2A",
      "booking_label": "James Math",
      "booking_start": "2016-01-20T18:00:00+10:00",
      "booking_end": "2016-01-20T19:00:00+10:00"
    }, {
      "room_id": "36616",
      "room_name": "Meeting Room 2B",
      "booking_label": "IELTS",
      "booking_start": "2016-01-20T10:00:00+10:00",
      "booking_end": "2016-01-20T11:00:00+10:00"
    }, {
      "room_id": "36616",
      "room_name": "Meeting Room 2B",
      "booking_label": "recording",
      "booking_start": "2016-01-20T11:00:00+10:00",
      "booking_end": "2016-01-20T12:00:00+10:00"
    }, {
      "room_id": "36616",
      "room_name": "Meeting Room 2B",
      "booking_label": "recording",
      "booking_start": "2016-01-20T12:00:00+10:00",
      "booking_end": "2016-01-20T13:00:00+10:00"
    }, {
      "room_id": "36616",
      "room_name": "Meeting Room 2B",
      "booking_label": "IELTS",
      "booking_start": "2016-01-20T13:00:00+10:00",
      "booking_end": "2016-01-20T14:00:00+10:00"
    }, {
      "room_id": "36616",
      "room_name": "Meeting Room 2B",
      "booking_label": "IELTS",
      "booking_start": "2016-01-20T15:00:00+10:00",
      "booking_end": "2016-01-20T16:00:00+10:00"
    }, {
      "room_id": "36616",
      "room_name": "Meeting Room 2B",
      "booking_label": "Luke",
      "booking_start": "2016-01-20T16:00:00+10:00",
      "booking_end": "2016-01-20T17:00:00+10:00"
    }, {
      "room_id": "36616",
      "room_name": "Meeting Room 2B",
      "booking_label": "Luke",
      "booking_start": "2016-01-20T17:00:00+10:00",
      "booking_end": "2016-01-20T18:00:00+10:00"
    }, {
      "room_id": "36616",
      "room_name": "Meeting Room 2B",
      "booking_label": "Luke",
      "booking_start": "2016-01-20T18:00:00+10:00",
      "booking_end": "2016-01-20T19:00:00+10:00"
    }],
    "last_updated": "2016-01-20T12:40:36+10:00"
  }
}
Ad

Answer

For most of what you want to do, there aren't any built in methods, so you're going to have to make your own.

You need to define a function to get the hours and minutes in the format you want from the timestamp. You can use the Date constructor to get a date object out of the timestamp, then use Date.toTimeString() and extract the first two sections of the result joined by a semicolon.

function getTime(timestamp) {
    return new Date(timestamp).toTimeString().split(':').filter(function(v,k){
        return k < 2;
    }).join(':');
}

Then it is just a matter of looping through the timeslots and assigning the results of the functions to the original properties. JavaScript objects are pass-by-reference, which means that we can cache the object path to save some keystrokes.

for(var i in timeslots) {
    timeslots[i].booking_start = getTime(timeslots[i].booking_start);
    timeslots[i].booking_end   = getTime(timeslots[i].booking_end);
}

Here's a simple example in action, I've slimmed down and condensed the JSON object to save space here.

// The slimmed down and condensed object
var response = {
    "bookings": {
        "group_id": 12306,
        "name": "Public Meeting Rooms",
var response = {
    "bookings": {
        "group_id": 12306,
        "name": "Public Meeting Rooms",
        "url": "http:theurlfeed.from.libcal",
        "timeslots": [{"room_id": "36615","room_name": "Meeting Room 2A","booking_label": "Mahjong","booking_start": "2016-01-20T10:00:00+10:00","booking_end": "2016-01-20T11:00:00+10:00"}, {"room_id": "36615","room_name": "Meeting Room 2A","booking_label": "Mahjong","booking_start": "2016-01-20T11:00:00+10:00","booking_end": "2016-01-20T12:00:00+10:00"},{"room_id": "36615","room_name": "Meeting Room 2A","booking_label": "Mahjong","booking_start": "2016-01-20T12:00:00+10:00","booking_end": "2016-01-20T13:00:00+10:00"}], 
        "last_updated": "2016-01-20T12:40:36+10:00"
    }
};

// Make it a function, for ease of use
function formatTimes(bookings) {
    // Format the timestamps
    function getTime(timestamp) {
        return new Date(timestamp).toTimeString().split(':').filter(function(v,k){
            return k < 2;
        }).join(':');
    }
    
    // Iterate timeslots, replacing the start and end values
    for(var i in bookings.timeslots) if(bookings.timeslots.hasOwnProperty(i)) {
        var timeslot = bookings.timeslots[i];
        timeslot.booking_start = getTime(timeslot.booking_start);
        timeslot.booking_end   = getTime(timeslot.booking_end);
    }
    
    // Replace the last updated value
    bookings.last_updated = getTime(bookings.last_updated);
}

// Format the times and display the result
formatTimes(response.bookings);
document.body.innerHTML = '<pre>' + JSON.stringify(response.bookings, null, 4) + '</pre>';

Ad
source: stackoverflow.com
Ad