Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • JonnyJ 2 posts 61 karma points
    Sep 02, 2014 @ 20:31
    JonnyJ
    0

    Formatting pubdate node from rss feed

    Hey everyone,

    I'm just about brand new to writing in Razor, and equally new to Umbraco. Before this, I've only worked in php based cms's before this, so some habits need to be broken. Also, some things just need to be relearned.

    So far, Umbraco is really great, love the community here, love the system. I've managed to have the majority of my questions answered through searches, but I'm hung up on this one.

    I'm making a piece of code where users can enter an rss feed and have it output the feed into a page. It works great, except the timestamp is displaying the rss default time, which is not terribly user friendly.

    Here's the code thus far:

             <div class="rssFeed">    
            @using System.Xml.XPath;
            @using System.Xml;
    
            @{
                XmlTextReader udBrudRSS = new XmlTextReader(RssFeedDrillDown);
                XmlDocument doc = new XmlDocument();
                doc.Load(udBrudRSS);
                XmlNodeList rssItems = doc.SelectNodes("//item");
            }
            <ul class="rssFeed">
                @{
                    //For each item node we can then ouput what we want
                    int someLinkCount = Int32.Parse(Model.GetElementValue("FeedItems"));
                    var i =0;
                    foreach (XmlNode node in rssItems)
                    {
                        <li class="rssFeedItem">
                            <div class="rssTitle"><a href="@node["link"].InnerText">@node["title"].InnerText</a></div>
                            <div class="rssDate">@node["pubDate"].InnerText</div>                            
                            <p class="rssMessage">@Html.Raw(@node["description"].InnerText)</p>
                        </li>
                        i++;
                        if (i == @someLinkCount){ break; }
                    }
                }
            </ul>
            <!-- /content -->
            </div>
    

    I've found the following snippet, which seems to answer the question i have:

    string orig ="Wed, 29 Oct 2008 14:14:48 +0000";
    string newstring =String.Format("{0:MM/dd/yyyy hh:mm tt}",DateTime.Parse(orig.Remove(orig.IndexOf(" +"))));
    

    But I'm having trouble bridging the two pieces of code!

    Any insight is greatly appreciated

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Sep 02, 2014 @ 20:48
    Jeavon Leopold
    100

    Hi Jonny and welcome to Our!

    I think you need something like this:

    var orig = "Wed, 29 Oct 2008 14:14:48 +0000".AsDateTime().ToString("D");
    

    Or in your specific code:

    @node["pubDate"].InnerText.AsDateTime().ToString("D");
    

    You find info on the various default formats here or custom ones here

    Jeavon

  • JonnyJ 2 posts 61 karma points
    Sep 02, 2014 @ 21:01
    JonnyJ
    0

    Brilliant! Thank you so much. I think I was overcomplicating the matter.

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Sep 02, 2014 @ 21:14
    Jeavon Leopold
    0

    You're welcome!

    I'm happy to have posted a solution using the Razor string conversion helper methods (AsDateTime) that I only recently discovered myself :-)

Please Sign in or register to post replies

Write your reply to:

Draft