Tim Hibbard

CEO for EnGraph software
posts - 629 , comments - 1682 , trackbacks - 459

My Links

News



Add to Google

Twitter












Tag Cloud

Article Categories

Archives

Post Categories

Image Galleries

EnGraph Blogs

Links

Other

Roll

C# / VB.NET function to generate pubDate from DateTime

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

 

Print | posted on Sunday, November 5, 2006 1:34 PM |

Feedback

Gravatar

# re: C# / VB.NET function to generate pubDate from DateTime

thanks a ton! :)
12/11/2006 9:25 AM | Marc
Gravatar

# re: C# / VB.NET function to generate pubDate from DateTime

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.
12/15/2008 2:55 PM | Big-O
Gravatar

# re: C# / VB.NET function to generate pubDate from DateTime

Thanks' this was awesome, saved me time not having to write this function myself! :)
9/25/2009 2:18 AM | Meek
Gravatar

# re: C# / VB.NET function to generate pubDate from DateTime

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.
12/21/2009 10:36 AM | Soumyabrata Paul
Gravatar

# re: C# / VB.NET function to generate pubDate from DateTime

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?
9/21/2011 12:00 AM | newbie
Gravatar

# re: C# / VB.NET function to generate pubDate from DateTime

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")
3/14/2012 2:14 PM | Ben
Post A Comment
Title:
Name:
Email:
Comment:
Verification:
 
 

Powered by: