Solved

How to convert to a 24 hour clock in vb 2005

Posted on 2011-09-21
15
228 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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
 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Exit the loop 4 49
vb.net help 4 34
DataGridView / get bound table name? 8 24
ASP.NET/VB: Convert Date and Time to YYYY-MM-DDTHH:MM:SS 3 10
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

856 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