none
How to convert 12H format to 24H format

    Question

  • I would like to convert 12H to 24H format.
    I have for example: 06:11:11 PM and I need to convert it so I can see 18:11:11 in VB.net.
    Please help me here.

    V. A.
    Thursday, February 25, 2010 9:08 PM

Answers

  • just saw this one, and it's already answered, but I thought I'd add this:


                '24 hr clock
                Label2.Text = Format(Now(), "HH:mm:ss")
    
                'AM/PM clock
                Label2.Text = Format(Now(), "h:mm:ss tt")
    Thursday, February 25, 2010 10:49 PM
  • I would like to convert 12H to 24H format.
    I have for example: 06:11:11 PM and I need to convert it so I can see 18:11:11 in VB.net.
    Please help me here.

    V. A.

    Hi,

    Add one button and one large-ish RichTextBox to a FORM and run this please.

    This will show you all the possible DATE or TIME or Date and Time formats.  :-)


    The MessageBox will show the time in 24 Hr format if you use.>>


            Dim twentyFourHour As String = Now.Hour.ToString("00") & ":" & Now.Minute.ToString("00") & ":" & Now.Second.ToString("00")
    
            MessageBox.Show(twentyFourHour)



    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim myDateTime As DateTime = Now
    
            Dim formatStrings() As String = myDateTime.GetDateTimeFormats
    
            Dim sb As New System.Text.StringBuilder
            For index As Integer = 0 To formatStrings.GetUpperBound(0)
                sb.Append(formatStrings(index) & System.Environment.NewLine)
            Next
    
            RichTextBox1.Text = sb.ToString
    
            Dim twentyFourHour As String = Now.Hour.ToString("00") & ":" & Now.Minute.ToString("00") & ":" & Now.Second.ToString("00")
    
            MessageBox.Show(twentyFourHour)
    
        End Sub
    End Class

    Regards,

    John
    • Marked as answer by vita3 Thursday, February 25, 2010 9:32 PM
    Thursday, February 25, 2010 9:29 PM

All replies

  • I would like to convert 12H to 24H format.
    I have for example: 06:11:11 PM and I need to convert it so I can see 18:11:11 in VB.net.
    Please help me here.

    V. A.

    Hi,

    Add one button and one large-ish RichTextBox to a FORM and run this please.

    This will show you all the possible DATE or TIME or Date and Time formats.  :-)


    The MessageBox will show the time in 24 Hr format if you use.>>


            Dim twentyFourHour As String = Now.Hour.ToString("00") & ":" & Now.Minute.ToString("00") & ":" & Now.Second.ToString("00")
    
            MessageBox.Show(twentyFourHour)



    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim myDateTime As DateTime = Now
    
            Dim formatStrings() As String = myDateTime.GetDateTimeFormats
    
            Dim sb As New System.Text.StringBuilder
            For index As Integer = 0 To formatStrings.GetUpperBound(0)
                sb.Append(formatStrings(index) & System.Environment.NewLine)
            Next
    
            RichTextBox1.Text = sb.ToString
    
            Dim twentyFourHour As String = Now.Hour.ToString("00") & ":" & Now.Minute.ToString("00") & ":" & Now.Second.ToString("00")
    
            MessageBox.Show(twentyFourHour)
    
        End Sub
    End Class

    Regards,

    John
    • Marked as answer by vita3 Thursday, February 25, 2010 9:32 PM
    Thursday, February 25, 2010 9:29 PM
  • If it is between 1pm and 12am...add 12hours to the hour. If it is 12:00 - 12:59 AM, subtract 12, or just set the hour to 0. From 1am - 1159am, do nothing.

    The only time you need to do anything is the two conditions I just mentioned.

    Thursday, February 25, 2010 9:30 PM
  • just saw this one, and it's already answered, but I thought I'd add this:


                '24 hr clock
                Label2.Text = Format(Now(), "HH:mm:ss")
    
                'AM/PM clock
                Label2.Text = Format(Now(), "h:mm:ss tt")
    Thursday, February 25, 2010 10:49 PM
  • Hi Joe,

    I thought I'd seen that on these forums before but it was ages ago!!  :-D


    Regards,

    John
    Friday, February 26, 2010 12:15 AM