Aidan Polivka
Aidan Polivka

Reputation: 21

Parsing unknown date fromat from String

Is there a way to programmatically find the Date/Time format of a string in Visual Basic?

My example comes from work. I was working on an Custom Web Page to compare tables on Excel Spreadsheets . My comparison of the records on each table is based off of last four digits of social security and birthdate, however currently the format of the birthdate has to be the same on both spreadsheets for it to run properly. The user wanted to compare early before we had updated the reports for this project, and I told her to make sure the birthday columns were in the same datetime format before she ran the web page. She did not.

I have two options, hard code the date time format that currently exists on the spreadsheets, or find a way to make the format irrelevant. I think it would be more fun to do the latter.

I know that there is a Date.ParseExact method, but that requires that you know the format of the string already.

To give you a better idea of what I want to do:

Dim Birthday1 As String = "04/15/1998 13:00:00"
Dim Birthday2 As String = "04/15/1998 1:00 PM"

'//Converts to Date in format "M/d/yyyy"
Dim dateBirthday1 As Date = ConvertFromAnyDateFormat(Birthday1)
Dim dateBirthday2 As Date = ConvertFromAnyDateFormat(Birthday2)

If dateBirthday1 = dateBirthday2 Then
     '//Do comparison operations or whatever
End If

Upvotes: 0

Views: 53

Answers (1)

Albert D. Kallal
Albert D. Kallal

Reputation: 49039

In your case, the strings are in good format. So just allow the system to cast the values

This will work for your two dates.

Imports System.Globalization

Public Class MyFunExample
    
   Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

      Dim Birthday1 As String = "04/15/1998 13:00:00"
      Dim Birthday2 As String = "04/15/1998 1:00 PM"
      Dim Birthday3 As String = "04/15/98 1:00 PM"
      Dim Birthday4 As String = "04/15/98"

      '//Converts to Date in format "M/d/yyyy"


      Dim dtBir1 As DateTime = Birthday1
      Dim dtBir2 As DateTime = Birthday2
      Dim dtBir3 As DateTime = MyDateCheck(Birthday3)
      Dim dtBir4 As DateTime = MyDateCheck(Birthday4)

      Debug.Print(dtBir1)
      Debug.Print(dtBir2)
      Debug.Print(dtBir3)
      Debug.Print(dtBir4)

     if dtBir1 = dtBir2 then
     End if

End Sub

Function MyDateCheck(sDate As String) As DateTime

    If IsDate(sDate) Then
        Return sDate
    End If

    Dim rFoundOne As DateTime
    ' if we get here, then lets try some other formats
    If DateTime.TryParse(sDate, CultureInfo.InvariantCulture, DateTimeStyles.AllowInnerWhite, rFoundOne) Then
        Return rFoundOne
    End If

    ' try additonal messy cases

    If DateTime.TryParseExact(sDate, "mm dd yy", CultureInfo.InvariantCulture, DateTimeStyles.AllowInnerWhite, rFoundOne) Then
        Return rFoundOne
    End If

    ' if all fail, then we return a "0" date
    Return DateTime.MinValue

End Function

Output:

1998-04-15 1:00:00 PM
1998-04-15 1:00:00 PM
1998-04-15 1:00:00 PM
1998-04-15

So if things are messed up - then you could introduce the date check routine, but in your case you just need to shove the strings into a datetime, and thus they are then considered NOT a string, but a internal date format number, and thus the compares will work.

Upvotes: 1

Related Questions