How to Format Date and Time in Visual Basic.Net


In this article I am going to show you how you can use different formats of date and time in Microsoft Visual Basic.Net. All you need to do is to create a form and place a Text Box and a Button on it and write the code on the click event of the button as shown.

To use different formats of date and time in Microsoft Visual Basic.Net.First of all create a form and place a Text Box and a Button on it. Now, change the text property of Button to Show Date as shown in image and type the following code in the click event of the button one by one and see the different date formats in VB.Net.


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim dt As Date
dt = Now
TextBox1.Text = dt ' date and time
TextBox1.Text = dt.ToShortDateString ' only date
TextBox1.Text = dt.ToLongDateString ' like Saturday, April 20, 2013
TextBox1.Text = dt.ToShortTimeString ' Time with Hours & minutes
TextBox1.Text = dt.ToLongTimeString ' Time including seconds
TextBox1.Text = dt.Hour ' to display only hour
TextBox1.Text = dt.Day ' to display only day
TextBox1.Text = dt.DayOfYear ' to display the day of the year
TextBox1.Text = dt.Minute ' to display the minute of the time

End Sub

You can also see many more properties of this date variable.

Regards
Keep coding


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: