Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

need help parsing a date out of a comment 1

Status
Not open for further replies.

DougP

MIS
Dec 13, 1999
5,985
US
I wrote a "comment" reader which interrogates every file in a .NET project folder and retrieves anything that starts with a single quote.
Often times there is a date like the following below. But the date is not always in a complete date format. Some have the time added, some don’t. They will always usually have month, day, year like so:
I need to be able to parse the date out if id exists but also eliminate the initials
The time would be nice to have but not necessary.
this code only gets a date if it matches this: 1-9-13 2:30pm DBP
Is there a way to get the date out of this consistently?
a few samples:
‘ 1-4-13 DBP
' 12-13-12 8:27am DBP
' 12-12-12 2:04pm DBP Remarked out Me...
‘ 4-15-13 DBP added a year to date button...

Code:
If InStr(1, line, "'") Then
If InStr(1, line, "------------------------------------") = 0 Then
  Comment = Trim(Replace(line, "'", " "))
If Val(Comment) > 0 Then
     testDate = Microsoft.VisualBasic.Left(Comment, 15)
              For a = 1 To Len(testDate)
      GoodDate = GoodDate & Mid(testDate, a, 1)
          If IsDate(GoodDate) Then
      DateOfComment = CDate(GoodDate)
   Exit For
End If

        Next
  Else
          DateOfComment = CDate(#12:00:00 AM#)
     End If


DougP
 
hi,

Something like this, although I'm not at all certain of the .net syntax...
Code:
Function CommentDate(comment As String)
    
    CommentDate = Split(comment, " ")(0)
    
    If IsDate(DateValue(CommentDate)) Then
        CommentDate = DateValue(CommentDate)
    Else
        CommentDate = 0
    End If
End Function

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 

This looks like a job for Regular Expressions!

This function takes a string input and returns blank if no date is found, the date if just a date is found or the date and time if both are found:

Code:
    Private Function ParseDateTime(ByVal InpputString As String) As String

        ' Note: add Imports System.Text.RegularExpressions to access Regex

        Dim sRetVal As String = ""

        Dim rgx As Regex
        Dim m As Match
        Dim TimeFound As Boolean = False

        rgx = New Regex("\d{1,2}\-\d{1,2}\-\d{1,2}\ \d{1,2}\:\d{1,2}[ap]m")

        If rgx.IsMatch(InpputString) Then
            m = rgx.Match(InpputString)
            sRetVal = m.Value
            TimeFound = True
        End If

        If Not TimeFound Then

            rgx = New Regex("\d{1,2}\-\d{1,2}\-\d{1,2}")

            If rgx.IsMatch(InpputString) Then
                m = rgx.Match(InpputString)
                sRetVal = m.Value
            End If

        End If

        Return sRetVal

    End Function

I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
jebenson, works fantastic, have a STAR


DougP
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top