C# pubDate RSS Format

RSS

What is the best way to format the date you put in the pubDate element in an RSS feed that is generated with the C# language? It is possible to generate RSS feeds using C# programs, but somewhat unintuitive to format the date correctly so it works in most RSS readers.

Format pubDate

First, every RSS feed should use the pubDate element 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. In this example, I show that you can pass the argument "o" to the ToString method on your DateTime instance to create a compatible string.

This C# example code generates RSS format dates. It writes pubDate elements.

Program that writes pubDate element [C#]

using System;

class Program
{
    static void Main()
    {
	// Format pubDate string.
	string result = "<pubDate>" +
	    DateTime.Now.ToString("o") +
	    "</pubDate>";
	Console.WriteLine(result);
    }
}

Output

<pubDate>2010-06-05T14:23:07.4491347-06:00</pubDate>

Does it work? The above example code works correctly in all RSS feed readers I have tested. If you use a format that is ambiguous in international situations, you will have problems; also, you seem to need the time zone part (-06.00) to ensure the readers deal with the date correctly.

DateTime Format

Note

Note

Although I do not use RSS at all myself, I have an RSS feed that I generate for Dot Net Perls. Unfortunately, the pubDate format I used was incorrect and this caused problems for some who do use RSS. Fortunately, Stefano Leardini wrote in with a suggested pubDate format and this corrected the issue, when I added code similar to that shown above. Dot Net Perls thanks 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. It is important that an unambiguous format be used, and that it includes time zone information.

Time Representations
.NET