Solved

How to convert to a 24 hour clock in vb 2005

Posted on 2011-09-21
15
237 Views
Last Modified: 2012-05-12
How can I convert a time using the code below to a 24 hour clock

Thanks in Advance
Private Sub Set_Date()
        Dim Timearr() As String
        Dim Datearr() As String
        Dim TwentyFour_HourTime As String
        DateTimeArr = Me.lblDisplayDate_Time.Text.Split(New Char() {Chr(32)}, StringSplitOptions.RemoveEmptyEntries)
        Datearr = DateTimeArr(0).Split(New Char() {Chr(47)}, StringSplitOptions.RemoveEmptyEntries)
        Datearr(2) = Replace(Datearr(2), "20", "")
        Timearr = DateTimeArr(1).Split(New Char() {Chr(58)}, StringSplitOptions.RemoveEmptyEntries)
        TwentyFour_HourTime = Timearr(0) & ":" & Timearr(1) & ":" & Timearr(2)
        TwentyFour_HourTime = Format(CDate(TwentyFour_HourTime), "HH:mm")
end sub

Open in new window

0
Comment
Question by:cmdolcet
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 7
15 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 36574382
I assume that doesn't work?...

It would be helpful to know what is in the "lblDisplayDate_Time" Label and what output you're getting in the "TwentyFour_HourTime" String!   =)
0
 

Author Comment

by:cmdolcet
ID: 36574437
Oh sorry the lblDisplayDate is a text that has both the date and time in it. I parse out the date and time into seperate arrays.

As for what theTwenty_four_HourTime it returns 11:20 or whatever time is currently on the PC. If I advance the time on the system clock to 1:20 I would like it to return 13:20 however it returns 1:20
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 36574450
Can you show an example of what is in the Label?  What code populates it?
0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

Author Comment

by:cmdolcet
ID: 36574484
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 36574511
But what code put the original value into your Label?
0
 

Author Comment

by:cmdolcet
ID: 36574540
DateTimeArr = Me.lblDisplayDate_Time.Text.Split(New Char() {Chr(32)}, StringSplitOptions.RemoveEmptyEntries)

"9/21/2011 11:34.33 AM"

0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 36574564
No, no sir.

In that line of code, you are accessing the "lblDisplayDate_Time" Label.

What code put that value into that Label in the first place?!

Why parse and reformat a static string when you could just format it correctly from the beginning?

Where does the 24 hour formatted string need to go?
0
 

Author Comment

by:cmdolcet
ID: 36574575
I need it to go back into the Timearr(1) which is the minute in this case and Timearr(2) which is the hour.

0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 36574584
...but Timearr() is a local variable in that sub.  It will go away.  What are you ultimately going to do with this 24 hr value?

Give me the big picture.

I feel you're working waaaaay to hard to get this value and I'm trying to make life easier for ya!  =)
0
 

Author Comment

by:cmdolcet
ID: 36574607
I am adding it to the Date_Time string then that string will be sent to a device. I do all the formatting because the device brings the Date and Time over together then it wants it in a certain format. As long as I an get the 24 clock for the Time im all set why isn;t the HH:mm working?
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 36574660
Create a DateTime from your string, then use ToString() to get it in the format you want:
Private Sub Set_Date()
        Dim dt As DateTime
        If DateTime.TryParse(Me.lblDisplayDate_Time.Text, dt) Then
            Dim str24Hr As String = dt.ToString("HH:mm")

            ' ... do something with "str24Hr" ...
            MessageBox.Show(str24Hr)
        Else
            MessageBox.Show("Invalid DateTime")
        End If
    End Sub

Open in new window

0
 

Author Comment

by:cmdolcet
ID: 36575607
Can I use my code instead??? It looks like the same process is being taken..... Why doesn't the HH:mm work correctly.
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 36575711
Your still doing way to much work.

Why are you splitting it all up just to put it back together?  Pretty sure you have some logic mistakes in there...

This is all your code boils down to:  *CDate() is just a different way of doing DateTime.TryParse() as in my post*
Private Sub Set_Date()
        Dim TwentyFour_HourTime As String = Format(CDate(Me.lblDisplayDate_Time.Text), "HH:mm")
        Debug.Print(TwentyFour_HourTime)
    End Sub

Open in new window

0
 

Author Comment

by:cmdolcet
ID: 36576115
How can I add the return value I got for the TwentyFour_HourTime and place it in a string array  I need to break it apart and send it in a specific order
Timearr = DateTimeArr(1).Split(New Char() {Chr(58)}, StringSplitOptions.RemoveEmptyEntries)
        Date_Time = "," & Timearr(1) & "," & Timearr(0) & "," & Datearr(1) & "," & "5" & "," & Datearr(0) & "," & Datearr(2)

Open in new window

0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 36576184
First, convert it to a DateTime.  Then you can use the ToString() method and give it your desired format.

I think you want?    minutes,24_hours,day,month,2_digit_year

        Dim dt As DateTime = DateTime.Parse(Me.lblDisplayDate_Time.Text)
        Dim Date_Time As String = dt.ToString(",mm,HH,dd,MM,yy")
        Debug.Print(Date_Time)


0

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question