Formatting
Dim sResult As String
sResult = System.String.Format("{0:MM\/dd\/yyyy}", System.DateTime.Now())
sResult = "01/06/2002"
Formatting Dates
Dim dtDateTime As System.DateTime
dtDateTime As New System.DateTime(2002, 1, 6, 18, 30, 20)
ToLongDateString
This format does not always display correctly and will depend on local settings.
It only displays the following format when the settings are US
System.DateTime dtDateTime;
dtDateTime = new System.DateTime(2020,1,6,18,30,20);
dtDateTime.ToLongDateString = "Sunday, January 06, 2020"
When the settings are UK the format is as follows:
dtDateTime.ToLongDateString = "January 06, 2002"
ToShortDateString
This format does not always display correctly and will depend on local settings.
dtDateTime.ToShortDateString = 01/06/2002
ToString
Again there are 2 different ways
Dim sDateString As String
sDateString = Now().ToString("dd mmm yy")
sDateString = System.DateTime.ToString(Now())
DateTimeFormatInfo
Defines how DateTime values are formatted and displayed
Added in .NET 2.0
Other Formats
dtDateTime.ToShortTimeString = 6:30 PM
dtDateTime.ToLongTimeString = 6:30:20 PM
dtDateTime.ToFileTime = 126548334205000000
dtDateTime.ToOADate = 37262.7710706019
dtDateTime.ToUniversalTime = 1/6/2002 11:30:20 PM
dtDateTime.ToLocalTime = 1/6/2002 1:30:20 PM
DateTime.ToString
When you want to format a date you have two choices.
(1) Use a built-in standard date format
(2) Use a custom date format
To format a date tou can use the ToString method
System.DateTime oDateTime;
oDateTime = System.DateTime.UtcNow;
System.Diagnostics.Debug.Print(oDateTime.ToString("dd-mm-yyyy"));
Instead of always using a custom date format there are also some built-in standard formats
These standard formats are only one character long.
If the format provided has more than one character it is assumed to be a custom format.
System.Diagnostics.Debug.Print(oDateTime.ToString("d"));
| d | Short Date |
| D | Long Date |
| f | Full Date (Long Date Short Date) |
| F | Full Date (Long Date Long Time) |
| g | General Date (Short Date, Short Time) |
| G | General Date (Short Date, Long Time) |
| m, M | Month Day |
| o, O | |
| r, R | |
| s | |
| t | Short Time |
| T | Long Time |
| u | Universal (Short Date Short Time) |
| U | Universal (Long Date Long Time) |
| y, Y |
System.Diagnostics.Debug.Print(oDateTime.ToString("d", new CultureInfo.CurrentCulture));
System.Diagnostics.Debug.Print(oDateTime.ToString("d", new CultureInfo("en-US"));
System.Diagnostics.Debug.Print(oDateTime.ToString("d", new CultureInfo("en-GB"));
The easiest way to obtain a date/time in a a specific format is to use the ToString method
oDateTime.ToString("dddd, dd mmm yy") = "Monday, 18 Jul 06"
oDateTime.ToString("m") = ??
You must use "MM" and not "mm" when converting a DateTime to a specific text format
oDataRow.Item("Birthday") = Me.dtpClientBirthday.Value.ToString("dd/MM/yyyy")
String to DateTime
String to TimeSpan
System.String.Format
System.String.Format("Current Date is {0:f}", Now()) = "Current Date is Monday, February 08, 2006 4:28 PM"
If you cannot find a standard date and time format that is appropriate you can create your own format putting together the special characters.
System.String.Format("Current Year is {0:yyyy}", Now()) = "Current Year is 2006"
The / and : formatting characters are particularly elusive because they are replaced by the default date and time separator defined for the current locale.
When formatting dates for SQL SELECT statements you want to can make sure that the given separator is used on all occassions by using the backslash escape character to force a specific separator.
System.String.Format("{0:dd\/MM\/yyyy}", Now()) = "01/05/2006"
System.DateTime.Now.ToString("hh:mm:ss") - this returns the current date and time
System.DateTime.Now.ToString(ddd mmmm") - this returns the current date and time
Date Formats
| d | |
| dd | |
| ddd | |
| dddd | |
| M | Month |
| MM | Displays the month as a number with a leading zero |
| MMM | |
| MMMM | |
| y | |
| yy | |
| yyyy | |
| D | |
| f | |
| F | |
| g | |
| G | |
| Y,y | |
| t | |
| T | |
| s | |
| u | |
| U | |
| R,r |
Time Formats
| H | |
| HH | |
| h | |
| hh | |
| m | Minutes |
| mm | |
| s | |
| ss | Seconds with a leading zero |
| t | |
| tt | AM / PM |
| z | |
| zz | |
| zzz | |
| / | |
| : | |
| \ |
© 2026 Better Solutions Limited. All Rights Reserved. © 2026 Better Solutions Limited TopPrevNext