Link to home
Start Free TrialLog in
Avatar of VinnyG3
VinnyG3Flag for United States of America

asked on

Military time converting

I am trying to make a program in VB that will convert military time to standard time. I have a simple GUI that has a place to input military time then another text box to show the standard time, then a button to press to do the conversion. I have no idea how to do the code, can anyone help?

Thank you,

Tyler
Avatar of SStory
SStory
Flag of United States of America image

It is very easy..  You just use the Format function.  
Do you mean the 24 hour clock?

You only need to change the Format to display in your chosen style.

This will show in 24 hour format.

TextBox2.Text = Format(CDate(TextBox1.Text), "hh:mm")

This will show in 12 hour format

TextBox2.Text = Format(CDate(TextBox1.Text, "hh:mm AM/PM"))
This should give you an idea:

        MsgBox(Format("HHmmssa", CDate("23:45:22")))
Of course replace the "23:25:22", with the string from your textbox...

Example:
txtStdTime.text=Format("HHmmssa", CDate(txtMilTime.Text))
Avatar of VinnyG3

ASKER

I tried the code below and im getting an error. Am i doing something wrong?
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        TextBox2.Text = Format("HHmmssa", CDate(TextBox1.Text))
 
 
 
    End Sub

Open in new window

Most of us probably assumed you were using tradidtional VB as opposed to .Net.

What is the error?
Avatar of VinnyG3

ASKER

InvalidCastException was unhandled
Conversion from string "22.35.03" to type 'Date' is not valid.

Yes i am using VB express 2005
ASKER CERTIFIED SOLUTION
Avatar of SStory
SStory
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of VinnyG3

ASKER

Thank you very much
You are welcome.  I'd also suggest validating user input to allow only numbers 0-9 and two ":" in the proper places before trying to convert it.