Partager via


Custom Date and Time Format Strings

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Updated: October 2010

A date and time format string defines the text representation of a DateTime or DateTimeOffset value that results from a formatting operation. It can also define the representation of a date and time value that is required in a parsing operation in order to successfully convert the string to a date and time. A custom format string consists of one or more custom date and time format specifiers. Any string that is not a standard date and time format string is interpreted as a custom date and time format string.

NoteNote:

Custom date and time format strings can be used with both DateTime and DateTimeOffset values.

In formatting operations, custom date and time format strings can be used either with the ToString method of a date and time instance or with a method that supports composite formatting. The following example illustrates both uses.

Dim thisDate1 As Date = #6/10/2011#
outputBlock.Text += "Today is " + thisDate1.ToString("MMMM dd, yyyy") + "." & vbCrLf

Dim thisDate2 As New DateTimeOffset(2011, 6, 10, 15, 24, 16, TimeSpan.Zero)
 outputBlock.Text += String.Format("The current date and time: {0:MM/dd/yy H:mm:ss zzz}",  
                   thisDate2) & vbCrLf
' The example displays the following output:
'    Today is June 10, 2011.
'    The current date and time: 06/10/11 15:24:16 +00:00
DateTime thisDate1 = new DateTime(2011, 6, 10);
outputBlock.Text += "Today is " + thisDate1.ToString("MMMM dd, yyyy") + "." + "\n";

DateTimeOffset thisDate2 = new DateTimeOffset(2011, 6, 10, 15, 24, 16,
                                              TimeSpan.Zero);
outputBlock.Text += String.Format("The current date and time: {0:MM/dd/yy H:mm:ss zzz}",
                   thisDate2) + "\n";
// The example displays the following output:
//    Today is June 10, 2011.
//    The current date and time: 06/10/11 15:24:16 +00:00

In parsing operations, custom date and time format strings can be used with the DateTime.ParseExact, DateTime.TryParseExact, DateTimeOffset.ParseExact, and DateTimeOffset.TryParseExact methods. These methods require that an input string conform exactly to a particular pattern for the parse operation to succeed. The following example illustrates a call to the DateTimeOffset.ParseExact(String, String, IFormatProvider) method to parse a date that must include a day, a month, and a two-digit year.

Imports System.Globalization

Module Example
   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim dateValues() As String = { "30-12-2011", "12-30-2011", 
                                      "30-12-11", "12-30-11" }
      Dim pattern As String = "MM-dd-yy"
      Dim parsedDate As Date

      For Each dateValue As String In dateValues
         If DateTime.TryParseExact(dateValue, pattern, Nothing, 
                                   DateTimeStyles.None, parsedDate) Then
            outputBlock.Text += String.Format("Converted '{0}' to {1:d}.",  
                              dateValue, parsedDate) & vbCrLf
         Else
            outputBlock.Text += String.Format("Unable to convert '{0}' to a date and time.",  
                              dateValue) & vbCrLf
         End If
      Next
   End Sub
End Module
' The example displays the following output:
'    Unable to convert '30-12-2011' to a date and time.
'    Unable to convert '12-30-2011' to a date and time.
'    Unable to convert '30-12-11' to a date and time.
'    Converted '12-30-11' to 12/30/2011.
using System;
using System.Globalization;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      string[] dateValues = { "30-12-2011", "12-30-2011", 
                              "30-12-11", "12-30-11" };
      string pattern = "MM-dd-yy";
      DateTime parsedDate;

      foreach (var dateValue in dateValues)
      {
         if (DateTime.TryParseExact(dateValue, pattern, null,
                                   DateTimeStyles.None, out parsedDate))
            outputBlock.Text += String.Format("Converted '{0}' to {1:d}.",
                              dateValue, parsedDate) + "\n";
         else
            outputBlock.Text += String.Format("Unable to convert '{0}' to a date and time.",
                              dateValue) + "\n";
      }
   }
}
// The example displays the following output:
//    Unable to convert '30-12-2011' to a date and time.
//    Unable to convert '12-30-2011' to a date and time.
//    Unable to convert '30-12-11' to a date and time.
//    Converted '12-30-11' to 12/30/2011.

The following table describes the custom date and time format specifiers and displays a result string produced by each format specifier. If a particular format specifier produces a localized result string, the example also notes the culture to which the result string applies. See the Notes section for additional information about using custom date and time format strings.

Format specifier

Description

Examples

"d"

The day of the month, from 1 through 31.

More information: The "d" Custom Format Specifier.

6/1/2009 1:45:30 PM -> 1

6/15/2009 1:45:30 PM -> 15

"dd"

The day of the month, from 01 through 31.

More information: The "dd" Custom Format Specifier.

6/1/2009 1:45:30 PM -> 01

6/15/2009 1:45:30 PM -> 15

"ddd"

The abbreviated name of the day of the week.

More information: The "ddd" Custom Format Specifier.

6/15/2009 1:45:30 PM -> Mon (en-US)

6/15/2009 1:45:30 PM -> Пн (ru-RU)

6/15/2009 1:45:30 PM -> lun. (fr-FR)

"dddd"

The full name of the day of the week.

More information: The "dddd" Custom Format Specifier.

6/15/2009 1:45:30 PM -> Monday (en-US)

6/15/2009 1:45:30 PM -> понедельник (ru-RU)

6/15/2009 1:45:30 PM -> lundi (fr-FR)

"f"

The tenths of a second in a date and time value.

More information: The "f" Custom Format Specifier.

6/15/2009 13:45:30.617 -> 6

6/15/2009 13:45:30.050 -> 0

"ff"

The hundredths of a second in a date and time value.

More information: The "ff" Custom Format Specifier.

6/15/2009 13:45:30.617 -> 61

6/15/2009 13:45:30.005 -> 00

"fff"

The milliseconds in a date and time value.

More information: The "fff" Custom Format Specifier.

6/15/2009 13:45:30.617 -> 617

6/15/2009 13:45:30.0005 -> 000

"ffff"

The ten thousandths of a second in a date and time value.

More information: The "ffff" Custom Format Specifier.

6/15/2009 13:45:30.6175 -> 6175

6/15/2009 13:45:30.00005 -> 0000

"fffff"

The hundred thousandths of a second in a date and time value.

More information: The "fffff" Custom Format Specifier.

6/15/2009 13:45:30.61754 -> 61754

6/15/2009 13:45:30.000005 -> 00000

"ffffff"

The millionths of a second in a date and time value.

More information: The "ffffff" Custom Format Specifier.

6/15/2009 13:45:30.617542 -> 617542

6/15/2009 13:45:30.0000005 -> 000000

"fffffff"

The ten millionths of a second in a date and time value.

More information: The "fffffff" Custom Format Specifier.

6/15/2009 13:45:30.6175425 -> 6175425

6/15/2009 13:45:30.0001150 -> 0001150

"F"

If non-zero, the tenths of a second in a date and time value.

More information: The "F" Custom Format Specifier.

6/15/2009 13:45:30.617 -> 6

6/15/2009 13:45:30.050 -> (no output)

"FF"

If non-zero, the hundredths of a second in a date and time value.

More information: The "FF" Custom Format Specifier.

6/15/2009 13:45:30.617 -> 61

6/15/2009 13:45:30.005 -> (no output)

"FFF"

If non-zero, the milliseconds in a date and time value.

More information: The "FFF" Custom Format Specifier.

6/15/2009 13:45:30.617 -> 617

6/15/2009 13:45:30.0005 -> (no output)

"FFFF"

If non-zero, the ten thousandths of a second in a date and time value.

More information: The "FFFF" Custom Format Specifier.

6/1/2009 13:45:30.5275 -> 5275

6/15/2009 13:45:30.00005 -> (no output)

"FFFFF"

If non-zero, the hundred thousandths of a second in a date and time value.

More information: The "FFFFF" Custom Format Specifier.

6/15/2009 13:45:30.61754 -> 61754

6/15/2009 13:45:30.000005 -> (no output)

"FFFFFF"

If non-zero, the millionths of a second in a date and time value.

More information: The "FFFFFF" Custom Format Specifier.

6/15/2009 13:45:30.617542 -> 617542

6/15/2009 13:45:30.0000005 -> (no output)

"FFFFFFF"

If non-zero, the ten millionths of a second in a date and time value.

More information: The "FFFFFFF" Custom Format Specifier.

6/15/2009 13:45:30.6175425 -> 6175425

6/15/2009 13:45:30.0001150 -> 000115

"g", "gg"

The period or era.

More information: The "g" or "gg" Custom Format Specifier.

6/15/2009 1:45:30 PM -> A.D.

"h"

The hour, using a 12-hour clock from 1 to 12.

More information: The "h" Custom Format Specifier.

6/15/2009 1:45:30 AM -> 1

6/15/2009 1:45:30 PM -> 1

"hh"

The hour, using a 12-hour clock from 01 to 12.

More information: The "hh" Custom Format Specifier.

6/15/2009 1:45:30 AM -> 01

6/15/2009 1:45:30 PM -> 01

"H"

The hour, using a 24-hour clock from 0 to 23.

More information: The "H" Custom Format Specifier.

6/15/2009 1:45:30 AM -> 1

6/15/2009 1:45:30 PM -> 13

"HH"

The hour, using a 24-hour clock from 00 to 23.

More information: The "HH" Custom Format Specifier.

6/15/2009 1:45:30 AM -> 01

6/15/2009 1:45:30 PM -> 13

"K"

Time zone information.

More information: The "K" Custom Format Specifier.

6/15/2009 1:45:30 PM, Kind Unspecified ->

6/15/2009 1:45:30 PM, Kind Utc -> Z

6/15/2009 1:45:30 PM, Kind Local -> -07:00

"m"

The minute, from 0 through 59.

More information: The "m" Custom Format Specifier.

6/15/2009 1:09:30 AM -> 9

6/15/2009 1:09:30 PM -> 9

"mm"

The minute, from 00 through 59.

More information: The "mm" Custom Format Specifier.

6/15/2009 1:09:30 AM -> 09

6/15/2009 1:09:30 PM -> 09

"M"

The month, from 1 through 12.

More information: The "M" Custom Format Specifier.

6/15/2009 1:45:30 PM -> 6

"MM"

The month, from 01 through 12.

More information: The "MM" Custom Format Specifier.

6/15/2009 1:45:30 PM -> 06

"MMM"

The abbreviated name of the month.

More information: The "MMM" Custom Format Specifier.

6/15/2009 1:45:30 PM -> Jun (en-US)

6/15/2009 1:45:30 PM -> juin (fr-FR)

6/15/2009 1:45:30 PM -> Jun (zu-ZA)

"MMMM"

The full name of the month.

More information: The "MMMM" Custom Format Specifier.

6/15/2009 1:45:30 PM -> June (en-US)

6/15/2009 1:45:30 PM -> juni (da-DK)

6/15/2009 1:45:30 PM -> uJuni (zu-ZA)

"s"

The second, from 0 through 59.

More information: The "s" Custom Format Specifier.

6/15/2009 1:45:09 PM -> 9

"ss"

The second, from 00 through 59.

More information: The "ss" Custom Format Specifier.

6/15/2009 1:45:09 PM -> 09

"t"

The first character of the AM/PM designator.

More information: The "t" Custom Format Specifier.

6/15/2009 1:45:30 PM -> P (en-US)

6/15/2009 1:45:30 PM -> 午 (ja-JP)

6/15/2009 1:45:30 PM -> (fr-FR)

"tt"

The AM/PM designator.

More information: The "tt" Custom Format Specifier.

6/15/2009 1:45:30 PM -> PM (en-US)

6/15/2009 1:45:30 PM -> 午後 (ja-JP)

6/15/2009 1:45:30 PM -> (fr-FR)

"y"

The year, from 0 to 99.

More information: The "y" Custom Format Specifier.

1/1/0001 12:00:00 AM -> 1

1/1/0900 12:00:00 AM -> 0

1/1/1900 12:00:00 AM -> 0

6/15/2009 1:45:30 PM -> 9

"yy"

The year, from 00 to 99.

More information: The "yy" Custom Format Specifier.

1/1/0001 12:00:00 AM -> 01

1/1/0900 12:00:00 AM -> 00

1/1/1900 12:00:00 AM -> 00

6/15/2009 1:45:30 PM -> 09

"yyy"

The year, with a minimum of three digits.

More information: The "yyy" Custom Format Specifier.

1/1/0001 12:00:00 AM -> 001

1/1/0900 12:00:00 AM -> 900

1/1/1900 12:00:00 AM -> 1900

6/15/2009 1:45:30 PM -> 2009

"yyyy"

The year as a four-digit number.

More information: The "yyyy" Custom Format Specifier.

1/1/0001 12:00:00 AM -> 0001

1/1/0900 12:00:00 AM -> 0900

1/1/1900 12:00:00 AM -> 1900

6/15/2009 1:45:30 PM -> 2009

"yyyyy"

The year as a five-digit number.

More information: The "yyyyy" Custom Format Specifier.

1/1/0001 12:00:00 AM -> 00001

6/15/2009 1:45:30 PM -> 02009

"z"

Hours offset from UTC, with no leading zeros.

More information: The "z" Custom Format Specifier.

6/15/2009 1:45:30 PM -07:00 -> -7

"zz"

Hours offset from UTC, with a leading zero for a single-digit value.

More information: The "zz" Custom Format Specifier.

6/15/2009 1:45:30 PM -07:00 -> -07

"zzz"

Hours and minutes offset from UTC.

More information: The "zzz" Custom Format Specifier.

6/15/2009 1:45:30 PM -07:00 -> -07:00

":"

The time separator.

More information: The ":" Custom Format Specifier.

6/15/2009 1:45:30 PM -> : (en-US)

6/15/2009 1:45:30 PM -> . (it-IT)

6/15/2009 1:45:30 PM -> : (ja-JP)

"/"

The date separator.

More Information: The "/" Custom Format Specifier.

6/15/2009 1:45:30 PM -> / (en-US)

6/15/2009 1:45:30 PM -> - (ar-DZ)

6/15/2009 1:45:30 PM -> . (tr-TR)

"string"

'string'

Literal string delimiter.

6/15/2009 1:45:30 PM ("arr:" h:m t) -> arr: 1:45 P

6/15/2009 1:45:30 PM ('arr:' h:m t) -> arr: 1:45 P

%

Defines the following character as a custom format specifier.

More information: Using Single Custom Format Specifiers.

6/15/2009 1:45:30 PM (%h) -> 1

\

The escape character.

6/15/2009 1:45:30 PM (h \h) -> 1 h

Any other character

The character is copied to the result string unchanged.

More information: Using the Escape Character.

6/15/2009 1:45:30 AM (arr hh:mm t) -> arr 01:45 A

The following sections provide additional information about each custom date and time format specifier. Unless otherwise noted, each specifier produces an identical string representation regardless of whether it is used with a DateTime value or a DateTimeOffset value.

The "d" Custom Format Specifier

The "d" custom format specifier represents the day of the month as a number from 1 through 31. A single-digit day is formatted without a leading zero.

If the "d" format specifier is used without other custom format specifiers, it is interpreted as the "d" standard date and time format specifier. For more information about using a single format specifier, see Using Single Custom Format Specifiers later in this topic.

The following example includes the "d" custom format specifier in several format strings.

Dim date1 As Date = #8/29/2008 7:27:15 PM#

outputBlock.Text &= date1.ToString("d, M", _
                  CultureInfo.InvariantCulture) & vbCrLf
' Displays 29, 8

outputBlock.Text &= date1.ToString("d MMMM", New CultureInfo("en-US")) & vbCrLf
' Displays 29 August
outputBlock.Text &= date1.ToString("d MMMM", New CultureInfo("es-MX")) & vbCrLf
' Displays 29 agosto                                                
DateTime date1 = new DateTime(2008, 8, 29, 19, 27, 15);

outputBlock.Text += date1.ToString("d, M",
                  CultureInfo.InvariantCulture) + "\n";
// Displays 29, 8

outputBlock.Text += date1.ToString("d MMMM",
                                   new CultureInfo("en-US")) + "\n";
// Displays 29 August
outputBlock.Text += date1.ToString("d MMMM",
                                   new CultureInfo("es-MX")) + "\n";
// Displays 29 agosto                                                

Back to table

The "dd" Custom Format Specifier

The "dd" custom format string represents the day of the month as a number from 01 through 31. A single-digit day is formatted with a leading zero.

The following example includes the "dd" custom format specifier in a custom format string.

Dim date1 As Date = #1/2/2008 6:30:15 AM#

outputBlock.Text &= date1.ToString("dd, MM", _
                  CultureInfo.InvariantCulture) & vbCrLf
' 02, 01
DateTime date1 = new DateTime(2008, 1, 2, 6, 30, 15);

outputBlock.Text += date1.ToString("dd, MM",
                  CultureInfo.InvariantCulture) + "\n";
// 02, 01

Back to table

The "ddd" Custom Format Specifier

The "ddd" custom format specifier represents the abbreviated name of the day of the week. The localized abbreviated name of the day of the week is retrieved from the DateTimeFormatInfo.AbbreviatedDayNames property of the current or specified culture.

The following example includes the "ddd" custom format specifier in a custom format string.

Dim date1 As Date = #8/29/2008 7:27:15 PM#

outputBlock.Text &= date1.ToString("ddd d MMM", New CultureInfo("en-US")) & vbCrLf
' Displays Fri 29 Aug
outputBlock.Text &= date1.ToString("ddd d MMM", New CultureInfo("fr-FR")) & vbCrLf
' Displays ven. 29 ao?t                                                
DateTime date1 = new DateTime(2008, 8, 29, 19, 27, 15);

outputBlock.Text += date1.ToString("ddd d MMM",
                                   new CultureInfo("en-US")) + "\n";
// Displays Fri 29 Aug
outputBlock.Text += date1.ToString("ddd d MMM", 
                                   new CultureInfo("fr-FR")) + "\n";
// Displays ven. 29 ao�t                                                

Back to table

The "dddd" Custom Format Specifier

The "dddd" custom format specifier (plus any number of additional "d" specifiers) represents the full name of the day of the week. The localized name of the day of the week is retrieved from the DateTimeFormatInfo.DayNames property of the current or specified culture.

The following example includes the "dddd" custom format specifier in a custom format string.

Dim date1 As Date = #8/29/2008 7:27:15 PM#

outputBlock.Text &= date1.ToString("dddd dd MMMM", New CultureInfo("en-US")) & vbCrLf
' Displays Friday 29 August
outputBlock.Text &= date1.ToString("dddd dd MMMM", New CultureInfo("it-IT")) & vbCrLf
' Displays venerd? 29 agosto                                          
DateTime date1 = new DateTime(2008, 8, 29, 19, 27, 15);

outputBlock.Text += date1.ToString("dddd dd MMMM",
                                   new CultureInfo("en-US"))  + "\n";
// Displays Friday 29 August
outputBlock.Text += date1.ToString("dddd dd MMMM",
                                   new CultureInfo("it-IT"))  + "\n";
// Displays venerd� 29 agosto                                

Back to table

The "f" Custom Format Specifier

The "f" custom format specifier represents the most significant digit of the seconds fraction; that is, it represents the tenths of a second in a date and time value.

If the "f" format specifier is used without other format specifiers, it is interpreted as the "f" standard date and time format specifier. For more information about using a single format specifier, see Using Single Custom Format Specifiers later in this topic.

When you use "f" format specifiers as part of a format string supplied to the ParseExact, TryParseExact, ParseExact, or TryParseExact method, the number of "f" format specifiers indicates the number of most significant digits of the seconds fraction that must be present to successfully parse the string.

The following example includes the "f" custom format specifier in a custom format string.

Dim date1 As New Date(2008, 8, 29, 19, 27, 15, 18)
Dim ci As CultureInfo = CultureInfo.InvariantCulture

outputBlock.Text &= String.Format(date1.ToString("hh:mm:ss.f", ci)) & vbCrLf
' Displays 07:27:15.0
outputBlock.Text &= String.Format(date1.ToString("hh:mm:ss.F", ci)) & vbCrLf
' Displays 07:27:15
outputBlock.Text &= String.Format(date1.ToString("hh:mm:ss.ff", ci)) & vbCrLf
' Displays 07:27:15.01
outputBlock.Text &= String.Format(date1.ToString("hh:mm:ss.FF", ci)) & vbCrLf
' Displays 07:27:15.01
outputBlock.Text &= String.Format(date1.ToString("hh:mm:ss.fff", ci)) & vbCrLf
' Displays 07:27:15.018
outputBlock.Text &= String.Format(date1.ToString("hh:mm:ss.FFF", ci)) & vbCrLf
' Displays 07:27:15.018
DateTime date1 = new DateTime(2008, 8, 29, 19, 27, 15, 18);
CultureInfo ci = CultureInfo.InvariantCulture;

outputBlock.Text += String.Format(date1.ToString("hh:mm:ss.f", ci)) + "\n";
// Displays 07:27:15.0
outputBlock.Text += String.Format(date1.ToString("hh:mm:ss.F", ci)) + "\n";
// Displays 07:27:15
outputBlock.Text += String.Format(date1.ToString("hh:mm:ss.ff", ci)) + "\n";
// Displays 07:27:15.01
outputBlock.Text += String.Format(date1.ToString("hh:mm:ss.FF", ci)) + "\n";
// Displays 07:27:15.01
outputBlock.Text += String.Format(date1.ToString("hh:mm:ss.fff", ci)) + "\n";
// Displays 07:27:15.018
outputBlock.Text += String.Format(date1.ToString("hh:mm:ss.FFF", ci)) + "\n";
// Displays 07:27:15.018

Back to table

The "ff" Custom Format Specifier

The "ff" custom format specifier represents the two most significant digits of the seconds fraction; that is, it represents the hundredths of a second in a date and time value.

following example includes the "ff" custom format specifier in a custom format string.

Dim date1 As New Date(2008, 8, 29, 19, 27, 15, 18)
Dim ci As CultureInfo = CultureInfo.InvariantCulture

