Time Format

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pablojoshua
    New Member
    • Jun 2007
    • 10

    Time Format

    I am having problems formatting time in visual basic. I need it to be in 24 hour time without the AM or PM at the end. So if I wanted 1:29pm (12-hour) it would come to be 13:29

    Thanks
  • Killer42
    Recognized Expert Expert
    • Oct 2006
    • 8429

    #2
    Originally posted by pablojoshua
    I am having problems formatting time in visual basic. I need it to be in 24 hour time without the AM or PM at the end. So if I wanted 1:29pm (12-hour) it would come to be 13:29
    What version of VB?

    In VB6, you should be able to use Format(YourDate Time, "hh:nn").

    Comment

    • PCroser
      New Member
      • Jun 2007
      • 2

      #3
      I think it should be format("HH:mm)

      the uppercase HH represents 24 hour time.
      the lowercase hh represents 12 hour time.

      Peter

      Comment

      • Killer42
        Recognized Expert Expert
        • Oct 2006
        • 8429

        #4
        Originally posted by PCroser
        the uppercase HH represents 24 hour time.
        the lowercase hh represents 12 hour time.
        Does it? That's odd, I tested my suggestion before posting and it worked. Perhaps it depends on VB version, or Windows regional settings.

        Comment

        • pablojoshua
          New Member
          • Jun 2007
          • 10

          #5
          well windows died on me, so im running on a 40GB hard drive.

          I had (reinstalling now) VB6.

          And I recovered all my data succesfully, so I will try this soon.

          Thanks!

          One more question, if I convert it to 24 hour time, then send it to a string variable, will is still work using the < and > (less than/greater than) statements on it. Cause it works if you just use [text1.text = time] and then in a timer just have like
          If text1.text = > 12:32:01 PM then
          text2.text = "Lunch is over"
          end if
          cause thats worked for me so far. The only reason that I need 24 hour time is because VB doesn't regognize if it is AM or PM. So even if its > 12:32:01 AM the code will still run.

          Thanks Again!


          Cheers

          Comment

          • Killer42
            Recognized Expert Expert
            • Oct 2006
            • 8429

            #6
            I'd say the problem there is just that you're comparing string. So all it's doing is looking at the characters in the string, one by one, not the actual time value. As a string, "2:13 AM" is greater than "12:54 PM" simply because the character "2" has a higher ASCII value than "1". If you convert them to an actual time value of course, it's a different matter.

            If you want to compare times you're better off converting back and forth as required. Working with strings that represent things like a date or time is very risky, as the format can change from one PC to the next.

            Tip - in many cases it's better to use something like a datetime picker control rather than a textbox. In some cases, I've also used one or two labels to display a time value, with a couple of "spinner" or "updown" controls for the user to adjust the time. If you let people type in a value, they will always find some way to stuff it up.

            (In fact I had to debug a case like this just recently, where the user entered a time of "15:3O". Which looks alright, but most certainly isn't. Try copying and pasting it if you don't believe me.)

            Comment

            • pablojoshua
              New Member
              • Jun 2007
              • 10

              #7
              could you please just give like an example code for the 24 hour format. If i do this:

              Private Sub Timer1_Timer()
              Text1.Text = Time & Format("15:30", "HH:mm")
              End Sub
              the text box will give me this

              5:28:19 PM 15:30
              thanks

              Comment

              • Killer42
                Recognized Expert Expert
                • Oct 2006
                • 8429

                #8
                [CODE=vb]Private Sub Timer1_Timer()
                Text1.Text = Format(Time, "HH:mm")
                End Sub[/CODE]Is this what you're after?

                Comment

                • Killer42
                  Recognized Expert Expert
                  • Oct 2006
                  • 8429

                  #9
                  Also, for your comparison I think you can use something like this...
                  [CODE=vb]
                  If CDate(Text1.Tex t) > CDate("12:30:59 ") Then
                  Text2.Text = "Lunch is over"
                  End If[/CODE]Of course, ideally you'd (A) validate Text1.Text first to ensure it's a proper time value, and (B) use a constant (or variable) for the end of lunch, not hard-code a string in the test like this and convert it every time.

                  Comment

                  • pablojoshua
                    New Member
                    • Jun 2007
                    • 10

                    #10
                    Originally posted by Killer42
                    [CODE=vb]Private Sub Timer1_Timer()
                    Text1.Text = Format(Time, "HH:mm")
                    End Sub[/CODE]Is this what you're after?

                    THANK-YOU...thats all I needed...lol

                    Comment

                    • Killer42
                      Recognized Expert Expert
                      • Oct 2006
                      • 8429

                      #11
                      Originally posted by pablojoshua
                      THANK-YOU...thats all I needed...lol
                      Glad we could help. :)

                      Comment

                      • pranav1234
                        New Member
                        • Mar 2014
                        • 1

                        #12
                        You all can use this :-
                        DateTime.Now.ToLocalTime.ToString

                        The Output :-
                        30-Mar-14 2:51:15

                        Comment

                        • Killer42
                          Recognized Expert Expert
                          • Oct 2006
                          • 8429

                          #13
                          Not in VB6 you can't.

                          Comment

                          • MarcPerez
                            New Member
                            • Sep 2015
                            • 1

                            #14
                            You can do this
                            [Text1] = Format(Time, "HH:mm")

                            Result is your current time with 24 hours format
                            Example 3pm

                            15:00

                            Comment

                            Working...