Formatting Date and Time VB.NET

The Date literal must be enclosed within number signs (#date#) and specify the date value in the format M/d/yyyy like #09/06/2014#. Else, the way how your code is interpreted may vary depending on the locale in which your application is deployed.

With this requirement the meaning of your code should never change whether if it has a different date and time format settings. For example, you hard-coded a Date literal of #9/6/2014# which means September 6, 2014. That will compile 9/6/2014 as you want mm/dd/yyyy. However, if you deploy application in several locales using dd/mm/yyyy date format, your hard-coded literal would compile to June 9, 2014. For other locales using yyyy/mm/dd, the literal would be invalid causing compilation error.

To convert a Date literal to the format of your locale or to a custom format, use the Format function of String class, specifying either a predefined or user-defined date format.

Predefined Date/Time Formats

The following table identifies the predefined date and time format names. These may be used by name as the style argument for the Format function:
Format Name Description
General Date, or G Displays a date and/or time. For example, 3/12/2008 11:07:31 AM. Date display is determined by your application's current culture value.
Long Date, Medium Date, or D Displays a date according to your current culture's long date format. For example, Wednesday, March 12, 2008.
Short Date, or d Displays a date using your current culture's short date format. For example, 3/12/2008. The d character displays the day in a user-defined date format.
Long Time, Medium Time, or T Displays a time using your current culture's long time format; typically includes hours, minutes, seconds. For example, 11:07:31 AM.
Short Time or t Displays a time using your current culture's short time format. For example, 11:07 AM. The t character displays AM or PM values for locales that use a 12-hour clock in a user-defined time format.
f Displays the long date and short time according to your current culture's format. For example, Wednesday, March 12, 2008 11:07 AM.
F Displays the long date and long time according to your current culture's format. For example, Wednesday, March 12, 2008 11:07:31 AM.
g Displays the short date and short time according to your current culture's format. For example, 3/12/2008 11:07 AM.
M, m Displays the month and the day of a date. For example, March 12. The M character displays the month in a user-defined date format. The m character displays the minutes in a user-defined time format.
R, r Formats the date according to the RFC1123Pattern property. For example, Wed, 12 Mar 2008 11:07:31 GMT. The formatted date does not adjust the value of the date and time. You must adjust the Date/Time value to GMT before calling the Format function.
s Formats the date and time as a sortable index. For example, 2008-03-12T11:07:31. The s character displays the seconds in a user-defined time format.
u Formats the date and time as a GMT sortable index. For example, 2008-03-12 11:07:31Z.
U Formats the date and time with the long date and long time as GMT. For example, Wednesday, March 12, 2008 6:07:31 PM.
Y, y Formats the date as the year and month. For example, March, 2008. The Y and y characters display the year in a user-defined date format.

Pre-Defined Format Example

The following example demonstrates how this can be achived.
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim SampleDateTime As Date = #9/6/2014 1:14:23 PM#
        Debug.Print("Formatting Date and Time Examples:")
        Debug.Print("Using Format 'D:' " & SampleDateTime.ToString("D"))
        Debug.Print("Using Format 'd:' " & SampleDateTime.ToString("d"))
        Debug.Print("Using Format 'T:' " & SampleDateTime.ToString("T"))
        Debug.Print("Using Format 't:' " & SampleDateTime.ToString("t"))
        Debug.Print("Using Format 'F:' " & SampleDateTime.ToString("F"))
        Debug.Print("Using Format 'f:' " & SampleDateTime.ToString("f"))
        Debug.Print("Using Format 'G:' " & SampleDateTime.ToString("G"))
        Debug.Print("Using Format 'g:' " & SampleDateTime.ToString("g"))
        Debug.Print("Using Format 'M:' " & SampleDateTime.ToString("M"))
        Debug.Print("Using Format 'm:' " & SampleDateTime.ToString("m"))
        Debug.Print("Using Format 'R:' " & SampleDateTime.ToString("R"))
        Debug.Print("Using Format 'r:' " & SampleDateTime.ToString("r"))
        Debug.Print("Using Format 'Y:' " & SampleDateTime.ToString("Y"))
        Debug.Print("Using Format 'y:' " & SampleDateTime.ToString("y"))

    End Sub
End Class

Pre-Defined Output

When the project is compiled it will print the following results.
Formatting Date and Time Examples:
Using Format 'D:' Saturday, September 06, 2014
Using Format 'd:' 9/6/2014
Using Format 'T:' 1:14:23 PM
Using Format 't:' 1:14 PM
Using Format 'F:' Saturday, September 06, 2014 1:14:23 PM
Using Format 'f:' Saturday, September 06, 2014 1:14 PM
Using Format 'G:' 9/6/2014 1:14:23 PM
Using Format 'g:' 9/6/2014 1:14 PM
Using Format 'M:' September 06
Using Format 'm:' September 06
Using Format 'R:' Sat, 06 Sep 2014 13:14:23 GMT
Using Format 'r:' Sat, 06 Sep 2014 13:14:23 GMT
Using Format 'Y:' September, 2014
Using Format 'y:' September, 2014

User-Defined Date and Time Formats

The following table shows the characters you can use to create user-defined date/time formats. Unlike in earlier versions of Visual Basic, these format characters are case-sensitive.
Character Description
(:) Time separator. In some locales, other characters may be used to represent the time separator. The time separator separates hours, minutes, and seconds when time values are formatted. The actual character that is used as the time separator in formatted output is determined by your application's current culture value.
(/) Date separator. In some locales, other characters may be used to represent the date separator. The date separator separates the day, month, and year when date values are formatted. The actual character that is used as the date separator in formatted output is determined by your application's current culture.
(%) Used to indicate that the following character should be read as a single-letter format without regard to any trailing letters. Also used to indicate that a single-letter format is read as a user-defined format. See what follows for additional details.
d Displays the day as a number without a leading zero (for example, 1). Use %d if this is the only character in your user-defined numeric format.
dd Displays the day as a number with a leading zero (for example, 01).
ddd Displays the day as an abbreviation (for example, Sun).
dddd Displays the day as a full name (for example, Sunday).
M Displays the month as a number without a leading zero (for example, January is represented as 1). Use %M if this is the only character in your user-defined numeric format.
MM Displays the month as a number with a leading zero (for example, 01/12/01).
MMM Displays the month as an abbreviation (for example, Jan).
MMMM Displays the month as a full month name (for example, January).
gg Displays the period/era string (for example, A.D.).
h Displays the hour as a number without leading zeros using the 12-hour clock (for example, 1:15:15 PM). Use %h if this is the only character in your user-defined numeric format.
hh Displays the hour as a number with leading zeros using the 12-hour clock (for example, 01:15:15 PM).
H Displays the hour as a number without leading zeros using the 24-hour clock (for example, 1:15:15). Use %H if this is the only character in your user-defined numeric format.
HH Displays the hour as a number with leading zeros using the 24-hour clock (for example, 01:15:15).
m Displays the minute as a number without leading zeros (for example, 12:1:15). Use %m if this is the only character in your user-defined numeric format.
mm Displays the minute as a number with leading zeros (for example, 12:01:15).
s Displays the second as a number without leading zeros (for example, 12:15:5). Use %s if this is the only character in your user-defined numeric format.
ss Displays the second as a number with leading zeros (for example, 12:15:05).
f Displays fractions of seconds. For example ff displays hundredths of seconds, whereas ffff displays ten-thousandths of seconds. You may use up to seven f symbols in your user-defined format. Use %f if this is the only character in your user-defined numeric format.
t Uses the 12-hour clock and displays an uppercase A for any hour before noon; displays an uppercase P for any hour between noon and 11:59 P.M. Use %t if this is the only character in your user-defined numeric format.
tt For locales that use a 12-hour clock, displays an uppercase AM with any hour before noon; displays an uppercase PM with any hour between noon and 11:59 P.M. For locales that use a 24-hour clock, displays nothing.
y Displays the year number (0-9) without leading zeros. Use %y if this is the only character in your user-defined numeric format.
yy Displays the year in two-digit numeric format with a leading zero, if applicable.
yyy Displays the year in four-digit numeric format.
yyyy Displays the year in four-digit numeric format.
z Displays the timezone offset without a leading zero (for example, -8). Use %z if this is the only character in your user-defined numeric format.
zz Displays the timezone offset with a leading zero (for example, -08)
zzz Displays the full timezone offset (for example, -08:00)

User Defined Date and Time Formatting Examples

Copy and Paste the following codes into your project then compile.
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim SampleDateTime As Date = #9/6/2014 1:14:23 PM#
        Debug.Print("User-Defined Date and Time Formatting Examples:")
        Debug.Print("Prints '9/6/14': " & Format(SampleDateTime, "M/d/y"))
        Debug.Print("Prints '6-Sep': " & Format(SampleDateTime, "d-MMM"))
        Debug.Print("Prints '6-Sep-14': " & Format(SampleDateTime, "d-MMM-yy"))
        Debug.Print("Prints '6 September': " & Format(SampleDateTime, "d MMMM"))
        Debug.Print("Prints 'September 14': " & Format(SampleDateTime, "MMMM yy"))
        Debug.Print("Prints '2014-09-06': " & Format(SampleDateTime, "yyyy-MM-dd"))
        Debug.Print("Prints '01:14 PM': " & Format(SampleDateTime, "hh:mm tt"))
        Debug.Print("Prints '1:14:23 P': " & Format(SampleDateTime, "h:mm:ss t"))
        Debug.Print("Prints '13:14': " & Format(SampleDateTime, "H:mm"))
        Debug.Print("Prints '13:14:23': " & Format(SampleDateTime, "H:mm:ss"))
        Debug.Print("Prints '9/6/2014 13:14': " & Format(SampleDateTime, "M/d/yyyy H:mm"))

    End Sub
End Class

User-Defined Output

When the project is compile it will print the following results.
User-Defined Date and Time Formatting Examples:
Prints '9/6/14': 9/6/14
Prints '6-Sep': 6-Sep
Prints '6-Sep-14': 6-Sep-14
Prints '6 September': 6 September
Prints 'September 14': September 14
Prints '2014-09-06': 2014-09-06
Prints '01:14 PM': 01:14 PM
Prints '1:14:23 P': 1:14:23 P
Prints '13:14': 13:14
Prints '13:14:23': 13:14:23
Prints '9/6/2014 13:14': 9/6/2014 13:14

Comments

  1. I just found this blog and have high hopes for it to continue. Keep up the great work, its hard to find good ones. I have added to my favorites. Thank You. this

    ReplyDelete

Post a Comment

Popular posts from this blog

How to Create a Configuration.INI Files in VB6

How to Set Windows Form Always on Top of Other Applications in VB6

How to Minimize/Send an App Icon to System Tray in Visual Basic 6