• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1615
  • Last Modified:

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
0
VinnyG3
Asked:
VinnyG3
  • 5
  • 3
  • 2
1 Solution
 
SStoryCommented:
It is very easy..  You just use the Format function.  
0
 
GrahamSkanCommented:
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"))
0
 
SStoryCommented:
This should give you an idea:

        MsgBox(Format("HHmmssa", CDate("23:45:22")))
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
SStoryCommented:
Of course replace the "23:25:22", with the string from your textbox...

Example:
txtStdTime.text=Format("HHmmssa", CDate(txtMilTime.Text))
0
 
VinnyG3Author Commented:
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

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

What is the error?
0
 
VinnyG3Author Commented:
InvalidCastException was unhandled
Conversion from string "22.35.03" to type 'Date' is not valid.

Yes i am using VB express 2005
0
 
SStoryCommented:
try using 22:35:03 instead

if you must use periods then write a routine to replace all periods with ":" because that is what Format expects.
0
 
VinnyG3Author Commented:
Thank you very much
0
 
SStoryCommented:
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.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 5
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now