Solved

How to convert to a 24 hour clock in vb 2005

Posted on 2011-09-21
15
221 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
  • 8
  • 7
15 Comments
 
LVL 85

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 85

Expert Comment

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

Author Comment

by:cmdolcet
ID: 36574484
0
 
LVL 85

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 85

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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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 85

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 85

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 85

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 85

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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 …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

707 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now