views:

849

answers:

6
+2  Q: 

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:

20081105_131212

UPDATE:
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 appropriatly to generate my filename date using this:

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

Thanks for the suggestions. SO Rules!

A: 

Format(date, "yyyyMMdd_HHmmss")

More help on format function.

Salman Kasbati
Those should be upper case H's.
Joel Coehoorn
Also, this would work with a date object, but the question is relating to a specifically formatted string with extra info that needs cleaned out.
Kevin Fairchild
A: 

If by VB you mean VB.NET you could use Date.Parse followed by ToString() with a format string:

Date.Parse(YourDateString).ToString("yyyyMMdd_HHmmss")

Note: Remove the initial "Date: " before you parse the string.

splattne
+5  A: 

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:
http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

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

Joel Coehoorn
A: 

Like @splattne's solution in VB.NET, but with the cleanup as well...

Dim strDateVal As String = "Date: Wed, 5 Nov 2008 13:12:12 -0500 (EST)"
strDateVal = strDateVal.Substring(strDateVal.IndexOf(", ") + 2, strDateVal.Length - strDateVal.IndexOf(", ") - 2)
strDateVal = strDateVal.Substring(0, strDateVal.LastIndexOf(" ")).TrimEnd
Dim DateVal As Date = Date.Parse(strDateVal)
Dim NewStringVal As String = Format(DateVal, "yyyyMMdd_HHmmss")

NOTE: This ignores the timezone in order to match your expected result (per the example data in the question)

Kevin Fairchild
+1  A: 

For a pure VB solution I would go

Function ConvertDateString(ByVal Original As String) As String
    Dim Elements As String() = Split(Original, " ")
    Dim DateString As String = Elements(3) & " " & Elements(2) & " " & Elements(4) & " " & Elements(5)
    Return Date.Parse(DateString).ToString("yyyyMMdd_HHmmsss")
End Function

You could eliminate DateString by just using the concatenated string in the Parase. It will fit on one line if your resolution is 1024 by 768 or bigger.

RS Conley
A: 

Dim strDateVal As String = "Date: Wed, 5 Nov 2008 13:12:12 -0500 (EST)" strDateVal = strDateVal.Substring(strDateVal.IndexOf(", ") + 2, strDateVal.Length - strDateVal.IndexOf(", ") - 2) strDateVal = strDateVal.Substring(0, strDateVal.LastIndexOf(" ")).TrimEnd Dim DateVal As Date = Date.Parse(strDateVal) Dim NewStringVal As String = Format(DateVal, "ddMMyyyy_HHmmss")