TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# pubDate RSS Format

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

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.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf