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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Time Conversion

Status
Not open for further replies.

tmishue

Programmer
Jun 26, 2001
35
0
0
US
Can anyone help me?

I have a form in which users enter a time in timetextbx. I have an option box to select AM or PM. I need to convert the value in the timetextbx to military time if PM is selected then conver it into DateTime format using the CDate function.

Does anyone have any suggestions?

Thanks tbmishue
 
This should work for u:

Option Explicit

Private Sub Command1_Click()
Dim MilTime As String
Dim CivTime As String

CivTime = Text1.Text

MilTime = CivToMil(CivTime)

MsgBox MilTime


End Sub

Private Function CivToMil(CivTime As String) As String
'assumes CivTime is standard VB time format(i.e. "1:13:01 PM" or "10:56:32 AM"
'also assumes CivTime is a legal time. If you tell this function that it's
'24 o'clock, it'll tell you the military time is 36:00

Dim res() As String
Dim rtnResult As String
Dim MilHour As Integer
Dim a As Integer

res = Split(CivTime, ":")

If InStr(1, res(UBound(res)), "AM") Then 'it's morning, remove " AM"
CivToMil = Left$(CivTime, Len(CivTime) - 3)
Exit Function
End If

MilHour = res(LBound(res)) + 12 'add 12 to the hour
If MilHour = 24 Then MilHour = 0 '24:00 = 00:00

For a = (LBound(res) + 1) To UBound(res)
rtnResult = rtnResult & ":" & res(a)
Next a

rtnResult = MilHour & rtnResult

CivToMil = Left$(rtnResult, Len(rtnResult) - 3) 'remove " PM"

End Function

That should get u started, good luck!

-Mike

Difference between a madman and a genius:
A madman uses his genius destructively,
A genius uses his madness constructively.
 
Text1 = FormatDateTime(Now, vbShortTime) David Paulson

 
Or you use the format function allthough a sure way of doing it correct will not be with h:m:s (allthough it works it could cause problems). The best way is this:

Dim harm As String
harm = "17-08-1999 08:15 PM"
MsgBox Format(CDate(harm), "Medium Time") ' gives a PM AM time
MsgBox Format(CDate(harm), "Short Time") ' gives a 24 hour time
 
I do not know what kind of problems, I used to use the h:m or hh:mm but somebody told me the other way is better because the hh:mm could cause a problem (I never had a problem) but Short Time works fine so I stick with that one.
 
I have been using it for years without problems. I am not trying to be rude, so please do not take it that way, but if "someone" told you that it could cause problem, and they, nor you had any reason to think that, which is a worse practice? Using a proven technique, that is more versitile than a VB constant, or telling others not to use techniques that are proven based on nothing. What happens down the road when tbmishue needs to do something that is not provided in a constant. tbmishue may waste time trying to find a way around it based on your post.
 
woyler is right, using h:m offers more flex and I cannot produce an example where it goes wrong. It`s just when I have to use the date spesificly in an 24h format I allways use the constant.
Converting the time the other way (using Medium Time) the only way (I know of) of converting a time to PM AM.
h:m allways returns a 24 hour based time.
 
Thanks for the fast responses and all your help. I got it working.


tbmishue

;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top