Home |
Search |
Today's Posts |
|
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
For my program, valid standard time formats look like this:
1:59 am 159am 159 AM 1:59 PM I created the following code to convert the above types of formats into military times. Does anyone know if this code can be improved or simplified? I prefer to do time conversions as quickly as possible. Maybe someone has a better technique than mine? Here is the code: '---------------------------------------------------------- Dim reg As New VBScript_RegExp_55.RegExp Dim sentence As String Dim tHours As String Dim tMins As String Dim tMeridiem As String Dim militaryHour As String Dim match, matches sentence = "6:59 pm" ' Example input time reg.Pattern = "^\s*(1[012]|[1-9]):?([0-5][0-9])\s*(am|pm)$" reg.IgnoreCase = True reg.Global = True ' Check if format is valid If reg.Test(sentence) Then Set matches = reg.Execute(sentence) Set match = matches(0) tHours = match.SubMatches(0) tMins = match.SubMatches(1) tMeridiem = match.SubMatches(2) If (tHours = "12") Then ' Handle midnight and noon cases If (tMeridiem = "am") Then militaryHour = "00" Else militaryHour = "12" End If Else If (tMeridiem = "am") Then militaryHour = tHours Else militaryHour = CStr(Val(tHours) + 12) End If If (Val(militaryHour) < 10) Then militaryHour = "0" + militaryHour End If End If MsgBox "Military time: " + militaryHour + tMins Else MsgBox "Invalid format time string" End If |
#2
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Robert Crandal wrote:
For my program, valid standard time formats look like this: 1:59 am 159am 159 AM 1:59 PM I created the following code to convert the above types of formats into military times. Does anyone know if this code can be improved or simplified? I prefer to do time conversions as quickly as possible. Maybe someone has a better technique than mine? Here is the code: [snip] I'm not very good with regex's so I can't comment on that part... but your code for the time translation can be seriously simplified. VBA can format times into military time using Format(source$, "HHmm"). Try this: Dim reg As New VBScript_RegExp_55.RegExp Dim sentence As String Dim tResult As String Dim match, matches sentence = "6:59 pm" ' Example input time reg.Pattern = "^\s*(1[012]|[1-9]):?([0-5][0-9])\s*(am|pm)$" reg.IgnoreCase = True reg.Global = True ' Check if format is valid If reg.Test(sentence) Then Set matches = reg.Execute(sentence) Set match = matches(0) tResult = Format$(CDate(match.SubMatches(0) & ":" & _ match.SubMatches(1) & _ match.SubMatches(2)), "HHmm") MsgBox "Military time: " & tResult Else MsgBox "Invalid format time string" End If -- Omnia mutantur, nihil interit. (All things change, but nothing is truly lost.) |
#3
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
The slowest aspect to your routine would be creating the RegExp object,
particularly first time though you could maintain it in scope and reuse it. As written it your code would return an incorrect time with "AM", try lCase(tMeridiem) = "am" There are no doubt several other ways, eg Sub test() Dim arr, v arr = Array("1:59 am", "159 AM", _ "159 AM", " 1:59 PM") For Each v In arr Debug.Print strDate2MilTime(v) Next End Sub Function Date2MilTime(vTime As Variant) As String Dim s As String Dim sNum As String On Error Resume Next Date2MilTime = Format(CDate(vTime), "hhmm") If Err Then On Error GoTo errExit s = Replace(vTime, " ", "") If InStr(2, s, ":") = 0 Then sNum = Val(s) s = ":" & Right$(sNum, 2) & Mid$(s, Len(sNum) + 1, 2) s = Left$(sNum, IIf(Len(sNum) = 3, 1, 2)) & s End If Date2MilTime = Format(CDate(s), "hhmm") End If Exit Function errExit: Date2MilTime = "Error" End Function caveat, only tested with the given examples Regards, Peter T "Robert Crandal" wrote in message ... For my program, valid standard time formats look like this: 1:59 am 159am 159 AM 1:59 PM I created the following code to convert the above types of formats into military times. Does anyone know if this code can be improved or simplified? I prefer to do time conversions as quickly as possible. Maybe someone has a better technique than mine? Here is the code: '---------------------------------------------------------- Dim reg As New VBScript_RegExp_55.RegExp Dim sentence As String Dim tHours As String Dim tMins As String Dim tMeridiem As String Dim militaryHour As String Dim match, matches sentence = "6:59 pm" ' Example input time reg.Pattern = "^\s*(1[012]|[1-9]):?([0-5][0-9])\s*(am|pm)$" reg.IgnoreCase = True reg.Global = True ' Check if format is valid If reg.Test(sentence) Then Set matches = reg.Execute(sentence) Set match = matches(0) tHours = match.SubMatches(0) tMins = match.SubMatches(1) tMeridiem = match.SubMatches(2) If (tHours = "12") Then ' Handle midnight and noon cases If (tMeridiem = "am") Then militaryHour = "00" Else militaryHour = "12" End If Else If (tMeridiem = "am") Then militaryHour = tHours Else militaryHour = CStr(Val(tHours) + 12) End If If (Val(militaryHour) < 10) Then militaryHour = "0" + militaryHour End If End If MsgBox "Military time: " + militaryHour + tMins Else MsgBox "Invalid format time string" End If |
#4
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
"Peter T" wrote:
The slowest aspect to your routine would be creating the RegExp object, particularly first time though you could maintain it in scope and reuse it. As written it your code would return an incorrect time with "AM", try lCase(tMeridiem) = "am" Wow, I don't know how I missed that "AM" error. I thought because I set IgnoreCase equal to True that it would have worked for both "am" and "AM" cases. Guess I was wrong. There are no doubt several other ways, eg Sub test() Dim arr, v arr = Array("1:59 am", "159 AM", _ "159 AM", " 1:59 PM") For Each v In arr Debug.Print strDate2MilTime(v) Next End Sub Function Date2MilTime(vTime As Variant) As String Dim s As String Dim sNum As String On Error Resume Next Date2MilTime = Format(CDate(vTime), "hhmm") If Err Then On Error GoTo errExit s = Replace(vTime, " ", "") If InStr(2, s, ":") = 0 Then sNum = Val(s) s = ":" & Right$(sNum, 2) & Mid$(s, Len(sNum) + 1, 2) s = Left$(sNum, IIf(Len(sNum) = 3, 1, 2)) & s End If Date2MilTime = Format(CDate(s), "hhmm") End If Exit Function errExit: Date2MilTime = "Error" End Function caveat, only tested with the given examples Nice alternative solution. I wonder if this will be faster than the previous solution? |
#5
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
"Robert Crandal" wrote in message
Nice alternative solution. I wonder if this will be faster than the previous solution? I did test before posting (because you said speed was important) and found my alternative was about twice as fast as the RegExp, and that was maintaining and reusing the RegExp object. That said if only calling a few times the difference would be negligible. However when not reusing but recreating the RegExp object each time it was tens of times faster, as expected. Regards, Peter T |
#6
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]() "Robert Crandal" wrote in message As written it your code would return an incorrect time with "AM", try lCase(tMeridiem) = "am" I thought because I set IgnoreCase equal to True that it would have worked for both "am" and "AM" cases. Guess I was wrong. IgnoreCase makes matching against the pattern insensitive, however the SubMatches will return the matched element of the input string in its original case. Regards, Peter T |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Converting Standard Time to Military Time in Excel | Excel Discussion (Misc queries) | |||
Converting standard time to military time | Excel Worksheet Functions | |||
formula for converting military time to standard time, etc | Excel Discussion (Misc queries) | |||
How to I convert standard time to Military or 24 hour format? | Excel Discussion (Misc queries) | |||
Convert data into standard military time format | Excel Discussion (Misc queries) |