TheDeveloperBlog.com


C# pubDate RSS Format

PubDate, RSS. The pubDate RSS element requires a certain date format. We can generate this in a C# program. It is somewhat unintuitive to format the date correctly so it works in most RSS readers.


Example. First, every RSS feed should use the pubDate element. It is nested inside every item element. Inside the pubDate element, you need to use a format that has both a time zone and is useful for other countries.

Next: We pass a format string argument to the ToString method on DateTime. We call ToString again and replace a colon char.

Based on:

.NET 4.5

C# program that writes pubDate element

using System;

class Program
{
    static void Main()
    {
	Console.WriteLine(DateString(DateTime.Now));
    }

    /// <summary>
    /// DateString.
    /// </summary>
    static string DateString(DateTime pubDate)
    {
	var value = pubDate.ToString("ddd',' d MMM yyyy HH':'mm':'ss") +
	    " " +
	    pubDate.ToString("zzzz").Replace(":", "");
	return value;
    }
}

Output

Sun, 13 Apr 2014 06:08:54 -0700


Discussion. This code works in all RSS feed readers I tested. If you use a format that is ambiguous in international situations, you will have problems. Also, you need the time zone part (-06.00) to ensure the readers deal correctly with the date.

I rarely use RSS myself. But I have an RSS feed that I generate for this site. Unfortunately the pubDate format I used was incorrect and this caused problems for some who do use RSS.

Note: Stefano Leardini wrote in with a suggested pubDate format. This corrected the issue. I thank all contributors to the site.


Summary. The pubDate element on an RSS feed is an important one because it helps establish a temporal pattern to your posting habits. Further, it helps clients such as Outlook sort posts correctly.

So: It is important that an unambiguous format be used, and that it includes time zone information.