本文介绍了如何在ASP.NET中格式化日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过雅虎管道运行我的Facebook状态Feed,并将其输入/嵌入到我自己的托管网站的页面。

I am running my Facebook status feed through Yahoo pipes and outputting/embedding it on to a page my own hosted website.

XML Feed包含一个日期。我想格式化日期,但不知道如何。 XML部分是日期输出是... < pubDate>星期四,2011年7月14日20:38:07 + 0100< / pubDate> 我想要渲染有些东西像14/07/2011。

The XML feed contains a date. I want to format the date but don't know how to. The XML part is date output is...<pubDate>Thu, 14 Jul 2011 20:38:07 +0100</pubDate> and I would like it to render something like 14/07/2011.

任何帮助不胜感激。

我有c#代码...

protected void Page_Load(object sender, EventArgs e)
    {

        WebRequest MyRssRequest = WebRequest.Create("http://pipes.yahoo.com/pipes/pipe.run?FacebookRssUrl=http%3A%2F%2Fwww.facebook.com%2Ffeeds%2Fpage.php%3Fid%3D456456456456456%26format%3Drss20&_id=456456456456456456464&_render=rss");
        WebResponse MyRssResponse = MyRssRequest.GetResponse();

        Stream MyRssStream = MyRssResponse.GetResponseStream();

        // Load previously created XML Document
        XmlDocument MyRssDocument = new XmlDocument();
        MyRssDocument.Load(MyRssStream);

        XmlNodeList MyRssList = MyRssDocument.SelectNodes("rss/channel/item");

        string sTitle = "";
        string sLink = "";
        string sDescription = "";

        // Iterate/Loop through RSS Feed items
        for (int i = 0; i < 3; i++)
        {
            XmlNode MyRssDetail;

            MyRssDetail = MyRssList.Item(i).SelectSingleNode("title");
            if (MyRssDetail != null)
                sTitle = MyRssDetail.InnerText;
            else
                sTitle = "";

            MyRssDetail = MyRssList.Item(i).SelectSingleNode("link");
            if (MyRssDetail != null)
                sLink = MyRssDetail.InnerText;
            else
                sLink = "";

            MyRssDetail = MyRssList.Item(i).SelectSingleNode("pubDate");
            if (MyRssDetail != null)
                sDescription = MyRssDetail.InnerText;

            else
            {
                sDescription = "";
            }

            // Now generating HTML table rows and cells based on Title,Link & Description
            HtmlTableCell block = new HtmlTableCell();
            block.InnerHtml = "<span style='font-weight:bold'><a href='" + sLink + "' target='new'>"+ sTitle + "</a></span>";
            HtmlTableRow row = new HtmlTableRow();
            row.Cells.Add(block);
            tbl_Feed_Reader.Rows.Add(row);
            HtmlTableCell block_description = new HtmlTableCell();
            block_description.InnerHtml = "<p align='justify'>" + sDescription + "</p>";
            HtmlTableRow row2 = new HtmlTableRow();
            row2.Cells.Add(block_description);
            tbl_Feed_Reader.Rows.Add(row2);
        }
    }


推荐答案

你可以将其解析为DateTime,然后呈现。 / p>

You can parse it to DateTime and render it in specific format you want.

var x = DateTime.Parse(MyRssDetail.InnerText);
//Your date format
sDescription = x.ToString("d MMM yyyy");

这篇关于如何在ASP.NET中格式化日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-10 22:54