outputBlock.Text &= String.Format(date1.ToString("hh:mm:ss.f", ci)) & vbCrLf
' Displays 07:27:15.0
outputBlock.Text &= String.Format(date1.ToString("hh:mm:ss.F", ci)) & vbCrLf
' Displays 07:27:15
outputBlock.Text &= String.Format(date1.ToString("hh:mm:ss.ff", ci)) & vbCrLf
' Displays 07:27:15.01
outputBlock.Text &= String.Format(date1.ToString("hh:mm:ss.FF", ci)) & vbCrLf
' Displays 07:27:15.01
outputBlock.Text &= String.Format(date1.ToString("hh:mm:ss.fff", ci)) & vbCrLf
' Displays 07:27:15.018
outputBlock.Text &= String.Format(date1.ToString("hh:mm:ss.FFF", ci)) & vbCrLf
' Displays 07:27:15.018
DateTime date1 = new DateTime(2008, 8, 29, 19, 27, 15, 18);
CultureInfo ci = CultureInfo.InvariantCulture;

outputBlock.Text += String.Format(date1.ToString("hh:mm:ss.f", ci)) + "\n";
// Displays 07:27:15.0
outputBlock.Text += String.Format(date1.ToString("hh:mm:ss.F", ci)) + "\n";
// Displays 07:27:15
outputBlock.Text += String.Format(date1.ToString("hh:mm:ss.ff", ci)) + "\n";
// Displays 07:27:15.01
outputBlock.Text += String.Format(date1.ToString("hh:mm:ss.FF", ci)) + "\n";
// Displays 07:27:15.01
outputBlock.Text += String.Format(date1.ToString("hh:mm:ss.fff", ci)) + "\n";
// Displays 07:27:15.018
outputBlock.Text += String.Format(date1.ToString("hh:mm:ss.FFF", ci)) + "\n";
// Displays 07:27:15.018

Back to table

The "fff" Custom Format Specifier

The "fff" custom format specifier represents the three most significant digits of the seconds fraction; that is, it represents the milliseconds in a date and time value.

The following example includes the "fff" custom format specifier in a custom format string.

Dim date1 As New Date(2008, 8, 29, 19, 27, 15, 18)
Dim ci As CultureInfo = CultureInfo.InvariantCulture

outputBlock.Text &= String.Format(date1.ToString("hh:mm:ss.f", ci)) & vbCrLf
' Displays 07:27:15.0
outputBlock.Text &= String.Format(date1.ToString("hh:mm:ss.F", ci)) & vbCrLf
' Displays 07:27:15
outputBlock.Text &= String.Format(date1.ToString("hh:mm:ss.ff", ci)) & vbCrLf
' Displays 07:27:15.01
outputBlock.Text &= String.Format(date1.ToString("hh:mm:ss.FF", ci)) & vbCrLf
' Displays 07:27:15.01
outputBlock.Text &= String.Format(date1.ToString("hh:mm:ss.fff", ci)) & vbCrLf
' Displays 07:27:15.018
outputBlock.Text &= String.Format(date1.ToString("hh:mm:ss.FFF", ci)) & vbCrLf
' Displays 07:27:15.018
DateTime date1 = new DateTime(2008, 8, 29, 19, 27, 15, 18);
CultureInfo ci = CultureInfo.InvariantCulture;

outputBlock.Text += String.Format(date1.ToString("hh:mm:ss.f", ci)) + "\n";
// Displays 07:27:15.0
outputBlock.Text += String.Format(date1.ToString("hh:mm:ss.F", ci)) + "\n";
// Displays 07:27:15
outputBlock.Text += String.Format(date1.ToString("hh:mm:ss.ff", ci)) + "\n";
// Displays 07:27:15.01
outputBlock.Text += String.Format(date1.ToString("hh:mm:ss.FF", ci)) + "\n";
// Displays 07:27:15.01
outputBlock.Text += String.Format(date1.ToString("hh:mm:ss.fff", ci)) + "\n";
// Displays 07:27:15.018
outputBlock.Text += String.Format(date1.ToString("hh:mm:ss.FFF", ci)) + "\n";
// Displays 07:27:15.018

Back to table

The "ffff" Custom Format Specifier

The "ffff" custom format specifier represents the four most significant digits of the seconds fraction; that is, it represents the ten thousandths of a second in a date and time value.

Although it is possible to display the ten thousandths of a second component of a time value, that value may not be meaningful. The precision of date and time values depends on the resolution of the system clock. On the Windows NT version 3.5 (and later) and Windows Vista operating systems, the clock's resolution is approximately 10-15 milliseconds.

Back to table

The "fffff" Custom Format Specifier

Represents the five most significant digits of the seconds fraction; that is, it represents the hundred thousandths of a second in a date and time value.

Although it is possible to display the hundred thousandths of a second component of a time value, that value may not be meaningful. The precision of date and time values depends on the resolution of the system clock. On the Windows NT 3.5 (and later) and Windows Vista operating systems, the clock's resolution is approximately 10-15 milliseconds.

Back to table

The "ffffff" Custom Format Specifier

The "ffffff" custom format specifier represents the six most significant digits of the seconds fraction; that is, it represents the millionths of a second in a date and time value.

Although it is possible to display the millionths of a second component of a time value, that value may not be meaningful. The precision of date and time values depends on the resolution of the system clock. On the Windows NT 3.5 (and later) and Windows Vista operating systems, the clock's resolution is approximately 10-15 milliseconds.

Back to table

The "fffffff" Custom Format Specifier

The "fffffff" custom format specifier represents the seven most significant digits of the seconds fraction; that is, it represents the ten millionths of a second in a date and time value.

Although it is possible to display the ten millionths of a second component of a time value, that value may not be meaningful. The precision of date and time values depends on the resolution of the system clock. On the Windows NT 3.5 (and later) and Windows Vista operating systems, the clock's resolution is approximately 10-15 milliseconds.

Back to table

The "F" Custom Format Specifier

The "F" custom format specifier represents the most significant digit of the seconds fraction; that is, it represents the tenths of a second in a date and time value. Nothing is displayed if the digit is zero.

If the "F" format specifier is used without other format specifiers, it is interpreted as the "F" standard date and time format specifier. For more information about using a single format specifier, see Using Single Custom Format Specifiers later in this topic.

The number of "F" format specifiers used with the ParseExact, TryParseExact, ParseExact, or TryParseExact method indicates the maximum number of most significant digits of the seconds fraction that can be present to successfully parse the string.

The following example includes the "F" custom format specifier in a custom format string.

Dim date1 As New Date(2008, 8, 29, 19, 27, 15, 18)
Dim ci As CultureInfo = CultureInfo.InvariantCulture

outputBlock.Text &= String.Format(date1.ToString("hh:mm:ss.f", ci)) & vbCrLf
' Displays 07:27:15.0
outputBlock.Text &= String.Format(date1.ToString("hh:mm:ss.F", ci)) & vbCrLf
' Displays 07:27:15
outputBlock.Text &= String.Format(date1.ToString("hh:mm:ss.ff", ci)) & vbCrLf
' Displays 07:27:15.01
outputBlock.Text &= String.Format(date1.ToString("hh:mm:ss.FF", ci)) & vbCrLf
' Displays 07:27:15.01
outputBlock.Text &= String.Format(date1.ToString("hh:mm:ss.fff", ci)) & vbCrLf
' Displays 07:27:15.018
outputBlock.Text &= String.Format(date1.ToString("hh:mm:ss.FFF", ci)) & vbCrLf
' Displays 07:27:15.018
DateTime date1 = new DateTime(2008, 8, 29, 19, 27, 15, 18);
CultureInfo ci = CultureInfo.InvariantCulture;

outputBlock.Text += String.Format(date1.ToString("hh:mm:ss.f", ci)) + "\n";
// Displays 07:27:15.0
outputBlock.Text += String.Format(date1.ToString("hh:mm:ss.F", ci)) + "\n";
// Displays 07:27:15
outputBlock.Text += String.Format(date1.ToString("hh:mm:ss.ff", ci)) + "\n";
// Displays 07:27:15.01
outputBlock.Text += String.Format(date1.ToString("hh:mm:ss.FF", ci)) + "\n";
// Displays 07:27:15.01
outputBlock.Text += String.Format(date1.ToString("hh:mm:ss.fff", ci)) + "\n";
// Displays 07:27:15.018
outputBlock.Text += String.Format(date1.ToString("hh:mm:ss.FFF", ci)) + "\n";
// Displays 07:27:15.018

Back to table

The "FF" Custom Format Specifier

The "FF" custom format specifier represents the two most significant digits of the seconds fraction; that is, it represents the hundredths of a second in a date and time value. However, trailing zeros or two zero digits are not displayed.

The following example includes the "FF" custom format specifier in a custom format string.

Dim date1 As New Date(2008, 8, 29, 19, 27, 15, 18)
Dim ci As CultureInfo = CultureInfo.InvariantCulture

outputBlock.Text &= String.Format(date1.ToString("hh:mm:ss.f", ci)) & vbCrLf
' Displays 07:27:15.0
outputBlock.Text &= String.Format(date1.ToString("hh:mm:ss.F", ci)) & vbCrLf
' Displays 07:27:15
outputBlock.Text &= String.Format(date1.ToString("hh:mm:ss.ff", ci)) & vbCrLf
' Displays 07:27:15.01
outputBlock.Text &= String.Format(date1.ToString("hh:mm:ss.FF", ci)) & vbCrLf
' Displays 07:27:15.01
outputBlock.Text &= String.Format(date1.ToString("hh:mm:ss.fff", ci)) & vbCrLf
' Displays 07:27:15.018
outputBlock.Text &= String.Format(date1.ToString("hh:mm:ss.FFF", ci)) & vbCrLf
' Displays 07:27:15.018
DateTime date1 = new DateTime(2008, 8, 29, 19, 27, 15, 18);
CultureInfo ci = CultureInfo.InvariantCulture;

outputBlock.Text += String.Format(date1.ToString("hh:mm:ss.f", ci)) + "\n";
// Displays 07:27:15.0
outputBlock.Text += String.Format(date1.ToString("hh:mm:ss.F", ci)) + "\n";
// Displays 07:27:15
outputBlock.Text += String.Format(date1.ToString("hh:mm:ss.ff", ci)) + "\n";
// Displays 07:27:15.01
outputBlock.Text += String.Format(date1.ToString("hh:mm:ss.FF", ci)) + "\n";
// Displays 07:27:15.01
outputBlock.Text += String.Format(date1.ToString("hh:mm:ss.fff", ci)) + "\n";
// Displays 07:27:15.018
outputBlock.Text += String.Format(date1.ToString("hh:mm:ss.FFF", ci)) + "\n";
// Displays 07:27:15.018

Back to table

The "FFF" Custom Format Specifier

The "FFF" custom format specifier represents the three most significant digits of the seconds fraction; that is, it represents the milliseconds in a date and time value. However, trailing zeros or three zero digits are not displayed.

The following example includes the "FFF" custom format specifier in a custom format string.

Dim date1 As New Date(2008, 8, 29, 19, 27, 15, 18)
Dim ci As CultureInfo = CultureInfo.InvariantCulture

outputBlock.Text &= String.Format(date1.ToString("hh:mm:ss.f", ci)) & vbCrLf
' Displays 07:27:15.0
outputBlock.Text &= String.Format(date1.ToString("hh:mm:ss.F", ci)) & vbCrLf
' Displays 07:27:15
outputBlock.Text &= String.Format(date1.ToString("hh:mm:ss.ff", ci)) & vbCrLf
' Displays 07:27:15.01
outputBlock.Text &= String.Format(date1.ToString("hh:mm:ss.FF", ci)) & vbCrLf
' Displays 07:27:15.01
outputBlock.Text &= String.Format(date1.ToString("hh:mm:ss.fff", ci)) & vbCrLf
' Displays 07:27:15.018
outputBlock.Text &= String.Format(date1.ToString("hh:mm:ss.FFF", ci)) & vbCrLf
' Displays 07:27:15.018
DateTime date1 = new DateTime(2008, 8, 29, 19, 27, 15, 18);
CultureInfo ci = CultureInfo.InvariantCulture;

outputBlock.Text += String.Format(date1.ToString("hh:mm:ss.f", ci)) + "\n";
// Displays 07:27:15.0
outputBlock.Text += String.Format(date1.ToString("hh:mm:ss.F", ci)) + "\n";
// Displays 07:27:15
outputBlock.Text += String.Format(date1.ToString("hh:mm:ss.ff", ci)) + "\n";
// Displays 07:27:15.01
outputBlock.Text += String.Format(date1.ToString("hh:mm:ss.FF", ci)) + "\n";
// Displays 07:27:15.01
outputBlock.Text += String.Format(date1.ToString("hh:mm:ss.fff", ci)) + "\n";
// Displays 07:27:15.018
outputBlock.Text += String.Format(date1.ToString("hh:mm:ss.FFF", ci)) + "\n";
// Displays 07:27:15.018

Back to table

The "FFFF" Custom Format Specifier

The "FFFF" custom format specifier represents the four most significant digits of the seconds fraction; that is, it represents the ten thousandths of a second in a date and time value. However, trailing zeros or four zero digits are not displayed.

Although it is possible to display the ten thousandths of a second component of a time value, that value may not be meaningful. The precision of date and time values depends on the resolution of the system clock. On the Windows NT 3.5 (and later) and Windows Vista operating systems, the clock's resolution is approximately 10-15 milliseconds.

Back to table

The "FFFFF" Custom Format Specifier

The "FFFFF" custom format specifier represents the five most significant digits of the seconds fraction; that is, it represents the hundred thousandths of a second in a date and time value. However, trailing zeros or five zero digits are not displayed.

Although it is possible to display the hundred thousandths of a second component of a time value, that value may not be meaningful. The precision of date and time values depends on the resolution of the system clock. On the Windows NT 3.5 (and later) and Windows Vista operating systems, the clock's resolution is approximately 10-15 milliseconds.

Back to table

The "FFFFFF" Custom Format Specifier

The "FFFFFF" custom format specifier represents the six most significant digits of the seconds fraction; that is, it represents the millionths of a second in a date and time value. However, trailing zeros or six zero digits are not displayed.

Although it is possible to display the millionths of a second component of a time value, that value may not be meaningful. The precision of date and time values depends on the resolution of the system clock. On the Windows NT 3.5 (and later) and Windows Vista operating systems, the clock's resolution is approximately 10-15 milliseconds.

Back to table

The "FFFFFFF" Custom Format Specifier

The "FFFFFFF" custom format specifier represents the seven most significant digits of the seconds fraction; that is, it represents the ten millionths of a second in a date and time value. However, trailing zeros or seven zero digits are not displayed.

Although it is possible to display the ten millionths of a second component of a time value, that value may not be meaningful. The precision of date and time values depends on the resolution of the system clock. On the Windows NT 3.5 (and later) and Windows Vista operating systems, the clock's resolution is approximately 10-15 milliseconds.

Back to table

The "g" or "gg" Custom Format Specifier

The "g" or "gg" custom format specifiers (plus any number of additional "g" specifiers) represents the period or era, such as A.D. The formatting operation ignores this specifier if the date to be formatted does not have an associated period or era string.

If the "g" format specifier is used without other custom format specifiers, it is interpreted as the "g" standard date and time format specifier. For more information about using a single format specifier, see Using Single Custom Format Specifiers later in this topic.

The following example includes the "g" custom format specifier in a custom format string.

Dim date1 As Date = #8/4/0070#

outputBlock.Text &= date1.ToString("MM/dd/yyyy g", _
                  CultureInfo.InvariantCulture) & vbCrLf
' Displays 08/04/0070 A.D.                        
outputBlock.Text &= date1.ToString("MM/dd/yyyy g", New CultureInfo("fr-FR")) & vbCrLf
' Displays 08/04/0070 ap. J.-C.
DateTime date1 = new DateTime(70, 08, 04);

outputBlock.Text += date1.ToString("MM/dd/yyyy g", 
                                   CultureInfo.InvariantCulture)  + "\n";
// Displays 08/04/0070 A.D.                        
outputBlock.Text += date1.ToString("MM/dd/yyyy g",
                                   new CultureInfo("fr-FR"))  + "\n";
// Displays 08/04/0070 ap. J.-C.

Back to table

The "h" Custom Format Specifier

The "h" custom format specifier represents the hour as a number from 1 through 12; that is, the hour is represented by a 12-hour clock that counts the whole hours since midnight or noon. A particular hour after midnight is indistinguishable from the same hour after noon. The hour is not rounded, and a single-digit hour is formatted without a leading zero. For example, given a time of 5:43 in the morning or afternoon, this custom format specifier displays "5".

If the "h" format specifier is used without other custom format specifiers, it is interpreted as a standard date and time format specifier and throws a FormatException. For more information about using a single format specifier, see Using Single Custom Format Specifiers later in this topic.

The following example includes the "h" custom format specifier in a custom format string.

Dim date1 As Date
date1 = #6:09:01 PM#
outputBlock.Text &= date1.ToString("h:m:s.F t", _
                  CultureInfo.InvariantCulture) & vbCrLf
' Displays 6:9:1 P
outputBlock.Text &= date1.ToString("h:m:s.F t", New CultureInfo("el-GR")) & vbCrLf
' Displays 6:9:1 ?                        
date1 = New Date(2008, 1, 1, 18, 9, 1, 500)
outputBlock.Text &= date1.ToString("h:m:s.F t", _
                  CultureInfo.InvariantCulture) & vbCrLf
' Displays 6:9:1.5 P
outputBlock.Text &= date1.ToString("h:m:s.F t", New CultureInfo("el-GR")) & vbCrLf
' Displays 6:9:1.5 ?
DateTime date1;
date1 = new DateTime(2008, 1, 1, 18, 9, 1);
outputBlock.Text += date1.ToString("h:m:s.F t",
                                   CultureInfo.InvariantCulture)  + "\n";
// Displays 6:9:1 P
outputBlock.Text += date1.ToString("h:m:s.F t",
                  new CultureInfo("el-GR")) + "\n";
// Displays 6:9:1 �                        
date1 = new DateTime(2008, 1, 1, 18, 9, 1, 500);
outputBlock.Text += date1.ToString("h:m:s.F t",
                  CultureInfo.InvariantCulture) + "\n";
// Displays 6:9:1.5 P
outputBlock.Text += date1.ToString("h:m:s.F t",
                  new CultureInfo("el-GR")) + "\n";
// Displays 6:9:1.5 �

Back to table

The "hh" Custom Format Specifier

The "hh" custom format specifier (plus any number of additional "h" specifiers) represents the hour as a number from 01 through 12; that is, the hour is represented by a 12-hour clock that counts the whole hours since midnight or noon. A particular hour after midnight is indistinguishable from the same hour after noon. The hour is not rounded, and a single-digit hour is formatted with a leading zero. For example, given a time of 5:43 in the morning or afternoon, this format specifier displays "05".

The following example includes the "hh" custom format specifier in a custom format string.

Dim date1 As Date
date1 = #6:09:01 PM#
outputBlock.Text &= date1.ToString("hh:mm:ss tt", _
                  CultureInfo.InvariantCulture) & vbCrLf
' Displays 06:09:01 PM                        
outputBlock.Text &= date1.ToString("hh:mm:ss tt", New CultureInfo("hu-HU")) & vbCrLf
' Displays 06:09:01 du.
date1 = New Date(2008, 1, 1, 18, 9, 1, 500)
outputBlock.Text &= date1.ToString("hh:mm:ss.ff tt", _
                  CultureInfo.InvariantCulture) & vbCrLf
' Displays 06:09:01.50 PM                        
outputBlock.Text &= date1.ToString("hh:mm:ss.ff tt", New CultureInfo("hu-HU")) & vbCrLf
' Displays 06:09:01.50 du.
DateTime date1;
date1 = new DateTime(2008, 1, 1, 18, 9, 1);
outputBlock.Text += date1.ToString("hh:mm:ss tt",
                  CultureInfo.InvariantCulture) + "\n";
// Displays 06:09:01 PM                        
outputBlock.Text += date1.ToString("hh:mm:ss tt",
                  new CultureInfo("hu-HU")) + "\n";
// Displays 06:09:01 du.
date1 = new DateTime(2008, 1, 1, 18, 9, 1, 500);
outputBlock.Text += date1.ToString("hh:mm:ss.ff tt",
                  CultureInfo.InvariantCulture) + "\n";
// Displays 06:09:01.50 PM                        
outputBlock.Text += date1.ToString("hh:mm:ss.ff tt",
                  new CultureInfo("hu-HU")) + "\n";
// Displays 06:09:01.50 du.

Back to table

The "H" Custom Format Specifier

The "H" custom format specifier represents the hour as a number from 0 through 23; that is, the hour is represented by a zero-based 24-hour clock that counts the hours since midnight. A single-digit hour is formatted without a leading zero.

If the "H" format specifier is used without other custom format specifiers, it is interpreted as a standard date and time format specifier and throws a FormatException. For more information about using a single format specifier, see Using Single Custom Format Specifiers later in this topic.

The following example includes the "H" custom format specifier in a custom format string.

Dim date1 As Date = #6:09:01 AM#
outputBlock.Text &= date1.ToString("H:mm:ss", _
                  CultureInfo.InvariantCulture) & vbCrLf
' Displays 6:09:01                        
DateTime date1 = new DateTime(2008, 1, 1, 6, 9, 1);
outputBlock.Text += date1.ToString("H:mm:ss",
                  CultureInfo.InvariantCulture) + "\n";
// Displays 6:09:01              

Back to table

The "HH" Custom Format Specifier

The "HH" custom format specifier (plus any number of additional "H" specifiers) represents the hour as a number from 00 through 23; that is, the hour is represented by a zero-based 24-hour clock that counts the hours since midnight. A single-digit hour is formatted with a leading zero.

The following example includes the "HH" custom format specifier in a custom format string.

Dim date1 As Date = #6:09:01 AM#
outputBlock.Text &= date1.ToString("HH:mm:ss", _
                  CultureInfo.InvariantCulture) & vbCrLf
' Displays 06:09:01                        
DateTime date1 = new DateTime(2008, 1, 1, 6, 9, 1);
outputBlock.Text += date1.ToString("HH:mm:ss",
                  CultureInfo.InvariantCulture) + "\n";
// Displays 06:09:01                        

Back to table

The "K" Custom Format Specifier

The "K" custom format specifier represents the time zone information of a date and time value. When this format specifier is used with DateTime values, the result string is defined by the value of the DateTime.Kind property:

For DateTimeOffset values, the "K" format specifier is equivalent to the "zz" format specifier, and produces a result string containing the DateTimeOffset value's offset from UTC.

If the "K" format specifier is used without other custom format specifiers, it is interpreted as a standard date and time format specifier and throws a FormatException. For more information about using a single format specifier, see Using Single Custom Format Specifiers later in this topic.

