How to format Date in ASP.NET

拜拜、爱过 提交于 2019-12-25 03:51:14

问题


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

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.

Any help much appreciated.

I have c# code...

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

回答1:


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");



回答2:


You can save the date by using DateTime.Parse or DateTime.ParseExact.

DateTime xmlDateTime = DateTime.Parse(xmlValue);

From there, it's a matter of outputting it using DateTime.ToString() with the desired format parameters (or any of the built-in ToShortDateString, ToLongDateString, ToShortTimeString, etc.)

xmlDateTime.ToShortDateString();



回答3:


I would do something like:

DateTime parsedDate=DateTime.Min;
if(DateTime.TryParse(xmlStringWithDate,ref parsedDate))
{
   //Do something useful with parsedDate since the parse was successful
}



回答4:


You could use String.Format to do this:

 sDescription = String.Format("{0:D}", MyRssDetail.InnerText);

Or convert it to a date, then go from there:

sDescription = Convert.ToDateTime(MyRssDetail.InnerText).toShortDateString();



回答5:


String.Format({0:D}, dateToFormat)

I am assuming you want to do the pubDate?

sDescription = String.Format({0:D}, MyRssDetail.InnerText.ToDate())


来源:https://stackoverflow.com/questions/6947423/how-to-format-date-in-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!