Export (0) Print
Expand All

DateTimeFormatInfo.TimeSeparator Property

Gets or sets the string that separates the components of time, that is, the hour, minutes, and seconds.

Namespace:  System.Globalization
Assembly:  mscorlib (in mscorlib.dll)

public string TimeSeparator { get; set; }

Property Value

Type: System.String
The string that separates the components of time. The default for InvariantInfo is ":".

ExceptionCondition
ArgumentNullException

The property is being set to null.

InvalidOperationException

The property is being set and the DateTimeFormatInfo object is read-only.

If the custom pattern includes the format pattern ":", DateTime.ToString displays the value of TimeSeparator in place of the ":" in the format pattern.

The time separator is derived from the ShortTimePattern property. We recommend that you set the time separator in short or long time patterns to an exact string instead of using the time separator placeholder. For example, to obtain the pattern h-mm-ss, set the pattern to "h-mm-ss". This practice also enables you to set patterns such as "h'h 'mm'm 'ss's'" (3h 36m 12s) that include multiple types of separators. The TimeSeparator property defines the string that replaces the time separator (":" custom date and time format specifier) in a result string in a formatting operation. It also defines the time separator string in a parsing operation.

The following example instantiates a CultureInfo object for the en-US culture, changes its date separator to ".", and displays a date by using the "t", "T", "F", "f", "G", and "g" standard format strings.

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      DateTime value = new DateTime(2013, 9, 8, 14, 30, 0);

      string[] formats = { "t", "T", "f", "F", "G", "g" };
      CultureInfo culture = CultureInfo.CreateSpecificCulture("en-US");
      DateTimeFormatInfo dtfi = culture.DateTimeFormat;
      dtfi.TimeSeparator = ".";

      foreach (var fmt in formats)
         Console.WriteLine("{0}: {1}", fmt, value.ToString(fmt, dtfi));
   }
}
// The example displays the following output: 
//       t: 2.30 PM 
//       T: 2.30.00 PM 
//       f: Sunday, September 08, 2013 2.30 PM 
//       F: Sunday, September 08, 2013 2.30.00 PM 
//       G: 9/8/2013 2.30.00 PM 
//       g: 9/8/2013 2.30 PM

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft