0

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.

2
  • 1
    That's not JSON, that's just a Javascript object. Are you asking how to parse those dates with Javascript? Commented Jul 12, 2013 at 17:05
  • @nullability yes. I was confusing another project I'm working on at the same time. Updated the question. Commented Jul 12, 2013 at 17:05

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Great idea and was just what I needed to get started.
-1 for Datejs. Alpha-1 since 2007, 126 open issues, and no development since 2008. If you want a library that does that and is maintained, look at moment.js instead.
Thanks for the input. I never professed that Datejs was the best or that I was up to date on all JavaScript date libraries.
No problem. Just making sure you are aware. DateJS was a great idea, but they dropped the ball.
0

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;
    };

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.