Results 1 to 7 of 7

Thread: Display Date and time on a form VS 2022

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2022
    Posts
    7

    Display Date and time on a form VS 2022

    Hi I am trying to display the date and time in a text box,

    I can get the date to show but have an issue displaying the time.
    Any help would be great thanks in advance.

    Sample of code below.

    Dim theDate As Date
    theDate = Format(Now(), "Short Date")
    TextBox1.Text = theDate

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Display Date and time on a form VS 2022

    Firstly, don't use that Format function at all, ever. It's a VB6 holdover that has no place in new VB.NET code. The same goes for that Now property.

    As for the question, if you want to display the time then why would you specify a format that is date only? Whether you use old VB6 methods or new .NET methods, if you want to display the date and time then specify a format that includes both date and time, which "Short Date" does not.

    You are also getting the current date and time as a Date, converting that to a String, converting that String to a Date and then converting that Date to a String again. No, don't do that. If you simply did this:
    vb.net Code:
    1. TextBox1.Text = Date.Now
    it would work. That's not ideal though, because you're relying on the system to perform an implicit conversion from Date to String. You really ought to turn Option Strict On, which would force you to do it explicitly, e.g.
    vb.net Code:
    1. TextBox1.Text = Date.Now.ToString()
    That will display the date and time in the system default format. If you want a specific format then you can specify that, e.g.
    vb.net Code:
    1. TextBox1.Text = Date.Now.ToString("d/MM/yyyy h:mm tt")

  3. #3

    Thread Starter
    New Member
    Join Date
    Jun 2022
    Posts
    7

    Re: Display Date and time on a form VS 2022

    Quote Originally Posted by jmcilhinney View Post
    Firstly, don't use that Format function at all, ever. It's a VB6 holdover that has no place in new VB.NET code. The same goes for that Now property.

    As for the question, if you want to display the time then why would you specify a format that is date only? Whether you use old VB6 methods or new .NET methods, if you want to display the date and time then specify a format that includes both date and time, which "Short Date" does not.

    You are also getting the current date and time as a Date, converting that to a String, converting that String to a Date and then converting that Date to a String again. No, don't do that. If you simply did this:
    vb.net Code:
    1. TextBox1.Text = Date.Now
    it would work. That's not ideal though, because you're relying on the system to perform an implicit conversion from Date to String. You really ought to turn Option Strict On, which would force you to do it explicitly, e.g.
    vb.net Code:
    1. TextBox1.Text = Date.Now.ToString()
    That will display the date and time in the system default format. If you want a specific format then you can specify that, e.g.
    vb.net Code:
    1. TextBox1.Text = Date.Now.ToString("d/MM/yyyy h:mm tt")
    THanks for the responce I added box 3 to my form, however nothing is displayed and no errors.

    Private Sub TextBox1_TextChanged_1(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
    TextBox1.Text = Date.Now.ToString("d/MM/yyyy h:mm tt")

    End Sub
    End Class
    TextBox1 is the label of the text box.

    Kind regards

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,041

    Re: Display Date and time on a form VS 2022

    You put it in the textchanged event handler. What did you expect to happen? That event is only raised when the text in the textbox changes. Type a character into the textbox, and you'll see your date. I really doubt that's the behavior you wanted, but that's the behavior you will get because of how you wrote the code.

    When do you want that date to show up? Do you want it when the form loads? If so, then put it in form load event handler. Do you want it when you press a button? Then put it in the click event handler for that button.
    My usual boring signature: Nothing

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,479

    Re: Display Date and time on a form VS 2022

    You wouldn’t put the code in TextBox1_TextChanged… Use your Form1_Load event handler, for a static date and time. If you want the time to update in real time, use a Timer…

    Code:
    Public Class Form1
        Private WithEvents tmr as New Timer With {.Interval = 500}
    
        Public Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Tmr.Start
        End Sub
    
        Private Sub tmr_Tick(sender As Object, e As EventArgs) Handles tmr.Tick
            TextBox1.Text = … ‘your code here
        End Sub
    
    End Class

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,041

    Re: Display Date and time on a form VS 2022

    Also, if you do want to be updating that date/time, whether with a timer or by some other means, then it shouldn't be in a textbox, it should be in a label. The user expects to be able to change information in a textbox. If you are putting the date/time in a textbox, then the user will expect to be able to change it, so if you are just going to replace those changes, you will just confuse the user. If they shouldn't expect to be able to change the values, then use a label rather than a textbox.
    My usual boring signature: Nothing

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Display Date and time on a form VS 2022

    Quote Originally Posted by Shaggy Hiker View Post
    Also, if you do want to be updating that date/time, whether with a timer or by some other means, then it shouldn't be in a textbox, it should be in a label. The user expects to be able to change information in a textbox. If you are putting the date/time in a textbox, then the user will expect to be able to change it, so if you are just going to replace those changes, you will just confuse the user. If they shouldn't expect to be able to change the values, then use a label rather than a textbox.
    Quite so, and if you are intending for the user to input the date and/or time then you should be using a DateTimePicker.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width