Format date and time - Visual Basic .NET

Tags: VB.NET, VB 2008, VB 2010, VB 2012, VB 2013

For this tutorial I use a DateTimePicker to custom format it

format date and time in vb.net

The above DateTimePicker is in the default format

To change it to yyyy/MM/DD

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) _
  Handles MyBase.Load
        DateTimePicker1.Format = DateTimePickerFormat.Custom
        DateTimePicker1.CustomFormat = "yyyy/MM/dd"
End Sub

Result:

date and time format in vb.net

To change it to yyyy-mm-dd change the above custom format to the following

DateTimePicker1.CustomFormat = "yyyy-MM-dd"

You can choose from the table below to make your own custom date and time. Remeber you can use seperators such as :, /, - to seprate dates and times.

Days

Format Keyword

Description

"d"

The day position in a month: 1 - 31

"dd"

The day position in a month: 01- 31

"ddd"

The abbreviated name of the day: Sun, Mon, Tue etc...

"dddd"

The full name of the day in a week: Sunday, Monday, Tuesday etc...

Months

Format Keyword

Description

"M"

The month without the leading zero:  1 to 12.

"MM"

The month with the leading zero to make it 2 digit 01 - 12.

"MMM"

The name of the month in  abbreviated format: Jan, Feb, Mar, etc...

"MMMM"

The name of the month in a full format: January, February, March, etc...

Years

Format Keyword

Description

"y"

The year without leading zero: 0 to 99.

"yy"

The year with the leading zero in two digit format: 00 to 99.

"yyyy"

The full format of the year in four digits: 2014, 2015, etc...

Hours

Format Keyword

Description

"h"

The hour in 12-hour format without leading zeros:  1 to 12.

"hh"

The hour in 12-hour format with the leading zeros: 01 to 12.

"H"

The hour in a 24-hour format without leading zeros: 0 to 23.

"HH"

The hourin a 24-hour format with leading zero: 00 to 23.

"t"

First charachter of AM/PM

"tt"

AM or PM

Minutes

Format Keyword

Description

"m"

The minute without leading zeros: 0 to 59.

"mm"

The minute in two digit format with leading zero: 00 to 59.

Seconds

Format Keyword

Description

"s"

The second without leading zeros: 0 to 59.

"ss"

The second in two digit format with leading zero: 00 to 59.


Share This

Home | About | Contact | Privacy Policy

Copyright visual-basic-tutorials.com 2017 - All Rights Reserved.