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 standard formatting strings to format Dates in VB .NET
DescriptionThis example shows how to use standard 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. For example, the following code displays the current date in the system's short date format.

    Debug.WriteLine(Now.ToString("d"))

The following list summarizes the standard date formats.

  • d - Short date
  • D - Long date
  • t - Short time
  • T - Long time
  • f - Full (long date, short time)
  • F - Full (long date, long time)
  • g - General (short date, short time)
  • G - General (short date, long time)
  • M - Month and day
  • R - RFC1123 format
  • s - Sortable date/time (ISO 8601)
  • u - Universal date/time
  • U - Universal date/time
  • Y - Year and month

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. It simply calls the current Date's ToString function, passing it the first character in the format string (for example, this might be "d - Short date").

 
Private Sub ShowValue()
    Try
        txtResult.Text = _
            Now.ToString(cboFormat.Text.Substring(0, 1))
    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