LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 161
Default Efficient standard to military time

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




 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Converting Standard Time to Military Time in Excel mtvschultz Excel Discussion (Misc queries) 3 May 5th 23 11:42 AM
Converting standard time to military time C. Excel Worksheet Functions 9 June 1st 17 10:06 PM
formula for converting military time to standard time, etc Pattio Excel Discussion (Misc queries) 8 February 17th 08 01:12 AM
How to I convert standard time to Military or 24 hour format? Nacho Excel Discussion (Misc queries) 5 June 28th 06 07:14 PM
Convert data into standard military time format geog Excel Discussion (Misc queries) 2 December 12th 05 07:46 PM


All times are GMT +1. The time now is 12:01 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"