The following example displays the string that results from using the "K" custom format specifier with various DateTime and DateTimeOffset values on a system in the U.S. Pacific Time zone.

outputBlock.Text &= Date.Now.ToString("%K") & vbCrLf
' Displays -07:00
outputBlock.Text &= Date.UtcNow.ToString("%K") & vbCrLf
' Displays Z      
outputBlock.Text &= String.Format("'{0}'", _
                  Date.SpecifyKind(Date.Now, _
                                   DateTimeKind.Unspecified). _
                  ToString("%K")) & vbCrLf
' Displays ''      
outputBlock.Text &= DateTimeOffset.Now.ToString("%K") & vbCrLf
' Displays -07:00
outputBlock.Text &= DateTimeOffset.UtcNow.ToString("%K") & vbCrLf
' Displays +00:00
outputBlock.Text &= String.Format(New DateTimeOffset(2008, 5, 1, 6, 30, 0, _
                                     New TimeSpan(5, 0, 0)). _
                  ToString("%K")) & vbCrLf
' Displays +05:00                        
outputBlock.Text += DateTime.Now.ToString("%K") + "\n";
// Displays -07:00
outputBlock.Text += DateTime.UtcNow.ToString("%K") + "\n";
// Displays Z      
outputBlock.Text += String.Format("'{0}'",
                  DateTime.SpecifyKind(DateTime.Now,
                       DateTimeKind.Unspecified).ToString("%K")) + "\n";
// Displays ''      
outputBlock.Text += DateTimeOffset.Now.ToString("%K") + "\n";
// Displays -07:00
outputBlock.Text += DateTimeOffset.UtcNow.ToString("%K") + "\n";
// Displays +00:00
outputBlock.Text += String.Format(new DateTimeOffset(2008, 5, 1, 6, 30, 0,
                      new TimeSpan(5, 0, 0)).ToString("%K")) + "\n";
// Displays +05:00                        

Back to table

The "m" Custom Format Specifier

The "m" custom format specifier represents the minute as a number from 0 through 59. The minute represents whole minutes that have passed since the last hour. A single-digit minute is formatted without a leading zero.

If the "m" format specifier is used without other custom format specifiers, it is interpreted as the "m" standard date and time format specifier. For more information about using a single format specifier, see Using Single Custom Format Specifiers later in this topic.

The following example includes the "m" custom format specifier in a custom format string.

Dim date1 As Date
date1 = #6:09:01 PM#
outputBlock.Text &= date1.ToString("h:m:s.F t", _
                  CultureInfo.InvariantCulture) & vbCrLf
' Displays 6:9:1 P
outputBlock.Text &= date1.ToString("h:m:s.F t", New CultureInfo("el-GR")) & vbCrLf
' Displays 6:9:1 ?                        
date1 = New Date(2008, 1, 1, 18, 9, 1, 500)
outputBlock.Text &= date1.ToString("h:m:s.F t", _
                  CultureInfo.InvariantCulture) & vbCrLf
' Displays 6:9:1.5 P
outputBlock.Text &= date1.ToString("h:m:s.F t", New CultureInfo("el-GR")) & vbCrLf
' Displays 6:9:1.5 ?
DateTime date1;
date1 = new DateTime(2008, 1, 1, 18, 9, 1);
outputBlock.Text += date1.ToString("h:m:s.F t",
                                   CultureInfo.InvariantCulture)  + "\n";
// Displays 6:9:1 P
outputBlock.Text += date1.ToString("h:m:s.F t",
                  new CultureInfo("el-GR")) + "\n";
// Displays 6:9:1 �                        
date1 = new DateTime(2008, 1, 1, 18, 9, 1, 500);
outputBlock.Text += date1.ToString("h:m:s.F t",
                  CultureInfo.InvariantCulture) + "\n";
// Displays 6:9:1.5 P
outputBlock.Text += date1.ToString("h:m:s.F t",
                  new CultureInfo("el-GR")) + "\n";
// Displays 6:9:1.5 �

Back to table

The "mm" Custom Format Specifier

The "mm" custom format specifier (plus any number of additional "m" specifiers) represents the minute as a number from 00 through 59. The minute represents whole minutes that have passed since the last hour. A single-digit minute is formatted with a leading zero.

The following example includes the "mm" custom format specifier in a custom format string.

Dim date1 As Date
date1 = #6:09:01 PM#
outputBlock.Text &= date1.ToString("hh:mm:ss tt", _
                  CultureInfo.InvariantCulture) & vbCrLf
' Displays 06:09:01 PM                        
outputBlock.Text &= date1.ToString("hh:mm:ss tt", New CultureInfo("hu-HU")) & vbCrLf
' Displays 06:09:01 du.
date1 = New Date(2008, 1, 1, 18, 9, 1, 500)
outputBlock.Text &= date1.ToString("hh:mm:ss.ff tt", _
                  CultureInfo.InvariantCulture) & vbCrLf
' Displays 06:09:01.50 PM                        
outputBlock.Text &= date1.ToString("hh:mm:ss.ff tt", New CultureInfo("hu-HU")) & vbCrLf
' Displays 06:09:01.50 du.
DateTime date1;
date1 = new DateTime(2008, 1, 1, 18, 9, 1);
outputBlock.Text += date1.ToString("hh:mm:ss tt",
                  CultureInfo.InvariantCulture) + "\n";
// Displays 06:09:01 PM                        
outputBlock.Text += date1.ToString("hh:mm:ss tt",
                  new CultureInfo("hu-HU")) + "\n";
// Displays 06:09:01 du.
date1 = new DateTime(2008, 1, 1, 18, 9, 1, 500);
outputBlock.Text += date1.ToString("hh:mm:ss.ff tt",
                  CultureInfo.InvariantCulture) + "\n";
// Displays 06:09:01.50 PM                        
outputBlock.Text += date1.ToString("hh:mm:ss.ff tt",
                  new CultureInfo("hu-HU")) + "\n";
// Displays 06:09:01.50 du.

Back to table

The "M" Custom Format Specifier

The "M" custom format specifier represents the month as a number from 1 through 12. A single-digit month is formatted without a leading zero.

If the "M" format specifier is used without other custom format specifiers, it is interpreted as the "M" standard date and time format specifier. For more information about using a single format specifier, see Using Single Custom Format Specifiers later in this topic.

The following example includes the "M" custom format specifier in a custom format string.

Dim date1 As Date = #8/18/2008#
outputBlock.Text &= date1.ToString("(M & vbCrLf MMM, MMMM", _
                    New CultureInfo("en-US")) & vbCrLf
' Displays (8) Aug, August
outputBlock.Text &= date1.ToString("(M & vbCrLf MMM, MMMM", _
                    New CultureInfo("nl-NL")) & vbCrLf
' Displays (8) aug, augustus
outputBlock.Text &= date1.ToString("(M & vbCrLf MMM, MMMM", _
                    New CultureInfo("lv-LV")) & vbCrLF
' Displays (8) Aug, augusts                       
DateTime date1 = new DateTime(2008, 8, 18);
outputBlock.Text += date1.ToString("(M) MMM, MMMM",
                  new CultureInfo("en-US")) + "\n";
// Displays (8) Aug, August
outputBlock.Text += date1.ToString("(M) MMM, MMMM",
                  new CultureInfo("nl-NL")) + "\n";
// Displays (8) aug, augustus
outputBlock.Text += date1.ToString("(M) MMM, MMMM",
                  new CultureInfo("lv-LV")) + "\n";
// Displays (8) Aug, augusts                       

Back to table

The "MM" Custom Format Specifier

The "MM" custom format specifier represents the month as a number from 01 through 12. A single-digit month is formatted with a leading zero.

The following example includes the "MM" custom format specifier in a custom format string.

Dim date1 As Date = #1/2/2008 6:30:15 AM#

outputBlock.Text &= date1.ToString("dd, MM", _
                  CultureInfo.InvariantCulture) & vbCrLf
' 02, 01
DateTime date1 = new DateTime(2008, 1, 2, 6, 30, 15);

outputBlock.Text += date1.ToString("dd, MM",
                  CultureInfo.InvariantCulture) + "\n";
// 02, 01

Back to table

The "MMM" Custom Format Specifier

The "MMM" custom format specifier represents the abbreviated name of the month. The localized abbreviated name of the month is retrieved from the DateTimeFormatInfo.AbbreviatedMonthNames property of the current or specified culture.

The following example includes the "MMM" custom format specifier in a custom format string.

Dim date1 As Date = #8/29/2008 7:27:15 PM#

outputBlock.Text &= date1.ToString("ddd d MMM", New CultureInfo("en-US")) & vbCrLf
' Displays Fri 29 Aug
outputBlock.Text &= date1.ToString("ddd d MMM", New CultureInfo("fr-FR")) & vbCrLf
' Displays ven. 29 ao?t                                                
DateTime date1 = new DateTime(2008, 8, 29, 19, 27, 15);

outputBlock.Text += date1.ToString("ddd d MMM",
                                   new CultureInfo("en-US")) + "\n";
// Displays Fri 29 Aug
outputBlock.Text += date1.ToString("ddd d MMM", 
                                   new CultureInfo("fr-FR")) + "\n";
// Displays ven. 29 ao�t                                                

Back to table

The "MMMM" Custom Format Specifier

The "MMMM" custom format specifier represents the full name of the month. The localized name of the month is retrieved from the DateTimeFormatInfo.MonthNames property of the current or specified culture.

The following example includes the "MMMM" custom format specifier in a custom format string.

Dim date1 As Date = #8/29/2008 7:27:15 PM#

outputBlock.Text &= date1.ToString("dddd dd MMMM", New CultureInfo("en-US")) & vbCrLf
' Displays Friday 29 August
outputBlock.Text &= date1.ToString("dddd dd MMMM", New CultureInfo("it-IT")) & vbCrLf
' Displays venerd? 29 agosto                                          
DateTime date1 = new DateTime(2008, 8, 29, 19, 27, 15);

outputBlock.Text += date1.ToString("dddd dd MMMM",
                                   new CultureInfo("en-US"))  + "\n";
// Displays Friday 29 August
outputBlock.Text += date1.ToString("dddd dd MMMM",
                                   new CultureInfo("it-IT"))  + "\n";
// Displays venerd� 29 agosto                                

Back to table

The "s" Custom Format Specifier

The "s" custom format specifier represents the seconds as a number from 0 through 59. The result represents whole seconds that have passed since the last minute. A single-digit second is formatted without a leading zero.

If the "s" format specifier is used without other custom format specifiers, it is interpreted as the "s" standard date and time format specifier. For more information about using a single format specifier, see Using Single Custom Format Specifiers later in this topic.

The following example includes the "s" custom format specifier in a custom format string.

Dim date1 As Date
date1 = #6:09:01 PM#
outputBlock.Text &= date1.ToString("h:m:s.F t", _
                  CultureInfo.InvariantCulture) & vbCrLf
' Displays 6:9:1 P
outputBlock.Text &= date1.ToString("h:m:s.F t", New CultureInfo("el-GR")) & vbCrLf
' Displays 6:9:1 ?                        
date1 = New Date(2008, 1, 1, 18, 9, 1, 500)
outputBlock.Text &= date1.ToString("h:m:s.F t", _
                  CultureInfo.InvariantCulture) & vbCrLf
