Geeks With Blogs

News



Add to Google

Tim Hibbard CEO for EnGraph software

According to the RSS specification, the pubDate element (when the item was published) must be formatted like this : Mon, 28 May 1979 19:45:32 GMT

Here is a function that will accept a DateTime object and return a string formatted properly for pubDate:

C#

private string BuildPubDate(DateTime d) { try { string RV = ""; string day = d.Day.ToString(); if (day.Length == 1) { day = "0" + day; } string month = d.Month.ToString(); if (month == "1") { month = "January"; } else if (month == "2") { month = "February"; } else if (month == "3") { month = "March"; } else if (month == "4") { month = "April"; } else if (month == "5") { month = "May"; } else if (month == "6") { month = "June"; } else if (month == "7") { month = "July"; } else if (month == "8") { month = "August"; } else if (month == "9") { month = "September"; } else if (month == "10") { month = "October"; } else if (month == "11") { month = "November"; } else if (month == "12") { month = "December"; } string mTime = ""; DateTime mDate = d.ToUniversalTime(); if (mDate.Hour.ToString().Length == 1) { mTime = "0" + mDate.Hour.ToString(); } else { mTime = mDate.Hour.ToString(); } mTime += ":"; if (mDate.Minute.ToString().Length == 1) { mTime += "0" + mDate.Minute.ToString(); } else { mTime += mDate.Minute.ToString(); } mTime += ":"; if (mDate.Second.ToString().Length == 1) { mTime += "0" + mDate.Second.ToString(); } else { mTime += mDate.Second.ToString(); } RV = d.DayOfWeek.ToString().Substring(0, 3); RV += ", " + day + " " + month.Substring(0, 3); RV += " " + d.Year.ToString() + " " + mTime + " GMT"; return RV; } catch (Exception) { return null; } }

 

VB.NET

 

Private Function BuildPubDate(ByVal d As Date) As String Try Dim RV As String Dim day As String = d.Day.ToString If day.Length = 1 Then day = "0" & day Dim month As String = d.Month.ToString Select Case month Case "1" month = "January" Case "2" month = "February" Case "3" month = "March" Case "4" month = "April" Case "5" month = "May" Case "6" month = "June" Case "7" month = "July" Case "8" month = "August" Case "9" month = "September" Case "10" month = "October" Case "11" month = "November" Case "12" month = "December" End Select Dim Mtime As String = "" Dim mDate As Date = d.ToUniversalTime If mDate.Hour.ToString.Length = 1 Then Mtime = "0" & mDate.Hour.ToString Else Mtime = mDate.Hour.ToString End If Mtime += ":" If mDate.Minute.ToString.Length = 1 Then Mtime += "0" & mDate.Minute.ToString Else Mtime += mDate.Minute.ToString End If Mtime += ":" If mDate.Second.ToString.Length = 1 Then Mtime += "0" & mDate.Second.ToString Else Mtime += mDate.Second.ToString End If RV = d.DayOfWeek.ToString.Substring(0, 3) RV += ", " & day & " " & month.Substring(0, 3) RV += " " & d.Year.ToString & " " & Mtime & " GMT" Return RV Catch ex As Exception Return "" End Try End Function

 

Posted on Sunday, November 5, 2006 1:34 PM | Back to top


Comments on this post: C# / VB.NET function to generate pubDate from DateTime

# re: C# / VB.NET function to generate pubDate from DateTime
Requesting Gravatar...
thanks a ton! :)
Left by Marc on Dec 11, 2006 9:25 AM

# re: C# / VB.NET function to generate pubDate from DateTime
Requesting Gravatar...
I realize that this is an old post, but I thought I'd mention that this method is completely unnecessary. If you have a datetime object and you want a string that is formatted for an RSS pubDate field, all you have to do is this:

yourStringForThePubDate = yourDateObject.ToString("r");

One line of code. Done.
Left by Big-O on Dec 15, 2008 2:55 PM

# re: C# / VB.NET function to generate pubDate from DateTime
Requesting Gravatar...
Thanks' this was awesome, saved me time not having to write this function myself! :)
Left by Meek on Sep 25, 2009 2:18 AM

# re: C# / VB.NET function to generate pubDate from DateTime
Requesting Gravatar...
A very special thanks for this post. I have searched a lot but could hardly found.

This is really a great help for me. Thanks a lot once again.
Left by Soumyabrata Paul on Dec 21, 2009 10:36 AM

# re: C# / VB.NET function to generate pubDate from DateTime
Requesting Gravatar...
Trouble with using

yourDateObject.ToString("r");

It just converts your date format to required gmt format without actually taking the timezone into consideration. i.e If you are currently thursday 08 2011, 8:30 PM IST then it converts this into thursday 08 2011 20:30 GMT. But the problem is that when a user in IST subscribes to it, he will not get it as the IST time, instead the server converts the GMT to an IST time and this time will be different from the actual time. If you use the TIMEZONE object, newly introduced for conversion, even then some fault occurs w.r.t correct time . Any solution for this taking daylight also into consideration?
Left by newbie on Sep 21, 2011 12:00 AM

# re: C# / VB.NET function to generate pubDate from DateTime
Requesting Gravatar...
Regarding "r" format, is there a problem with the following or am I oversimplifying this?

Dim gmt As Date = [date].ToUniversalTime
Return gmt.ToString("r")
Left by Ben on Mar 14, 2012 2:14 PM

Your comment:
 (will show your gravatar)


Copyright © Tim Hibbard | Powered by: GeeksWithBlogs.net | Join free