Handling RFC 822 Date/Time in VB.NET

I’ve been working on a project that involved retrieving an RFC 822 Date/Time value in a string.  I needed to correctly convert that to a Date value, so I thought that would be very painless and off I went:

 

returnedDateString = “Tue, 13 May 2008 12:09:08 -0700 (PDT)”

myDate = returnedDateString.ToDate

 

Haha!  I wish…

 

Okay, next:

 

myDate  = CType(returnedDateString , Date)

 

Why the hell not?  Oh well.  NEXT!

 

myDate.FromString = …

 

I’m nothing if not optimistic.  Okay, research time.  Now, I’ve done far too much VB6 and VBA compared to VB.NET and it’s going to take me a few more years to get out of that mindset (I got my MCSD in 1999, after all).  Some of you may have figured this out already.

 

Anyway, I quickly narrowed it down to Date.Parse or Date.ParseExact or Date.TryParse or Date.TryParseExact.  The Parse methods don’t allow any kind of standard or custom date and time format strings, and they puke on this complicated example.  So the answer lies with TryParseExact (TryParse will throw an exception, TryParseExact won’t).

 

After looking at the different standard format strings, I thought I was in luck because “R” or “r’ is  the shortcut identifier for RFC1123 dates.  Oh.  That’s for this kind of pattern:

 

ddd, dd MMM yyyy HH’:’mm’:’ss ‘GMT’

 

Okay.  Looks like I have to build this format string myself.  Here’s the RFC 822 Date/Time spec from w3.org:

 

date-time   =  [ day “,” ] date time        ; dd mm yy
;  hh:mm:ss zzz

day         =  “Mon”  / “Tue” /  “Wed”  / “Thu”
/  “Fri”  / “Sat” /  “Sun”

date        =  1*2DIGIT month 2DIGIT        ; day month year
;  e.g. 20 Jun 82

month       =  “Jan”  /  “Feb” /  “Mar”  /  “Apr”
/  “May”  /  “Jun” /  “Jul”  /  “Aug”
/  “Sep”  /  “Oct” /  “Nov”  /  “Dec”

time        =  hour zone                    ; ANSI and Military

hour        =  2DIGIT “:” 2DIGIT [“:” 2DIGIT]
; 00:00:00 – 23:59:59

zone        =  “UT”  / “GMT”                ; Universal Time
; North American : UT
/  “EST” / “EDT”                ;  Eastern:  – 5/ – 4
/  “CST” / “CDT”                ;  Central:  – 6/ – 5
/  “MST” / “MDT”                ;  Mountain: – 7/ – 6
/  “PST” / “PDT”                ;  Pacific:  – 8/ – 7
/  1ALPHA                       ; Military: Z = UT;
;  A:-1; (J not used)
;  M:-12; N:+1; Y:+12
/ ( (“+” / “-“) 4DIGIT )        ; Local differential
;  hours+min. (HHMM)

 

To make a longer story thus far slightly shorter, I came up with this:

 

Date.TryParseExact(strDateNoGMT, “ddd, d MMM yyyy HH:mm:ss zzz”, Nothing, Globalization.DateTimeStyles.AssumeUniversal, myDate)

 

What’s strDateNoGMT?  Well, unless I’m mistaken, there are no custom date/time format strings that can handle the trailing GMT value, so I had to strip it out:

 

strDateNoGMT = returnedDateString .RemovereturnedDateString.IndexOf(“(“), 5).Trim

 

Hopefully this helps someone out, as I couldn’t find any examples.  Apparently RSS apps have to deal with RFC 822 Date/Time strings frequently, so maybe this will save some RSS dev out there who’s fighting over the same problem.

Eric Legault

Full-stack product builder & consultant for Microsoft 365 & Graph. Microsoft MVP 2003 - 2019. Outlook add-in guru. Rocker. Video/audio engineer. Collects Thors.

You may also like...

%d bloggers like this: