Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleUse custom formatting strings to format Dates in VB .NET
DescriptionThis example shows how to use custom formatting strings to format Dates in VB .NET.
Keywordsformat, format specifier, string, VB.NET
CategoriesVB.NET, Strings
 
The Date class's ToString method formats a Date value. A parameter indicates the custom format to use.

Some single characters may also indicate a standard format. For example, "d" means to use the system's short date format. To use the "d" character as a custom format specifier rather than as the standard one, use %d. That prints the Date's day number rather than the short date format.

Standard formats only work when they are used alone. So, for example, "-d" displays a dash followed by the day number.

The following list summarizes the custom date format strings. Some combined formats contain arbitrary characters to separate format specifiers. For example, "s^ff" indicates seconds followed by ^ followed by fractions of seconds to two digits.

  • d - Short date
  • %d - Day number
  • M?d - Month and day number
  • dd - Day number, two digits
  • ddd - Abbreviated day name
  • dddd - Full day name
  • f - Full (long date, short time)
  • %f - Fractions of second, one digit
  • s^f - Seconds and fractions of second, one digit
  • ff - Fractions of second, two digits
  • fff - Fractions of second, three digits
  • ffff - Fractions of second, four digits
  • fffff - Fractions of second, five digits
  • ffffff - Fractions of second, six digits
  • fffffff - Fractions of second, seven digits
  • g - General
  • %g - Era (eg. A.D.)
  • y-g - Year and era (eg. 5-A.D.)
  • gg - Era (eg. A.D.)
  • h - Hour (1-12) (Doesn't seem to work)
  • %h - Hour (1-12)
  • h-m - Hour and minute
  • hh - Hour (01-12)
  • H - Hour (0-23) (Doesn't seem to work)
  • HH - Hour (00-23)
  • m - Month name and date
  • %m - Minute (0-59)
  • hh_m - Hour and minute (0-59)
  • mm - Minute (00-59)
  • M - Month name and date
  • %M - Month number (1-12)
  • M+d - Month number and day number
  • MM - Month number (01-12)
  • MMM - Month abbreviation
  • MMMM - Month name
  • s - Standard sortable date/time
  • %s - Seconds (0-59)
  • s^ff - Seconds (0-59) and fraction of seconds
  • ss - Seconds (00-59)
  • t - Long time
  • %t - First letter of AM/PM designator
  • hh+t - Hour and first letter of AM/PM designator
  • tt - AM/PM designator
  • y - Short date
  • %y - Year (0-99)
  • m-y - Month and year
  • yy - Year (00-99)
  • yyyy - Year (0000-9999)
  • z - Doesn't work
  • %z - Whole hour time zone (-12 to +13)
  • Zone:z - Zone - and whole hour time zone (-12 to +13)
  • zz - Whole hour time zone (-12 to +13) with two digits
  • zzz - Time zone in hours and minutes})

This example program lets you enter values and enter or pick format strings to see the results. When you change the value or format string, the ShowValue routine displays the result. That routine passes the format string up to the first space to the Date's ToString method. (For example, The format string might be "%y - Year (0-99)" so it would use the string "%y".)

 
Private Sub ShowValue()
    Try
        Dim format_string As String = cboFormat.Text
        If format_string.IndexOf(" ") > 0 Then
            format_string = format_string.Substring(0, _
                format_string.IndexOf(" "))
        End If
        txtResult.Text = Now.ToString(format_string)
        Debug.WriteLine(format_string)
    Catch ex As Exception
        txtResult.Text = ""
    End Try
End Sub
 
Experiment with it and consult the online help about format strings to learn more.
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated