R – VB date conversion


Is there an easy way to convert a string that contains this:

Date: Wed, 5 Nov 2008 13:12:12 -0500 (EST)

into a string that contains this:


I ended up using date.tryparse which is similar to tryParseExact except you don't have to specify the format string. I did have to eliminate the () and the EST for this to work. The date string will always be EST because the date string comes from 1 web server.

Original string:

Date: Wed, 5 Nov 2008 13:12:12 -0500 (EST)

Using this code:

buff1.Remove(0, 6).Replace("(", "").Replace(")", "").Replace("EST", "").Trim()

Becomes this string:

Wed, 5 Nov 2008 13:12:12 -0500

Then I can format appropriately to generate my filename date using this:

 If Date.TryParse(buff1, dateValue) Then
   MsgBox(Format(dateValue, "yyyyMMdd_HHmmss"))
 End If

Best Solution

Even better than Date.Parse in this case would be Date.TryParseExact(). That would let you tell the framework what format you expect and return a boolean rather than throwing an exception if the parse fails.

Then use .ToString("yyyyMMdd_HHmmss") to get the desired new string format.

Here's the format string reference, in case you need it:

Finally, I noticed you're ignoring the -500 timezone offset. Are you sure that all your strings are really from the same time zone?