Jonny Sooter
Jonny Sooter

Reputation: 2417

Separating Date strings into JS Object

I'm building a calendar that takes a JS object input from an RSS feed. This RSS feed is generated automatically from a CMS used internally in my company. I cannot change anything on the CMS side nor the returned RSS feed. All I have access to is the object that is built from that feed.

The RSS feed joins the start date, end date, time, and title all in one string. I need to separate them into keys in my object so my calendar can display them.

The problem I'm running into is the RSS feed formats the string differently depending on how the event is setup in the CMS. For example:

"7/15/2013 8:00 PM - 9:00 PM Blah" //Date, Time, Title
"7/12/2013 Blue" //Date for all day event, Title
"7/6/2013 8:00 AM - 7/23/2013 9:00 AM Banana" //Long event - Start Date, Start Time, End Date, End Time, Title

As you can see, how different these are I'm having a hard time deciding how I should go about parsing these into my object. The object should look like this:

{
    title: 'Banana',
    start: new Date(2013, 7, 24, 10, 30),
    end: new Date(2013, 7, 24, 11, 30),
    allDay: false
}

My question comes down to this: What would be a the best way to approach this? Use regex, try to parse it manually with things like .indexOf("/"), build test cases for each one, or some other suggestion.

PS: A jQuery example is an acceptable answer as well.

Upvotes: 0

Views: 114

Answers (2)

Jonny Sooter
Jonny Sooter

Reputation: 2417

Based on Jon's answer, this is what I ended up with:

parseEntries: function() {
    //Rename to fit plugin requirements
    for (var i = 0; i < Calendar.entries.length; i++) {
        var entry = Calendar.entries[i];

        //Rename
        entry["url"] = entry["link"];
        delete entry["link"];

        var position = entry.title.indexOf(' - ');

        if (position === -1) {
            //All day event
            entry.allDay = true;
            var space = entry.title.indexOf(" "),
                title = entry.title.substring(space + 1),
                firstHalf = entry.title.slice(0, space); //Start date, no time because it's all day event
        } else {
            var firstHalf = entry.title.slice(0, position), //Start date/time
                secondHalf = entry.title.substring(position + 3);

            if (secondHalf.indexOf("AM") !== -1) {
                var title = secondHalf.substring(secondHalf.indexOf("AM") + 3); //Title if has AM
            } else {
                var title = secondHalf.substring(secondHalf.indexOf("PM") + 3); //Title if has PM
            }

            secondHalf = secondHalf.slice(0, -(title.length + 1)); //End date/time
        }

        entry["start"] = firstHalf;
        entry["end"] = secondHalf;
        entry.title = title;
    };

Upvotes: 0

Jon
Jon

Reputation: 1234

Are you able to use a library like Datejs?

You might want to start splitting on ' - ' (with spaces). If you have one part, you know it's an all day event with a title. If it's two parts, you know it's a start/end event and there's a title in the second piece. After you parse out the title, you can use Datejs to create Date objects:

Date.parse('7/15/2013 8:00 PM')

From there you should have enough to build your JSON object.

Upvotes: 1

Related Questions