如何在 ASP.NET 中设置日期格式

发布于 2024-11-27 18:07:45 字数 2543 浏览 0 评论 0原文

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

XML 提要包含日期。我想格式化日期但不知道该怎么做。 XML 部分是日期输出是...Thu, 14 Jul 2011 20:38:07 +0100 我希望它呈现类似 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);
        }
    }

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

晨光如昨 2024-12-04 18:07:45

您可以将其解析为 DateTime 并以您想要的特定格式呈现

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

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");
停顿的约定 2024-12-04 18:07:45

您可以使用 DateTime.ParseDateTime.ParseExact

DateTime xmlDateTime = DateTime.Parse(xmlValue);

从那里,需要使用 DateTime.ToString()< 输出它/code>具有所需的格式参数(或任何内置的 ToShortDateString、ToLongDateString、ToShortTimeString 等)

xmlDateTime.ToShortDateString();

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();
梦醒时光 2024-12-04 18:07:45

我会做类似的事情:

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

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
}
别闹i 2024-12-04 18:07:45

您可以使用 String.Format 来执行此操作:

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

或将其转换为日期,然后从那里开始:

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

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();
椒妓 2024-12-04 18:07:45
String.Format({0:D}, dateToFormat)

我假设你想做 pubDate?

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

I am assuming you want to do the pubDate?

sDescription = String.Format({0:D}, MyRssDetail.InnerText.ToDate())
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文