RSS源中pubDate的DateTime.Parse

时间:2009-09-24 21:10:03

标签: c# datetime rss parsing

我正在尝试从C#中的RSS提要中提取DateTime对象,并且DateTime.Parse(字符串)对于BBC rss提要正常工作,其格式如下:Thu,2009年9月24日13:08:30 GMT < / p>

但是当我尝试将其用于Engadget的Feed时,其日期格式类似于Thu,2009年9月24日17:04:00 EST抛出FormatException。

我在这里缺少一些直接的东西吗?

3 个答案:

答案 0 :(得分:5)

DateTime.Parse不理解EST。它只能理解字符串末尾的GMT。

标准日期和时间格式字符串链接: http://msdn.microsoft.com/en-us/library/az4se3k1.aspx

这是一个SO链接,帮助...... EST等无法识别。您必须将它们转换为时间偏移量:

Parse DateTime with time zone of form PST/CEST/UTC/etc

答案 1 :(得分:5)

在RSS Feed中解析日期非常令人沮丧。我遇到了一个名为Argotic Syndication Framework on CodePlex的精彩免费图书馆。它像冠军一样工作,也支持ATOM提要。从Feed中返回一个漂亮的小数据集,包括标准日期。

答案 2 :(得分:1)

刚写过这篇文章,其他人可能会发现它很有用。

/// <summary>
/// Converts c# DateTime object into pubdate format for RSS
/// Desired format: Mon, 28 Mar 2011 02:51:23 -0700
/// </summary>
/// <param name="Date">DateTime object to parse</param>
/// <returns>Formatted string in correct format</returns>
public static string PubDate(DateTime Date)
{
    string ReturnString = Date.DayOfWeek.ToString().Substring(0,3) + ", ";
    ReturnString += Date.Day + " ";
    ReturnString += CultureInfo.CurrentCulture.DateTimeFormat.GetAbbreviatedMonthName(Date.Month) + " ";
    ReturnString += Date.Year + " ";
    ReturnString += Date.TimeOfDay + " -0700";

    return ReturnString;
}
相关问题