【问题标题】:How to format Date in ASP.NET如何在 ASP.NET 中格式化日期
【发布时间】:2011-08-04 19:27:12
【问题描述】:

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

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

【问题讨论】:

  • 你的问题很不清楚。请自己阅读,看看是否有意义,然后对其进行编辑,以便人们理解您的要求。

标签: asp.net date format


【解决方案1】:

你可以把它解析成你想要的 DateTime 和 render it in specific format

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

【讨论】:

  • 很抱歉这么慢才回复你。非常感谢。
【解决方案2】:

您可以使用DateTime.ParseDateTime.ParseExact 保存日期。

DateTime xmlDateTime = DateTime.Parse(xmlValue);

从那里,只需使用带有所需格式参数(或任何内置 ToShortDateString、ToLongDateString、ToShortTimeString 等)的 DateTime.ToString() 输出它即可

xmlDateTime.ToShortDateString();

【讨论】:

    【解决方案3】:

    我会这样做:

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

    【讨论】:

      【解决方案4】:

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

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

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

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

      【讨论】:

        【解决方案5】:
        String.Format({0:D}, dateToFormat)
        

        我假设你想做 pubDate?

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-10-20
          • 2011-01-31
          • 2014-04-18
          • 2011-06-17
          • 1970-01-01
          相关资源
          最近更新 更多