' Displays 6:9:1.5 P
outputBlock.Text &= date1.ToString("h:m:s.F t", New CultureInfo("el-GR")) & vbCrLf
' Displays 6:9:1.5 ?
DateTime date1;
date1 = new DateTime(2008, 1, 1, 18, 9, 1);
outputBlock.Text += date1.ToString("h:m:s.F t",
                                   CultureInfo.InvariantCulture)  + "\n";
// Displays 6:9:1 P
outputBlock.Text += date1.ToString("h:m:s.F t",
                  new CultureInfo("el-GR")) + "\n";
// Displays 6:9:1 �                        
date1 = new DateTime(2008, 1, 1, 18, 9, 1, 500);
outputBlock.Text += date1.ToString("h:m:s.F t",
                  CultureInfo.InvariantCulture) + "\n";
// Displays 6:9:1.5 P
outputBlock.Text += date1.ToString("h:m:s.F t",
                  new CultureInfo("el-GR")) + "\n";
// Displays 6:9:1.5 �

Back to table

The "ss" Custom Format Specifier

The "ss" custom format specifier (plus any number of additional "s" specifiers) represents the seconds as a number from 00 through 59. The result represents whole seconds that have passed since the last minute. A single-digit second is formatted with a leading zero.

The following example includes the "ss" custom format specifier in a custom format string.

Dim date1 As Date
date1 = #6:09:01 PM#
outputBlock.Text &= date1.ToString("hh:mm:ss tt", _
                  CultureInfo.InvariantCulture) & vbCrLf
' Displays 06:09:01 PM                        
outputBlock.Text &= date1.ToString("hh:mm:ss tt", New CultureInfo("hu-HU")) & vbCrLf
' Displays 06:09:01 du.
date1 = New Date(2008, 1, 1, 18, 9, 1, 500)
outputBlock.Text &= date1.ToString("hh:mm:ss.ff tt", _
                  CultureInfo.InvariantCulture) & vbCrLf
' Displays 06:09:01.50 PM                        
outputBlock.Text &= date1.ToString("hh:mm:ss.ff tt", New CultureInfo("hu-HU")) & vbCrLf
' Displays 06:09:01.50 du.
DateTime date1;
date1 = new DateTime(2008, 1, 1, 18, 9, 1);
outputBlock.Text += date1.ToString("hh:mm:ss tt",
                  CultureInfo.InvariantCulture) + "\n";
// Displays 06:09:01 PM                        
outputBlock.Text += date1.ToString("hh:mm:ss tt",
                  new CultureInfo("hu-HU")) + "\n";
// Displays 06:09:01 du.
date1 = new DateTime(2008, 1, 1, 18, 9, 1, 500);
outputBlock.Text += date1.ToString("hh:mm:ss.ff tt",
                  CultureInfo.InvariantCulture) + "\n";
// Displays 06:09:01.50 PM                        
outputBlock.Text += date1.ToString("hh:mm:ss.ff tt",
                  new CultureInfo("hu-HU")) + "\n";
// Displays 06:09:01.50 du.

Back to table

The "t" Custom Format Specifier

The "t" custom format specifier represents the first character of the AM/PM designator. The appropriate localized designator is retrieved from the DateTimeFormatInfo.AMDesignator or DateTimeFormatInfo.PMDesignator property of the current or specific culture. The AM designator is used for all times from 0:00:00 (midnight) to 11:59:59.999. The PM designator is used for all times from 12:00:00 (noon) to 23:59:59.99.

If the "t" format specifier is used without other custom format specifiers, it is interpreted as the "t" standard date and time format specifier. For more information about using a single format specifier, see Using Single Custom Format Specifiers later in this topic.

The following example includes the "t" custom format specifier in a custom format string.

Dim date1 As Date
date1 = #6:09:01 PM#
outputBlock.Text &= date1.ToString("h:m:s.F t", _
                  CultureInfo.InvariantCulture) & vbCrLf
' Displays 6:9:1 P
outputBlock.Text &= date1.ToString("h:m:s.F t", New CultureInfo("el-GR")) & vbCrLf
' Displays 6:9:1 ?                        
date1 = New Date(2008, 1, 1, 18, 9, 1, 500)
outputBlock.Text &= date1.ToString("h:m:s.F t", _
                  CultureInfo.InvariantCulture) & vbCrLf
' Displays 6:9:1.5 P
outputBlock.Text &= date1.ToString("h:m:s.F t", New CultureInfo("el-GR")) & vbCrLf
' Displays 6:9:1.5 ?
DateTime date1;
date1 = new DateTime(2008, 1, 1, 18, 9, 1);
outputBlock.Text += date1.ToString("h:m:s.F t",
                                   CultureInfo.InvariantCulture)  + "\n";
// Displays 6:9:1 P
outputBlock.Text += date1.ToString("h:m:s.F t",
                  new CultureInfo("el-GR")) + "\n";
// Displays 6:9:1 �                        
date1 = new DateTime(2008, 1, 1, 18, 9, 1, 500);
outputBlock.Text += date1.ToString("h:m:s.F t",
                  CultureInfo.InvariantCulture) + "\n";
// Displays 6:9:1.5 P
outputBlock.Text += date1.ToString("h:m:s.F t",
                  new CultureInfo("el-GR")) + "\n";
// Displays 6:9:1.5 �

Back to table

The "tt" Custom Format Specifier

The "tt" custom format specifier (plus any number of additional "t" specifiers) represents the entire AM/PM designator. The appropriate localized designator is retrieved from the DateTimeFormatInfo.AMDesignator or DateTimeFormatInfo.PMDesignator property of the current or specific culture. The AM designator is used for all times from 0:00:00 (midnight) to 11:59:59.999. The PM designator is used for all times from 12:00:00 (noon) to 23:59:59.99.

Make sure to use the "tt" specifier for languages for which it is necessary to maintain the distinction between AM and PM. An example is Japanese, for which the AM and PM designators differ in the second character instead of the first character.

The following example includes the "tt" custom format specifier in a custom format string.

Dim date1 As Date
date1 = #6:09:01 PM#
outputBlock.Text &= date1.ToString("hh:mm:ss tt", _
                  CultureInfo.InvariantCulture) & vbCrLf
' Displays 06:09:01 PM                        
outputBlock.Text &= date1.ToString("hh:mm:ss tt", New CultureInfo("hu-HU")) & vbCrLf
' Displays 06:09:01 du.
date1 = New Date(2008, 1, 1, 18, 9, 1, 500)
outputBlock.Text &= date1.ToString("hh:mm:ss.ff tt", _
                  CultureInfo.InvariantCulture) & vbCrLf
' Displays 06:09:01.50 PM                        
outputBlock.Text &= date1.ToString("hh:mm:ss.ff tt", New CultureInfo("hu-HU")) & vbCrLf
' Displays 06:09:01.50 du.
DateTime date1;
date1 = new DateTime(2008, 1, 1, 18, 9, 1);
outputBlock.Text += date1.ToString("hh:mm:ss tt",
                  CultureInfo.InvariantCulture) + "\n";
// Displays 06:09:01 PM                        
outputBlock.Text += date1.ToString("hh:mm:ss tt",
                  new CultureInfo("hu-HU")) + "\n";
// Displays 06:09:01 du.
date1 = new DateTime(2008, 1, 1, 18, 9, 1, 500);
outputBlock.Text += date1.ToString("hh:mm:ss.ff tt",
                  CultureInfo.InvariantCulture) + "\n";
// Displays 06:09:01.50 PM                        
outputBlock.Text += date1.ToString("hh:mm:ss.ff tt",
                  new CultureInfo("hu-HU")) + "\n";
// Displays 06:09:01.50 du.

Back to table

The "y" Custom Format Specifier

The "y" custom format specifier represents the year as a one-digit or two-digit number. If the year has more than two digits, only the two low-order digits appear in the result. If the first digit of a two-digit year begins with a zero (for example, 2008), the number is formatted without a leading zero.

If the "y" format specifier is used without other custom format specifiers, it is interpreted as the "y" standard date and time format specifier. For more information about using a single format specifier, see Using Single Custom Format Specifiers later in this topic.

The following example includes the "y" custom format specifier in a custom format string.

Dim date1 As Date = #12/1/0001#
Dim date2 As Date = #1/1/2010#
outputBlock.Text &= date1.ToString("%y") & vbCrLf
' Displays 1
outputBlock.Text &= date1.ToString("yy") & vbCrLf
' Displays 01
outputBlock.Text &= date1.ToString("yyy") & vbCrLf
' Displays 001
outputBlock.Text &= date1.ToString("yyyy") & vbCrLf
' Displays 0001
outputBlock.Text &= date1.ToString("yyyyy") & vbCrLf
' Displays 00001
outputBlock.Text &= date2.ToString("%y") & vbCrLf
' Displays 10
outputBlock.Text &= date2.ToString("yy") & vbCrLf
' Displays 10
outputBlock.Text &= date2.ToString("yyy") & vbCrLf
' Displays 2010      
outputBlock.Text &= date2.ToString("yyyy") & vbCrLf
' Displays 2010      
outputBlock.Text &= date2.ToString("yyyyy") & vbCrLf
' Displays 02010      
DateTime date1 = new DateTime(1, 12, 1);
DateTime date2 = new DateTime(2010, 1, 1);
outputBlock.Text += date1.ToString("%y") + "\n";
// Displays 1
outputBlock.Text += date1.ToString("yy") + "\n";
// Displays 01
outputBlock.Text += date1.ToString("yyy") + "\n";
// Displays 001
outputBlock.Text += date1.ToString("yyyy") + "\n";
// Displays 0001
outputBlock.Text += date1.ToString("yyyyy") + "\n";
// Displays 00001
outputBlock.Text += date2.ToString("%y") + "\n";
// Displays 10
outputBlock.Text += date2.ToString("yy") + "\n";
// Displays 10
outputBlock.Text += date2.ToString("yyy") + "\n";
// Displays 2010      
outputBlock.Text += date2.ToString("yyyy") + "\n";
// Displays 2010      
outputBlock.Text += date2.ToString("yyyyy") + "\n";
// Displays 02010      

Back to table

The "yy" Custom Format Specifier

The "yy" custom format specifier represents the year as a two-digit number. If the year has more than two digits, only the two low-order digits appear in the result. If the two-digit year has fewer than two significant digits, the number is padded with leading zeros to produce two digits.

In a parsing operation, a two-digit year that is parsed using the "yy" custom format specifier is interpreted based on the Calendar.TwoDigitYearMax property of the format provider's current calendar. The following example parses the string representation of a date that has a two-digit year by using the default Gregorian calendar of the en-US culture, which, in this case, is the current culture. It then changes the current culture's CultureInfo object to use a GregorianCalendar object whose TwoDigitYearMax property has been modified.

Imports System.Globalization
Imports System.Threading

