VB Tutorial
Tools
Free Downloads
Free Link Directory

Basics
Data Types
Windows Forms
Buttons
Check Box
Text Box
Combo Box
Radio Buttons
Progress Bar
Color & Font Dialogs
Open & Save Dialogs
Process Control

More Complex Examples
Audio Player
Rotating Rectangle

Convert Date Format


Vb.Net & Visual Basic

Now say that you have a date in the form of 7/17/2008 and you need to convert or change it to a format like 17-Jul-2008 in either visual basic or vb.net. There is actually a very easy way of doing it and the keyword is "format".

One way of changing your date's format would be as simple as:
Debug.Print (Format (Date,  "dd-MMM-yyy:))

I have prepared you a little sample code to explain just one can use the format code. You can copy and paste the following code into your form.

Public Class Form1
Dim Date1 As Date = #7/17/2008#
Dim NewDate1 As String
Public WithEvents txtOld As New TextBox
Public WithEvents txtNew As New TextBox


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Text = "Convert Date Format"
Me.Controls.Add(txtOld) : txtOld.Top = 0 : txtOld.Width = 120
Me.Controls.Add(txtNew) : txtNew.Top = 0 + txtOld.Height : txtNew.Width = 120
txtOld.Text = "M/d/yyy"
txtNew.Text = (Format(Date1, "M/d/yyy"))
Debug.Print(Date1)
Debug.Print(Format(Date1, "dd-MMM-yyy"))

End Sub

Private Sub txtOld_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtOld.TextChanged
txtNew.Text = Format(Date1, txtOld.Text)
End Sub
End Class

The end result should look something like the following image:

Use the top text box to type in different formats for your date and the bottom one will display the results. You can try out some different formats like:

  1. d-M-yy  = 17-7-08

  2. dd MMM yyy  = 17 Jul 2008

  3. dddd d MMMM yyy  = Thursday 17 July 2008

If you liked this article then Please Thumb This Up with a short review - Stumble It!


Email us at: info@pro2visual.com

Get your Website Listed on our Free Link Banner Directory
Advertise your site with your own unique link banner we will also provide text links to your site.

 

 

Popular Articles

Moving a borderless form in vb.net

ComboBox1.Items.Add

Link Progress Bar with Timer in vb.net

How to open a folder using the process control

Start Vb.net programming. No get rich quick scam

How to Convert the Date Format

Click here for Industry Leading Development Softwa