How to use Date and Time in VB?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • neo008
    New Member
    • Jun 2007
    • 85

    How to use Date and Time in VB?

    Hi All,

    1. How can I show live clock (system's clock) on my MDI form? (Continuous updation)

    2. Fetched System Date is being displayed in MM/DD/YYYY format in a text box. Even when I set data format property to DD-Month-YYYY.


    can somebody help me.


    -NE☼
    Last edited by neo008; Aug 3 '07, 09:00 PM. Reason: prediction
  • Killer42
    Recognized Expert Expert
    • Oct 2006
    • 8429

    #2
    Originally posted by neo008
    1. How can I show live clock (system's clock) on my MDI form? (Continuous updation)
    I'd recommend using the StatusBar control, that's probably the simplest. Otherwise, I suppose you could use a timer control and write it to a label caption or a textbox.
    Originally posted by neo008
    2. Fetched System Date is being displayed in MM/DD/YYYY format in a text box. Even when I set data format property to DD-Month-YYYY.
    What "data format property" is that? (I assume you mean date format, but still don't know exactly what you're referring to.)
    If placing in a textbox, perhaps the simplest option is to first use Format() function to get the string you want. Ideally, you should use the named formats such as "General Date" to allow Windows to use the format specified on that PC. If you use your own custom format such as "MM/DD/YYYY" then you are forcing that on everybody.

    P.S. "Updation"? Hey, I've learned a new word today. :)

    Comment

    • JonJacobs
      New Member
      • Aug 2007
      • 22

      #3
      Code:
      Const MyFormat = "my chosen date format" ' replace with real format
      Dim MyDate as datetime = Date()
      Dim S as string = MyDate.ToString(MyFormat)
      'Display S in the appropriate place
      HTH

      Comment

      • Killer42
        Recognized Expert Expert
        • Oct 2006
        • 8429

        #4
        It would help if we knew what version we're talking about, as well.

        Comment

        • lee123
          Contributor
          • Feb 2007
          • 556

          #5
          how about he do this code for the time:

          place a label on your form and name it lbltime
          then put a timer on the form and put this code:

          Code:
          if lbltime.caption <> cstr(time) then
              lbltime.caption = time
          end if

          this works! and it shows the live time.

          lee123

          Comment

          • lee123
            Contributor
            • Feb 2007
            • 556

            #6
            ooooooooh i almost forgot you have to set the timer properties.

            set the timer interval to 500 (half a second)
            then set the timer enabled to true

            and that should do it for that.as for the label you probably set some other border for it.

            lee123

            Comment

            • neo008
              New Member
              • Jun 2007
              • 85

              #7
              Thanks for your kind help friends.

              KILLER-- I'm really sorry I forgot to mention the version i'm using. and its VB6.

              And the format i am talking about is standard "Date" fromat chosen through property "Data Format". There are so many standard formats are there and i chose e.g. 05-August-2007. [dd-mmmm-yyyy]

              Code:
              ShowDate.Text = Date
              Moreover same problem I am facing somewhere in numeric fields also, I restrict a text box upto 2 decimal places but while doing calculations in code it shows me any number of decimal places. I failed to make a CURRENCY TEXT BOX.

              JACOB --- I tried your code but its not even showing me any data type like "DateTime"

              Code:
              MyDate as DateTime
              it has only Date data type.
              Thanks a lot for your kind help but it didn't work.


              Lee --- Your code is absolutely working and my TIME PROBLEM IS SOLVED.
              Thanks a lot.


              ONLY DATE FORMAT IS TO BE SHOWN IN 05 August 2007 Format



              -NE☼

              Comment

              • Killer42
                Recognized Expert Expert
                • Oct 2006
                • 8429

                #8
                Originally posted by neo008
                ... standard "Date" fromat chosen through property "Data Format".
                Ah! Sorry, I haven't bound textboxes to database fields in years, so had completely forgotten about the Dataxxxx properties.

                Comment

                • JonJacobs
                  New Member
                  • Aug 2007
                  • 22

                  #9
                  Originally posted by neo008
                  KILLER-- I'm really sorry I forgot to mention the version i'm using. and its VB6.

                  I tried your code but its not even showing me any data type like "DateTime"

                  Code:
                  MyDate as DateTime
                  it has only Date data type.
                  Thanks a lot for your kind help but it didn't work.
                  Sorry, I had to guess at the version. datetime is legit in vb.net (VS 2005 I think) that I use at work.

                  Comment

                  • neo008
                    New Member
                    • Jun 2007
                    • 85

                    #10
                    Ah! Sorry, I haven't bound textboxes to database fields in years, so had completely forgotten about the Dataxxxx properties.
                    Well Killer, Do I have to really bind that text box with database for Dataxxxxx properties (DataFormat).
                    Anyway thanks for your time Killer.



                    Sorry, I had to guess at the version. datetime is legit in vb.net (VS 2005 I think) that I use at work.
                    Ya Jacob, its my fault I did't mention the version I'm using. Let it go coz one of my problems is solved by Lee and problem regarding date I'll keep fighting as usual.
                    Each of your ideas have values for me. thanks.


                    -NE☼

                    Comment

                    • Killer42
                      Recognized Expert Expert
                      • Oct 2006
                      • 8429

                      #11
                      Originally posted by neo008
                      Well Killer, Do I have to really bind that text box with database for Dataxxxxx properties (DataFormat).
                      I don't know. But I think that DataField, DataFormat, DataMember and DataSource are intended to work together when binding to a data field, in a data control or whatever. I'd suggest checking the doco.

                      Generally though, if you want a specific format in a textbox, I would just format the text before placing it there.

                      Comment

                      • hariharanmca
                        Top Contributor
                        • Dec 2006
                        • 1977

                        #12
                        Originally posted by neo008
                        Well Killer, Do I have to really bind that text box with database for Dataxxxxx properties (DataFormat).
                        Anyway thanks for your time Killer.




                        Ya Jacob, its my fault I did't mention the version I'm using. Let it go coz one of my problems is solved by Lee and problem regarding date I'll keep fighting as usual.
                        Each of your ideas have values for me. thanks.


                        -NE☼
                        Are you mean to change your system date format for your application? so that you can get things proper.
                        (but make sure this will change the system date format in case of power off or application closed by task manager)

                        Comment

                        • neo008
                          New Member
                          • Jun 2007
                          • 85

                          #13
                          Generally though, if you want a specific format in a textbox, I would just format the text before placing it there.
                          no boundaries are there for good programmers. your correct Killer. But I am doing lots of calculations on currency.....al so rounding it off upto two decimal places in code. but how to show eg. 123.40 and 123.00

                          i dont even want to think to arrange each digit place by place and add decimals at suffix. i'm scared.

                          Are you mean to change your system date format for your application? so that you can get things proper.
                          (but make sure this will change the system date format in case of power off or application closed by task manager)
                          I dont know much about it Hariharan that is it my system date format or not.....but I was also thinking in same way coz I tried toolbar and its also showing the same format......MM/DD/YYYY .

                          Thank you Killer and Hariharan for your kind support.

                          Comment

                          • Killer42
                            Recognized Expert Expert
                            • Oct 2006
                            • 8429

                            #14
                            Originally posted by neo008
                            no boundaries are there for good programmers. your correct Killer. But I am doing lots of calculations on currency.....al so rounding it off upto two decimal places in code. but how to show eg. 123.40 and 123.00

                            i dont even want to think to arrange each digit place by place and add decimals at suffix. i'm scared.
                            That's what the Format() function is for. Try it with the "Fixed" format. Look up the doco on Format, there are lots of options.

                            Comment

                            Working...