Module Example
   Public Sub Demo(outputBlock As System.Windows.Controls.TextBlock)
      Dim fmt As String = "dd-MMM-yy"
      Dim value As String = "24-Jan-49"

      Dim cal As Calendar = CultureInfo.CurrentCulture.Calendar
      outputBlock.Text += String.Format("Two Digit Year Range: {0} - {1}", 
                                        cal.TwoDigitYearMax - 99, cal.TwoDigitYearMax) + vbCrLf

      outputBlock.Text += String.Format("{0:d}", DateTime.ParseExact(value, fmt, Nothing)) + vbCrLf
      outputBlock.Text += vbCrLf

      cal = new GregorianCalendar()
      cal.TwoDigitYearMax = 2099
      Dim culture As CultureInfo = CType(CultureInfo.CurrentCulture.Clone(), CultureInfo)
      culture.DateTimeFormat.Calendar = cal
      Thread.CurrentThread.CurrentCulture = culture

      outputBlock.Text += String.Format("Two Digit Year Range: {0} - {1}", 
                                        cal.TwoDigitYearMax - 99, cal.TwoDigitYearMax) + vbCrLf
      outputBlock.Text += String.Format("{0:d}", DateTime.ParseExact(value, fmt, Nothing)) + vbCrLf
   End Sub
End Module
' The example displays the following output:
'       Two Digit Year Range: 1930 - 2029
'       1/24/1949
'       
'       Two Digit Year Range: 2000 - 2099
'       1/24/2049
using System;
using System.Globalization;
using System.Threading;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      string fmt = "dd-MMM-yy";
      string value = "24-Jan-49";

      GregorianCalendar cal = (GregorianCalendar) CultureInfo.CurrentCulture.Calendar;
      outputBlock.Text += String.Format("Two Digit Year Range: {0} - {1}\n", 
                        cal.TwoDigitYearMax - 99, cal.TwoDigitYearMax);

      outputBlock.Text += String.Format("{0:d}\n\n", 
                                        DateTime.ParseExact(value, fmt, null));

      cal = (GregorianCalendar) new GregorianCalendar(cal.CalendarType);
      cal.TwoDigitYearMax = 2099;
      CultureInfo culture = (CultureInfo) CultureInfo.CurrentCulture.Clone();
      culture.DateTimeFormat.Calendar = cal;
      Thread.CurrentThread.CurrentCulture = culture;

      outputBlock.Text += String.Format("Two Digit Year Range: {0} - {1}\n", 
                        cal.TwoDigitYearMax - 99, cal.TwoDigitYearMax);
      outputBlock.Text += String.Format("{0:d}\n", DateTime.ParseExact(value, fmt, null));
   }
}
// The example displays the following output:
//       Two Digit Year Range: 1930 - 2029
//       1/24/1949
//       
//       Two Digit Year Range: 2000 - 2099
//       1/24/2049

The following example includes the "yy" custom format specifier in a custom format string.

Dim date1 As Date = #12/1/0001#
Dim date2 As Date = #1/1/2010#
outputBlock.Text &= date1.ToString("%y") & vbCrLf
' Displays 1
outputBlock.Text &= date1.ToString("yy") & vbCrLf
' Displays 01
outputBlock.Text &= date1.ToString("yyy") & vbCrLf
' Displays 001
outputBlock.Text &= date1.ToString("yyyy") & vbCrLf
' Displays 0001
outputBlock.Text &= date1.ToString("yyyyy") & vbCrLf
' Displays 00001
outputBlock.Text &= date2.ToString("%y") & vbCrLf
' Displays 10
outputBlock.Text &= date2.ToString("yy") & vbCrLf
' Displays 10
outputBlock.Text &= date2.ToString("yyy") & vbCrLf
' Displays 2010      
outputBlock.Text &= date2.ToString("yyyy") & vbCrLf
' Displays 2010      
outputBlock.Text &= date2.ToString("yyyyy") & vbCrLf
' Displays 02010      
DateTime date1 = new DateTime(1, 12, 1);
DateTime date2 = new DateTime(2010, 1, 1);
outputBlock.Text += date1.ToString("%y") + "\n";
// Displays 1
outputBlock.Text += date1.ToString("yy") + "\n";
// Displays 01
outputBlock.Text += date1.ToString("yyy") + "\n";
// Displays 001
outputBlock.Text += date1.ToString("yyyy") + "\n";
// Displays 0001
outputBlock.Text += date1.ToString("yyyyy") + "\n";
// Displays 00001
outputBlock.Text += date2.ToString("%y") + "\n";
// Displays 10
outputBlock.Text += date2.ToString("yy") + "\n";
// Displays 10
outputBlock.Text += date2.ToString("yyy") + "\n";
// Displays 2010      
outputBlock.Text += date2.ToString("yyyy") + "\n";
// Displays 2010      
outputBlock.Text += date2.ToString("yyyyy") + "\n";
// Displays 02010      

Back to table

The "yyy" Custom Format Specifier

The "yyy" custom format specifier represents the year with a minimum of three digits. If the year has more than three significant digits, they are included in the result string. If the year has fewer than three digits, the number is padded with leading zeros to produce three digits.

NoteNote:

For the Thai Buddhist calendar, which can have five-digit years, this format specifier displays all five digits.

The following example includes the "yyy" custom format specifier in a custom format string.

Dim date1 As Date = #12/1/0001#
Dim date2 As Date = #1/1/2010#
outputBlock.Text &= date1.ToString("%y") & vbCrLf
' Displays 1
outputBlock.Text &= date1.ToString("yy") & vbCrLf
' Displays 01
outputBlock.Text &= date1.ToString("yyy") & vbCrLf
' Displays 001
outputBlock.Text &= date1.ToString("yyyy") & vbCrLf
' Displays 0001
outputBlock.Text &= date1.ToString("yyyyy") & vbCrLf
' Displays 00001
outputBlock.Text &= date2.ToString("%y") & vbCrLf
' Displays 10
outputBlock.Text &= date2.ToString("yy") & vbCrLf
' Displays 10
outputBlock.Text &= date2.ToString("yyy") & vbCrLf
' Displays 2010      
outputBlock.Text &= date2.ToString("yyyy") & vbCrLf
' Displays 2010      
outputBlock.Text &= date2.ToString("yyyyy") & vbCrLf
' Displays 02010      
DateTime date1 = new DateTime(1, 12, 1);
DateTime date2 = new DateTime(2010, 1, 1);
outputBlock.Text += date1.ToString("%y") + "\n";
// Displays 1
outputBlock.Text += date1.ToString("yy") + "\n";
// Displays 01
outputBlock.Text += date1.ToString("yyy") + "\n";
// Displays 001
outputBlock.Text += date1.ToString("yyyy") + "\n";
// Displays 0001
outputBlock.Text += date1.ToString("yyyyy") + "\n";
// Displays 00001
outputBlock.Text += date2.ToString("%y") + "\n";
// Displays 10
outputBlock.Text += date2.ToString("yy") + "\n";
// Displays 10
outputBlock.Text += date2.ToString("yyy") + "\n";
// Displays 2010      
outputBlock.Text += date2.ToString("yyyy") + "\n";
// Displays 2010      
outputBlock.Text += date2.ToString("yyyyy") + "\n";
// Displays 02010      

Back to table

The "yyyy" Custom Format Specifier

The "yyyy" custom format specifier represents the year as a four-digit number. If the year has more than four digits, only the four low-order digits appear in the result. If the year has fewer than four digits, the number is padded with leading zeros to achieve four digits.

NoteNote:

For the Thai Buddhist calendar, which can have five-digit years, this format specifier displays all five digits.

The following example includes the "yyyy" custom format specifier in a custom format string.

Dim date1 As Date = #12/1/0001#
Dim date2 As Date = #1/1/2010#
outputBlock.Text &= date1.ToString("%y") & vbCrLf
' Displays 1
outputBlock.Text &= date1.ToString("yy") & vbCrLf
' Displays 01
outputBlock.Text &= date1.ToString("yyy") & vbCrLf
' Displays 001
outputBlock.Text &= date1.ToString("yyyy") & vbCrLf
' Displays 0001
outputBlock.Text &= date1.ToString("yyyyy") & vbCrLf
' Displays 00001
outputBlock.Text &= date2.ToString("%y") & vbCrLf
' Displays 10
outputBlock.Text &= date2.ToString("yy") & vbCrLf
' Displays 10
outputBlock.Text &= date2.ToString("yyy") & vbCrLf
' Displays 2010      
outputBlock.Text &= date2.ToString("yyyy") & vbCrLf
' Displays 2010      
outputBlock.Text &= date2.ToString("yyyyy") & vbCrLf
' Displays 02010      
DateTime date1 = new DateTime(1, 12, 1);
DateTime date2 = new DateTime(2010, 1, 1);
outputBlock.Text += date1.ToString("%y") + "\n";
// Displays 1
outputBlock.Text += date1.ToString("yy") + "\n";
// Displays 01
outputBlock.Text += date1.ToString("yyy") + "\n";
// Displays 001
outputBlock.Text += date1.ToString("yyyy") + "\n";
// Displays 0001
outputBlock.Text += date1.ToString("yyyyy") + "\n";
// Displays 00001
outputBlock.Text += date2.ToString("%y") + "\n";
// Displays 10
outputBlock.Text += date2.ToString("yy") + "\n";
// Displays 10
outputBlock.Text += date2.ToString("yyy") + "\n";
// Displays 2010      
outputBlock.Text += date2.ToString("yyyy") + "\n";
// Displays 2010      
outputBlock.Text += date2.ToString("yyyyy") + "\n";
// Displays 02010      

Back to table

The "yyyyy" Custom Format Specifier

The "yyyyy" custom format specifier (plus any number of additional "y" specifiers) represents the year as a five-digit number. If the year has more than five digits, only the five low-order digits appear in the result. If the year has fewer than five digits, the number is padded with leading zeros to produce five digits.

If there are additional "y" specifiers, the number is padded with as many leading zeros as necessary to produce the number of "y" specifiers.

The following example includes the "yyyyy" custom format specifier in a custom format string.

Dim date1 As Date = #12/1/0001#
Dim date2 As Date = #1/1/2010#
outputBlock.Text &= date1.ToString("%y") & vbCrLf
' Displays 1
outputBlock.Text &= date1.ToString("yy") & vbCrLf
' Displays 01
outputBlock.Text &= date1.ToString("yyy") & vbCrLf
' Displays 001
outputBlock.Text &= date1.ToString("yyyy") & vbCrLf
' Displays 0001
outputBlock.Text &= date1.ToString("yyyyy") & vbCrLf
' Displays 00001
outputBlock.Text &= date2.ToString("%y") & vbCrLf
' Displays 10
outputBlock.Text &= date2.ToString("yy") & vbCrLf
' Displays 10
outputBlock.Text &= date2.ToString("yyy") & vbCrLf
' Displays 2010      
outputBlock.Text &= date2.ToString("yyyy") & vbCrLf
' Displays 2010      
outputBlock.Text &= date2.ToString("yyyyy") & vbCrLf
' Displays 02010      
DateTime date1 = new DateTime(1, 12, 1);
DateTime date2 = new DateTime(2010, 1, 1);
outputBlock.Text += date1.ToString("%y") + "\n";
// Displays 1
outputBlock.Text += date1.ToString("yy") + "\n";
// Displays 01
outputBlock.Text += date1.ToString("yyy") + "\n";
// Displays 001
outputBlock.Text += date1.ToString("yyyy") + "\n";
// Displays 0001
outputBlock.Text += date1.ToString("yyyyy") + "\n";
// Displays 00001
outputBlock.Text += date2.ToString("%y") + "\n";
// Displays 10
outputBlock.Text += date2.ToString("yy") + "\n";
// Displays 10
outputBlock.Text += date2.ToString("yyy") + "\n";
// Displays 2010      
outputBlock.Text += date2.ToString("yyyy") + "\n";
// Displays 2010      
outputBlock.Text += date2.ToString("yyyyy") + "\n";
// Displays 02010      

Back to table

The "z" Custom Format Specifier

With DateTime values, the "z" custom format specifier represents the signed offset of the local operating system's time zone from Coordinated Universal Time (UTC), measured in hours. It does not reflect the value of an instance's DateTime.Kind property. For this reason, the "z" format specifier is not recommended for use with DateTime values.

With DateTimeOffset values, this format specifier represents the DateTimeOffset value's offset from UTC in hours.

The offset is always displayed with a leading sign. A plus sign (+) indicates hours ahead of UTC, and a minus sign (-) indicates hours behind UTC. The offset ranges from –12 through +13. A single-digit offset is formatted without a leading zero.

If the "z" format specifier is used without other custom format specifiers, it is interpreted as a standard date and time format specifier and throws a FormatException. For more information about using a single format specifier, see Using Single Custom Format Specifiers later in this topic.

The following example includes the "z" custom format specifier in a custom format string.

Dim date1 As Date = Date.UtcNow
outputBlock.Text &= String.Format(String.Format("{0:%z}, {0:zz}, {0:zzz}", _
                  date1)) & vbCrLf
' Displays -7, -07, -07:00                     

Dim date2 As New DateTimeOffset(2008, 8, 1, 0, 0, 0, _
                                New TimeSpan(6, 0, 0))
outputBlock.Text &= String.Format(String.Format("{0:%z}, {0:zz}, {0:zzz}", _
                  date2)) & vbCrLf
' Displays +6, +06, +06:00
DateTime date1 = DateTime.UtcNow;
outputBlock.Text += String.Format(String.Format("{0:%z}, {0:zz}, {0:zzz}",
                  date1)) + "\n";
// Displays -7, -07, -07:00                     

DateTimeOffset date2 = new DateTimeOffset(2008, 8, 1, 0, 0, 0,
                                          new TimeSpan(6, 0, 0));
outputBlock.Text += String.Format(String.Format("{0:%z}, {0:zz}, {0:zzz}",
                  date2)) + "\n";
// Displays +6, +06, +06:00

Back to table

The "zz" Custom Format Specifier

With DateTime values, the "zz" custom format specifier represents the signed offset of the local operating system's time zone from UTC, measured in hours. It does not reflect the value of an instance's DateTime.Kind property. For this reason, the "zz" format specifier is not recommended for use with DateTime values.

With DateTimeOffset values, this format specifier represents the DateTimeOffset value's offset from UTC in hours.

The offset is always displayed with a leading sign. A plus sign (+) indicates hours ahead of UTC, and a minus sign (-) indicates hours behind UTC. The offset ranges from –12 through +13. A single-digit offset is formatted with a leading zero.

The following example includes the "zz" custom format specifier in a custom format string.

Dim date1 As Date = Date.UtcNow
outputBlock.Text &= String.Format(String.Format("{0:%z}, {0:zz}, {0:zzz}", _
                  date1)) & vbCrLf
' Displays -7, -07, -07:00                     

Dim date2 As New DateTimeOffset(2008, 8, 1, 0, 0, 0, _
                                New TimeSpan(6, 0, 0))
outputBlock.Text &= String.Format(String.Format("{0:%z}, {0:zz}, {0:zzz}", _
                  date2)) & vbCrLf
' Displays +6, +06, +06:00
DateTime date1 = DateTime.UtcNow;
outputBlock.Text += String.Format(String.Format("{0:%z}, {0:zz}, {0:zzz}",
                  date1)) + "\n";
// Displays -7, -07, -07:00                     

DateTimeOffset date2 = new DateTimeOffset(2008, 8, 1, 0, 0, 0,
                                          new TimeSpan(6, 0, 0));
outputBlock.Text += String.Format(String.Format("{0:%z}, {0:zz}, {0:zzz}",
                  date2)) + "\n";
// Displays +6, +06, +06:00

Back to table

The "zzz" Custom Format Specifier

With DateTime values, the "zzz" custom format specifier represents the signed offset of the local operating system's time zone from UTC, measured in hours and minutes. It does not reflect the value of an instance's DateTime.Kind property. For this reason, the "zzz" format specifier is not recommended for use with DateTime values.

With DateTimeOffset values, this format specifier represents the DateTimeOffset value's offset from UTC in hours and minutes.

The offset is always displayed with a leading sign. A plus sign (+) indicates hours ahead of UTC, and a minus sign (-) indicates hours behind UTC. The offset ranges from –12 through +13. A single-digit offset is formatted with a leading zero.

The following example includes the "zzz" custom format specifier in a custom format string.

Dim date1 As Date = Date.UtcNow
outputBlock.Text &= String.Format(String.Format("{0:%z}, {0:zz}, {0:zzz}", _
                  date1)) & vbCrLf
' Displays -7, -07, -07:00                     

Dim date2 As New DateTimeOffset(2008, 8, 1, 0, 0, 0, _
                                New TimeSpan(6, 0, 0))
outputBlock.Text &= String.Format(String.Format("{0:%z}, {0:zz}, {0:zzz}", _
                  date2)) & vbCrLf
' Displays +6, +06, +06:00
DateTime date1 = DateTime.UtcNow;
outputBlock.Text += String.Format(String.Format("{0:%z}, {0:zz}, {0:zzz}",
                  date1)) + "\n";
// Displays -7, -07, -07:00                     

DateTimeOffset date2 = new DateTimeOffset(2008, 8, 1, 0, 0, 0,
                                          new TimeSpan(6, 0, 0));
outputBlock.Text += String.Format(String.Format("{0:%z}, {0:zz}, {0:zzz}",
                  date2)) + "\n";
// Displays +6, +06, +06:00

Back to table

The ":" Custom Format Specifier

The ":" custom format specifier represents the localized time separator, which is used to differentiate hours, minutes, and seconds.

If the ":" format specifier is used without other custom format specifiers, it is interpreted as a standard date and time format specifier and throws a FormatException. For more information about using a single format specifier, see Using Single Custom Format Specifiers later in this topic.

Back to table

The "/" Custom Format Specifier

The "/" custom format specifier represents the localized date separator, which is used to differentiate years, months, and days..

If the "/" format specifier is used without other custom format specifiers, it is interpreted as a standard date and time format specifier and throws a FormatException. For more information about using a single format specifier, see Using Single Custom Format Specifiers later in this topic.

Back to table

Notes

Using Single Custom Format Specifiers

A custom date and time format string consists of two or more characters. Date and time formatting methods interpret any single-character string as a standard date and time format string. If they do not recognize the character as a valid format specifier, they throw a FormatException. For example, a format string that consists only of the specifier "h" is interpreted as a standard date and time format string. However, in this particular case, an exception is thrown because there is no "h" standard date and timeformat specifier.

To use any of the custom date and time format specifiers as the only specifier in a format string (that is, to use the "d", "f", "F", "g", "h", "H", "K", "m", "M", "s", "t", "y", "z", ":", or "/" custom format specifier by itself), include a space before or after the specifier, or include a percent ("%") format specifier before the single custom date and time specifier.

For example, "%h" is interpreted as a custom date and time format string that displays the hour represented by the current date and time value. You can also use the " h" or "h " format string, although this includes a space in the result string along with the hour. The following example illustrates these three format strings.

Dim dat1 As Date = #6/15/2009 1:45:00 PM#

outputBlock.Text += String.Format("'{0:%h}'", dat1) & vbCrLf
outputBlock.Text += String.Format("'{0: h}'", dat1) & vbCrLf
outputBlock.Text += String.Format("'{0:h }'", dat1) & vbCrLf
' The example displays the following output:
'       '1'
'       ' 1'
'       '1 '
DateTime dat1 = new DateTime(2009, 6, 15, 13, 45, 0);

outputBlock.Text += String.Format("'{0:%h}'", dat1) + "\n";
outputBlock.Text += String.Format("'{0: h}'", dat1) + "\n";
outputBlock.Text += String.Format("'{0:h }'", dat1) + "\n";
// The example displays the following output:
//       '1'
//       ' 1'
//       '1 '

Using the Escape Character

The "d", "f", "F", "g", "h", "H", "K", "m", "M", "s", "t", "y", "z", ":", or "/" characters in a format string are interpreted as custom format specifiers rather than as literal characters. To prevent a character from being interpreted as a format specifier, you can precede it with a backslash (\), which is the escape character. The escape character signifies that the following character is a character literal that should be included in the result string unchanged.

To include a backslash in a result string, you must escape it with another backslash (\\).

NoteNote:

Some compilers, such as the C++ and C# compilers, may also interpret a single backslash character as an escape character. To ensure that a string is interpreted correctly when formatting, you can use the verbatim string literal character (the @ character) before the string in C#, or add another backslash character before each backslash in C# and C++. The following C# example illustrates both approaches.

The following example uses the escape character to prevent the formatting operation from interpreting the "h" and "m" characters as format specifiers.

Dim date1 As Date = #6/15/2009 1:45:00 PM#
Dim fmt As String = "h \h m \m"

outputBlock.Text += String.Format("{0} ({1}) -> {2}", date1, fmt, date1.ToString(fmt)) & vbCrLf
' The example displays the following output:
'       6/15/2009 1:45:00 PM (h \h m \m) -> 1 h 45 m      
DateTime date = new DateTime(2009, 06, 15, 13, 45, 30, 90);
string fmt1 = "h \\h m \\m";
string fmt2 = @"h \h m \m";

outputBlock.Text += String.Format("{0} ({1}) -> {2}", date, fmt1, date.ToString(fmt1)) + "\n";
outputBlock.Text += String.Format("{0} ({1}) -> {2}", date, fmt2, date.ToString(fmt2)) + "\n";
// The example displays the following output:
//       6/15/2009 1:45:30 PM (h \h m \m) -> 1 h 45 m
//       6/15/2009 1:45:30 PM (h \h m \m) -> 1 h 45 m      

Control Panel Settings

The Regional and Language Options settings in Control Panel influence the result string produced by a formatting operation that includes many of the custom date and time format specifiers. These settings are used to initialize the DateTimeFormatInfo object associated with the current thread culture, which provides values used to govern formatting. Computers that use different settings generate different result strings.

In addition, if you use the CultureInfo.CultureInfo(String) constructor to instantiate a new CultureInfo object that represents the same culture as the current system culture, any user customizations will be reflected by the new CultureInfo object.

DateTimeFormatInfo Properties

Formatting is influenced by properties of the current DateTimeFormatInfo object, which is provided implicitly by the current thread culture or explicitly by the IFormatProvider parameter of the method that invokes formatting. For the IFormatProvider parameter, you should specify a CultureInfo object, which represents a culture, or a DateTimeFormatInfo object.

The result string produced by many of the custom date and time format specifiers also depends on properties of the current DateTimeFormatInfo object. Your application can change the result produced by some custom date and time format specifiers by changing the corresponding DateTimeFormatInfo property. For example, the "ddd" format specifier adds an abbreviated weekday name found in the AbbreviatedDayNames string array to the result string. Similarly, the "MMMM" format specifier adds a full month name found in the MonthNames string array to the result string.

Change History

Date

History

Reason

October 2010

Added information on the use of custom format strings in formatting and parsing operations.

Customer feedback.