C#idiomatic C#
custom-timespan-format-stringsstandard/base-types

Core library

Custom TimeSpan format strings

Understand custom TimeSpan format strings in .NET. A custom format string contains one or more TimeSpan format specifiers & any number of literal characters.

  • page standard/base-types/custom-timespan-format-strings
  • updated 03/30/2017

A System.TimeSpan format string defines the string representation of a System.TimeSpan value that results from a formatting operation. A custom format string consists of one or more custom System.TimeSpan format specifiers along with any number of literal characters. Any string that isn't a Standard TimeSpan format string is interpreted as a custom System.TimeSpan format string.

The string representations of System.TimeSpan values are produced by calls to the overloads of the System.TimeSpan.ToString method, and by methods that support composite formatting, such as System.String.Format. For more information, see Formatting Types and Composite Formatting. The following example illustrates the use of custom format strings in formatting operations.

sample · csharpsamples/snippets/csharp/VS_Snippets_CLR/conceptual.timespan.custom/cs/customformatexample1.cs1

csharp
// <Snippet1>
using System;

public class Example
{
   public static void Main()
   {
      TimeSpan duration = new TimeSpan(1, 12, 23, 62);

      string output = null;
      output = "Time of Travel: " + duration.ToString("%d") + " days";
      Console.WriteLine(output);
      output = "Time of Travel: " + duration.ToString(@"dd\.hh\:mm\:ss");
      Console.WriteLine(output);

      Console.WriteLine($"Time of Travel: {duration:%d} day(s)");
      Console.WriteLine($"Time of Travel: {duration:dd\\.hh\\:mm\\:ss} days");
   }
}
// The example displays the following output:
//       Time of Travel: 1 days
//       Time of Travel: 01.12:24:02
//       Time of Travel: 1 day(s)
//       Time of Travel: 01.12:24:02 days
// </Snippet1>

sample · vbsamples/snippets/visualbasic/VS_Snippets_CLR/conceptual.timespan.custom/vb/customformatexample1.vb1

vb
' Visual Basic .NET Document
Option Strict On

' <Snippet1>
Module Example
    Public Sub Main()
        Dim duration As New TimeSpan(1, 12, 23, 62)

        Dim output As String = Nothing
        output = "Time of Travel: " + duration.ToString("%d") + " days"
        Console.WriteLine(output)
        output = "Time of Travel: " + duration.ToString("dd\.hh\:mm\:ss")
        Console.WriteLine(output)

        Console.WriteLine("Time of Travel: {0:%d} day(s)", duration)
        Console.WriteLine("Time of Travel: {0:dd\.hh\:mm\:ss} days", duration)
    End Sub
End Module
' The example displays the following output:
'       Time of Travel: 1 days
'       Time of Travel: 01.12:24:02
'       Time of Travel: 1 day(s)
'       Time of Travel: 01.12:24:02 days
' </Snippet1>

Custom System.TimeSpan format strings are also used by the System.TimeSpan.ParseExact and System.TimeSpan.TryParseExact methods to define the required format of input strings for parsing operations. (Parsing converts the string representation of a value to that value.) The following example illustrates the use of standard format strings in parsing operations.

sample · csharpsamples/snippets/csharp/VS_Snippets_CLR/conceptual.timespan.custom/cs/customparseexample1.cs2

csharp
// <Snippet2>
using System;

public class Example
{
   public static void Main()
   {
      string value = null;
      TimeSpan interval;

      value = "6";
      if (TimeSpan.TryParseExact(value, "%d", null, out interval))
         Console.WriteLine($"{value} --> {interval.ToString("c")}");
      else
         Console.WriteLine($"Unable to parse '{value}'");

      value = "16:32.05";
      if (TimeSpan.TryParseExact(value, @"mm\:ss\.ff", null, out interval))
         Console.WriteLine($"{value} --> {interval.ToString("c")}");
      else
         Console.WriteLine($"Unable to parse '{value}'");

      value= "12.035";
      if (TimeSpan.TryParseExact(value, "ss\\.fff", null, out interval))
         Console.WriteLine($"{value} --> {interval.ToString("c")}");
      else
         Console.WriteLine($"Unable to parse '{value}'");
   }
}
// The example displays the following output:
//       6 --> 6.00:00:00
//       16:32.05 --> 00:16:32.0500000
//       12.035 --> 00:00:12.0350000
// </Snippet2>

sample · vbsamples/snippets/visualbasic/VS_Snippets_CLR/conceptual.timespan.custom/vb/customparseexample1.vb2

vb
' Visual Basic .NET Document
Option Strict On

' <Snippet2>
Module Example
    Public Sub Main()
        Dim value As String = Nothing
        Dim interval As TimeSpan

        value = "6"
        If TimeSpan.TryParseExact(value, "%d", Nothing, interval) Then
            Console.WriteLine("{0} --> {1}", value, interval.ToString("c"))
        Else
            Console.WriteLine("Unable to parse '{0}'", value)
        End If

        value = "16:32.05"
        If TimeSpan.TryParseExact(value, "mm\:ss\.ff", Nothing, interval) Then
            Console.WriteLine("{0} --> {1}", value, interval.ToString("c"))
        Else
            Console.WriteLine("Unable to parse '{0}'", value)
        End If

        value = "12.035"
        If TimeSpan.TryParseExact(value, "ss\.fff", Nothing, interval) Then
            Console.WriteLine("{0} --> {1}", value, interval.ToString("c"))
        Else
            Console.WriteLine("Unable to parse '{0}'", value)
        End If
    End Sub
End Module
' The example displays the following output:
'       6 --> 6.00:00:00
'       16:32.05 --> 00:16:32.0500000
'       12.035 --> 00:00:12.0350000
' </Snippet2>

The following table describes the custom date and time format specifiers.

Format specifier Description Example
"d", "%d" The number of whole days in the time interval.

More information: The "d" custom format specifier.
new TimeSpan(6, 14, 32, 17, 685):

%d --> "6"

d\.hh\:mm --> "6.14:32"
"dd"-"dddddddd" The number of whole days in the time interval, padded with leading zeros as needed.

More information: The "dd"-"dddddddd" custom format specifiers.
new TimeSpan(6, 14, 32, 17, 685):

ddd --> "006"

dd\.hh\:mm --> "06.14:32"
"h", "%h" The number of whole hours in the time interval that aren't counted as part of days. Single-digit hours don't have a leading zero.

More information: The "h" custom format specifier.
new TimeSpan(6, 14, 32, 17, 685):

%h --> "14"

hh\:mm --> "14:32"
"hh" The number of whole hours in the time interval that aren't counted as part of days. Single-digit hours have a leading zero.

More information: The "hh" custom format specifier.
new TimeSpan(6, 14, 32, 17, 685):

hh --> "14"

new TimeSpan(6, 8, 32, 17, 685):

hh --> 08
"m", "%m" The number of whole minutes in the time interval that aren't included as part of hours or days. Single-digit minutes don't have a leading zero.

More information: The "m" custom format specifier.
new TimeSpan(6, 14, 8, 17, 685):

%m --> "8"

h\:m --> "14:8"
"mm" The number of whole minutes in the time interval that aren't included as part of hours or days. Single-digit minutes have a leading zero.

More information: The "mm" custom format specifier.
new TimeSpan(6, 14, 8, 17, 685):

mm --> "08"

new TimeSpan(6, 8, 5, 17, 685):

d\.hh\:mm\:ss --> 6.08:05:17
"s", "%s" The number of whole seconds in the time interval that aren't included as part of hours, days, or minutes. Single-digit seconds don't have a leading zero.

More information: The "s" custom format specifier.
TimeSpan.FromSeconds(12.965):

%s --> 12

s\.fff --> 12.965
"ss" The number of whole seconds in the time interval that aren't included as part of hours, days, or minutes. Single-digit seconds have a leading zero.

More information: The "ss" custom format specifier.
TimeSpan.FromSeconds(6.965):

ss --> 06

ss\.fff --> 06.965
"f", "%f" The tenths of a second in a time interval.

More information: The "f" custom format specifier.
TimeSpan.FromSeconds(6.895):

f --> 8

ss\.f --> 06.8
"ff" The hundredths of a second in a time interval.

More information: The "ff" custom format specifier.
TimeSpan.FromSeconds(6.895):

ff --> 89

ss\.ff --> 06.89
"fff" The milliseconds in a time interval.

More information: The "fff" custom format specifier.
TimeSpan.FromSeconds(6.895):

fff --> 895

ss\.fff --> 06.895
"ffff" The ten-thousandths of a second in a time interval.

More information: The "ffff" custom format specifier.
TimeSpan.Parse("0:0:6.8954321"):

ffff --> 8954

ss\.ffff --> 06.8954
"fffff" The hundred-thousandths of a second in a time interval.

More information: The "fffff" custom format specifier.
TimeSpan.Parse("0:0:6.8954321"):

fffff --> 89543

ss\.fffff --> 06.89543
"ffffff" The millionths of a second in a time interval.

More information: The "ffffff" custom format specifier.
TimeSpan.Parse("0:0:6.8954321"):

ffffff --> 895432

ss\.ffffff --> 06.895432
"fffffff" The ten-millionths of a second (or the fractional ticks) in a time interval.

More information: The "fffffff" custom format specifier.
TimeSpan.Parse("0:0:6.8954321"):

fffffff --> 8954321

ss\.fffffff --> 06.8954321
"F", "%F" The tenths of a second in a time interval. Nothing is displayed if the digit is zero.

More information: The "F" custom format specifier.
TimeSpan.Parse("00:00:06.32"):

%F: 3

TimeSpan.Parse("0:0:3.091"):

ss\.F: 03.
"FF" The hundredths of a second in a time interval. Any fractional trailing zeros or two zero digits aren't included.

More information: The "FF" custom format specifier.
TimeSpan.Parse("00:00:06.329"):

FF: 32

TimeSpan.Parse("0:0:3.101"):

ss\.FF: 03.1
"FFF" The milliseconds in a time interval. Any fractional trailing zeros aren't included.

More information:
TimeSpan.Parse("00:00:06.3291"):

FFF: 329

TimeSpan.Parse("0:0:3.1009"):

ss\.FFF: 03.1
"FFFF" The ten-thousandths of a second in a time interval. Any fractional trailing zeros aren't included.

More information: The "FFFF" custom format specifier.
TimeSpan.Parse("00:00:06.32917"):

FFFFF: 3291

TimeSpan.Parse("0:0:3.10009"):

ss\.FFFF: 03.1
"FFFFF" The hundred-thousandths of a second in a time interval. Any fractional trailing zeros aren't included.

More information: The "FFFFF" custom format specifier.
TimeSpan.Parse("00:00:06.329179"):

FFFFF: 32917

TimeSpan.Parse("0:0:3.100009"):

ss\.FFFFF: 03.1
"FFFFFF" The millionths of a second in a time interval. Any fractional trailing zeros aren't displayed.

More information: The "FFFFFF" custom format specifier.
TimeSpan.Parse("00:00:06.3291791"):

FFFFFF: 329179

TimeSpan.Parse("0:0:3.1000009"):

ss\.FFFFFF: 03.1
"FFFFFFF" The ten-millions of a second in a time interval. Any fractional trailing zeros or seven zeros aren't displayed.

More information: The "FFFFFFF" custom format specifier.
TimeSpan.Parse("00:00:06.3291791"):

FFFFFF: 3291791

TimeSpan.Parse("0:0:3.1900000"):

ss\.FFFFFF: 03.19
'string' Literal string delimiter.

More information: Other characters.
new TimeSpan(14, 32, 17):

hh':'mm':'ss --> "14:32:17"
\ The escape character.

More information: Other characters.
new TimeSpan(14, 32, 17):

hh\:mm\:ss --> "14:32:17"
Any other character Any other unescaped character is interpreted as a custom format specifier.

More Information: Other characters.
new TimeSpan(14, 32, 17):

hh\:mm\:ss --> "14:32:17"

The "d" custom format specifier

The "d" custom format specifier outputs the value of the System.TimeSpan.Days property, which represents the number of whole days in the time interval. It outputs the full number of days in a System.TimeSpan value, even if the value has more than one digit. If the value of the System.TimeSpan.Days property is zero, the specifier outputs "0".

If the "d" custom format specifier is used alone, specify "%d" so that it isn't misinterpreted as a standard format string. The following example provides an illustration.

sample · csharpsamples/snippets/csharp/VS_Snippets_CLR/conceptual.timespan.custom/cs/customexamples1.cs3

csharp
using System;

public class Example
{
   public static void Main()
   {
      dSpecifier();
      Console.WriteLine("-------");
      ddSpecifier();
      Console.WriteLine("-------");
      hSpecifier();
      Console.WriteLine("-------");
      ParseH();
      Console.WriteLine("-------");
      ParseHH();
      Console.WriteLine("-------");
      hhSpecifier();
      Console.WriteLine("-------");
      ParseM();
      Console.WriteLine("-------");
      mSpecifier();
      Console.WriteLine("-------");
      ParseMM();
      Console.WriteLine("-------");
      mmSpecifier();
      Console.WriteLine("-------");
      sSpecifier();
      Console.WriteLine("-------");
      ParseS();
      Console.WriteLine("-------");
      ParseSS();
      Console.WriteLine("-------");
      ssSpecifier();
      Console.WriteLine("-------");
   }

   private static void dSpecifier()
   {
      // <Snippet3>
      TimeSpan ts1 = new TimeSpan(16, 4, 3, 17, 250);
      Console.WriteLine(ts1.ToString("%d"));
      // Displays 16
      // </Snippet3>
      Console.WriteLine();

      // <Snippet4>
      TimeSpan ts2 = new TimeSpan(4, 3, 17);
      Console.WriteLine(ts2.ToString(@"d\.hh\:mm\:ss"));

      TimeSpan ts3 = new TimeSpan(3, 4, 3, 17);
      Console.WriteLine(ts3.ToString(@"d\.hh\:mm\:ss"));
      // The example displays the following output:
      //       0.04:03:17
      //       3.04:03:17
      // </Snippet4>
   }

   private static void ddSpecifier()
   {
      // <Snippet5>
      TimeSpan ts1 = new TimeSpan(0, 23, 17, 47);
      TimeSpan ts2 = new TimeSpan(365, 21, 19, 45);

      for (int ctr = 2; ctr <= 8; ctr++)
      {
         string fmt = new String('d', ctr) + @"\.hh\:mm\:ss";
         Console.WriteLine($"{fmt} --> {ts1.ToString(fmt)}");
         Console.WriteLine($"{fmt} --> {ts2.ToString(fmt)}");
         Console.WriteLine();
      }
      // The example displays the following output:
      //       dd\.hh\:mm\:ss --> 00.23:17:47
      //       dd\.hh\:mm\:ss --> 365.21:19:45
      //
      //       ddd\.hh\:mm\:ss --> 000.23:17:47
      //       ddd\.hh\:mm\:ss --> 365.21:19:45
      //
      //       dddd\.hh\:mm\:ss --> 0000.23:17:47
      //       dddd\.hh\:mm\:ss --> 0365.21:19:45
      //
      //       ddddd\.hh\:mm\:ss --> 00000.23:17:47
      //       ddddd\.hh\:mm\:ss --> 00365.21:19:45
      //
      //       dddddd\.hh\:mm\:ss --> 000000.23:17:47
      //       dddddd\.hh\:mm\:ss --> 000365.21:19:45
      //
      //       ddddddd\.hh\:mm\:ss --> 0000000.23:17:47
      //       ddddddd\.hh\:mm\:ss --> 0000365.21:19:45
      //
      //       dddddddd\.hh\:mm\:ss --> 00000000.23:17:47
      //       dddddddd\.hh\:mm\:ss --> 00000365.21:19:45
      // </Snippet5>
   }

   private static void hSpecifier()
   {
      // <Snippet6>
      TimeSpan ts = new TimeSpan(3, 42, 0);
      Console.WriteLine($"{ts:%h} hours {ts:%m} minutes");
      // The example displays the following output:
      //       3 hours 42 minutes
      // </Snippet6>

      // <Snippet7>
      TimeSpan ts1 = new TimeSpan(14, 3, 17);
      Console.WriteLine(ts1.ToString(@"d\.h\:mm\:ss"));

      TimeSpan ts2 = new TimeSpan(3, 4, 3, 17);
      Console.WriteLine(ts2.ToString(@"d\.h\:mm\:ss"));
      // The example displays the following output:
      //       0.14:03:17
      //       3.4:03:17
      // </Snippet7>
   }

   private static void ParseH()
   {
      // <Snippet8>
      string value = "8";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "%h", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       08:00:00
      // </Snippet8>
   }

   private static void ParseHH()
   {
      // <Snippet9>
      string value = "08";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "hh", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       08:00:00
      // </Snippet9>
   }

   private static void hhSpecifier()
   {
      // <Snippet10>
      TimeSpan ts1 = new TimeSpan(14, 3, 17);
      Console.WriteLine(ts1.ToString(@"d\.hh\:mm\:ss"));

      TimeSpan ts2 = new TimeSpan(3, 4, 3, 17);
      Console.WriteLine(ts2.ToString(@"d\.hh\:mm\:ss"));
      // The example displays the following output:
      //       0.14:03:17
      //       3.04:03:17
      // </Snippet10>
   }

   private static void ParseM()
   {
      // <Snippet11>
      string value = "3";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "%m", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       00:03:00
      // </Snippet11>
   }

   private static void mSpecifier()
   {
      // <Snippet12>
      TimeSpan ts1 = new TimeSpan(0, 6, 32);
      Console.WriteLine($"{ts1:m\\:ss} minutes");

      TimeSpan ts2 = new TimeSpan(3, 4, 3, 17);
      Console.WriteLine($"Elapsed time: {ts2:m\\:ss}");
      // The example displays the following output:
      //       6:32 minutes
      //       Elapsed time: 18:44
      // </Snippet12>
   }

   private static void ParseMM()
   {
      // <Snippet13>
      string value = "07";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "mm", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       00:07:00
      // </Snippet13>
   }

   private static void mmSpecifier()
   {
      // <Snippet14>
      TimeSpan departTime = new TimeSpan(11, 12, 00);
      TimeSpan arriveTime = new TimeSpan(16, 28, 00);
      Console.WriteLine($"Travel time: {arriveTime - departTime:hh\\:mm}");
      // The example displays the following output:
      //       Travel time: 05:16
      // </Snippet14>
   }

   private static void sSpecifier()
   {
      // <Snippet15>
      TimeSpan ts = TimeSpan.FromSeconds(12.465);
      Console.WriteLine(ts.ToString("%s"));
      // The example displays the following output:
      //       12
      // </Snippet15>
      Console.WriteLine();

      // <Snippet16>
      TimeSpan startTime = new TimeSpan(0, 12, 30, 15, 0);
      TimeSpan endTime = new TimeSpan(0, 12, 30, 21, 3);
      Console.WriteLine(@"Elapsed Time: {0:s\:fff} seconds",
                        endTime - startTime);
      // The example displays the following output:
      //       Elapsed Time: 6:003 seconds
      // </Snippet16>
   }

   private static void ParseS()
   {
      // <Snippet17>
      string value = "9";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "%s", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       00:00:09
      // </Snippet17>
   }

   private static void ParseSS()
   {
      // <Snippet18>
      string[] values = { "49", "9", "06" };
      TimeSpan interval;
      foreach (string value in values)
      {
         if (TimeSpan.TryParseExact(value, "ss", null, out interval))
            Console.WriteLine(interval.ToString("c"));
         else
            Console.WriteLine($"Unable to convert '{value}' to a time interval");
      }
      // The example displays the following output:
      //       00:00:49
      //       Unable to convert '9' to a time interval
      //       00:00:06
      // </Snippet18>
   }

   private static void ssSpecifier()
   {
      // <Snippet19>
      TimeSpan interval1 = TimeSpan.FromSeconds(12.60);
      Console.WriteLine(interval1.ToString(@"ss\.fff"));

      TimeSpan interval2 = TimeSpan.FromSeconds(6.485);
      Console.WriteLine(interval2.ToString(@"ss\.fff"));
      // The example displays the following output:
      //       12.600
      //       06.485
      // </Snippet19>
   }
}

sample · vbsamples/snippets/visualbasic/VS_Snippets_CLR/conceptual.timespan.custom/vb/customexamples1.vb3

vb
' Visual Basic .NET Document
Option Strict On

Module Example
    Public Sub Main()
        dSpecifier()
        Console.WriteLine("-------")
        ddSpecifier()
        Console.WriteLine("-------")
        hSpecifier()
        Console.WriteLine("-------")
        ParseH()
        Console.WriteLine("-------")
        ParseHH()
        Console.WriteLine("-------")
        hhSpecifier()
        Console.WriteLine("-------")
        ParseM()
        Console.WriteLine("-------")
        mSpecifier()
        Console.WriteLine("-------")
        ParseMM()
        Console.WriteLine("-------")
        mmSpecifier()
        Console.WriteLine("-------")
        sSpecifier()
        Console.WriteLine("-------")
        ParseS()
        Console.WriteLine("-------")
        ParseSS()
        Console.WriteLine("-------")
        ssSpecifier()
        Console.WriteLine("-------")
    End Sub

    Private Sub dSpecifier()
        ' <Snippet3>
        Dim ts As New TimeSpan(16, 4, 3, 17, 250)
        Console.WriteLine(ts.ToString("%d"))
        ' Displays 16   
        ' </Snippet3>
        Console.WriteLine()

        ' <Snippet4>
        Dim ts2 As New TimeSpan(4, 3, 17)
        Console.WriteLine(ts2.ToString("d\.hh\:mm\:ss"))

        Dim ts3 As New TimeSpan(3, 4, 3, 17)
        Console.WriteLine(ts3.ToString("d\.hh\:mm\:ss"))
        ' The example displays the following output:
        '       0.04:03:17
        '       3.04:03:17      
        ' </Snippet4>
    End Sub

    Private Sub ddSpecifier()
        ' <Snippet5>
        Dim ts1 As New TimeSpan(0, 23, 17, 47)
        Dim ts2 As New TimeSpan(365, 21, 19, 45)

        For ctr As Integer = 2 To 8
            Dim fmt As String = New String("d"c, ctr) + "\.hh\:mm\:ss"
            Console.WriteLine("{0} --> {1:" + fmt + "}", fmt, ts1)
            Console.WriteLine("{0} --> {1:" + fmt + "}", fmt, ts2)
            Console.WriteLine()
        Next
        ' The example displays the following output:
        '       dd\.hh\:mm\:ss --> 00.23:17:47
        '       dd\.hh\:mm\:ss --> 365.21:19:45
        '       
        '       ddd\.hh\:mm\:ss --> 000.23:17:47
        '       ddd\.hh\:mm\:ss --> 365.21:19:45
        '       
        '       dddd\.hh\:mm\:ss --> 0000.23:17:47
        '       dddd\.hh\:mm\:ss --> 0365.21:19:45
        '       
        '       ddddd\.hh\:mm\:ss --> 00000.23:17:47
        '       ddddd\.hh\:mm\:ss --> 00365.21:19:45
        '       
        '       dddddd\.hh\:mm\:ss --> 000000.23:17:47
        '       dddddd\.hh\:mm\:ss --> 000365.21:19:45
        '       
        '       ddddddd\.hh\:mm\:ss --> 0000000.23:17:47
        '       ddddddd\.hh\:mm\:ss --> 0000365.21:19:45
        '       
        '       dddddddd\.hh\:mm\:ss --> 00000000.23:17:47
        '       dddddddd\.hh\:mm\:ss --> 00000365.21:19:45      
        ' </Snippet5>
    End Sub

    Private Sub hSpecifier()
        ' <Snippet6>
        Dim ts As New TimeSpan(3, 42, 0)
        Console.WriteLine("{0:%h} hours {0:%m} minutes", ts)
        ' The example displays the following output:
        '       3 hours 42 minutes
        ' </Snippet6>
        Console.WriteLine()

        ' <Snippet7>
        Dim ts1 As New TimeSpan(14, 3, 17)
        Console.WriteLine(ts1.ToString("d\.h\:mm\:ss"))

        Dim ts2 As New TimeSpan(3, 4, 3, 17)
        Console.WriteLine(ts2.ToString("d\.h\:mm\:ss"))
        ' The example displays the following output:
        '       0.14:03:17
        '       3.4:03:17
        ' </Snippet7>
    End Sub

    Public Sub ParseH()
        ' <Snippet8>
        Dim value As String = "8"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "%h", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       08:00:00                              
        ' </Snippet8>
    End Sub

    Public Sub ParseHH()
        ' <Snippet9>
        Dim value As String = "08"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "hh", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       08:00:00                              
        ' </Snippet9>
    End Sub

    Private Sub hhSpecifier()
        ' <Snippet10>
        Dim ts1 As New TimeSpan(14, 3, 17)
        Console.WriteLine(ts1.ToString("d\.hh\:mm\:ss"))

        Dim ts2 As New TimeSpan(3, 4, 3, 17)
        Console.WriteLine(ts2.ToString("d\.hh\:mm\:ss"))
        ' The example displays the following output:
        '       0.14:03:17
        '       3.04:03:17
        ' </Snippet10>
    End Sub

    Public Sub ParseM()
        ' <Snippet11>
        Dim value As String = "3"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "%m", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       00:03:00                              
        ' </Snippet11>
    End Sub

    Private Sub mSpecifier()
        ' <Snippet12>
        Dim ts1 As New TimeSpan(0, 6, 32)
        Console.WriteLine("{0:m\:ss} minutes", ts1)

        Dim ts2 As New TimeSpan(0, 18, 44)
        Console.WriteLine("Elapsed time: {0:m\:ss}", ts2)
        ' The example displays the following output:
        '       6:32 minutes
        '       Elapsed time: 18:44
        ' </Snippet12>
    End Sub

    Public Sub ParseMM()
        ' <Snippet13>
        Dim value As String = "05"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "mm", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       00:05:00           
        ' </Snippet13>
    End Sub

    Private Sub mmSpecifier()
        ' <Snippet14>
        Dim departTime As New TimeSpan(11, 12, 00)
        Dim arriveTime As New TimeSpan(16, 28, 00)
        Console.WriteLine("Travel time: {0:hh\:mm}",
                          arriveTime - departTime)
        ' The example displays the following output:
        '       Travel time: 05:16      
        ' </Snippet14>
    End Sub

    Private Sub sSpecifier()
        ' <Snippet15>
        Dim ts As TimeSpan = TimeSpan.FromSeconds(12.465)
        Console.WriteLine(ts.ToString("%s"))
        ' The example displays the following output:
        '       12
        ' </Snippet15>
        Console.WriteLine()

        ' <Snippet16>
        Dim startTime As New TimeSpan(0, 12, 30, 15, 0)
        Dim endTime As New TimeSpan(0, 12, 30, 21, 3)
        Console.WriteLine("Elapsed Time: {0:s\:fff} seconds",
                          endTime - startTime)
        ' The example displays the following output:
        '       Elapsed Time: 6:003 seconds      
        ' </Snippet16>
    End Sub

    Public Sub ParseS()
        ' <Snippet17>
        Dim value As String = "9"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "%s", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       00:00:09
        ' </Snippet17>
    End Sub

    Public Sub ParseSS()
        ' <Snippet18>
        Dim values() As String = {"49", "9", "06"}
        Dim interval As TimeSpan
        For Each value As String In values
            If TimeSpan.TryParseExact(value, "ss", Nothing, interval) Then
                Console.WriteLine(interval.ToString("c"))
            Else
                Console.WriteLine("Unable to convert '{0}' to a time interval",
                                  value)
            End If
        Next
        ' The example displays the following output:
        '       00:00:49
        '       Unable to convert '9' to a time interval
        '       00:00:06
        ' </Snippet18>
    End Sub

    Private Sub ssSpecifier()
        ' <Snippet19>
        Dim interval1 As TimeSpan = TimeSpan.FromSeconds(12.60)
        Console.WriteLine(interval1.ToString("ss\.fff"))
        Dim interval2 As TimeSpan = TimeSpan.FromSeconds(6.485)
        Console.WriteLine(interval2.ToString("ss\.fff"))
        ' The example displays the following output:
        '       12.600
        '       06.485
        ' </Snippet19>
    End Sub
End Module

The following example illustrates the use of the "d" custom format specifier.

sample · csharpsamples/snippets/csharp/VS_Snippets_CLR/conceptual.timespan.custom/cs/customexamples1.cs4

csharp
using System;

public class Example
{
   public static void Main()
   {
      dSpecifier();
      Console.WriteLine("-------");
      ddSpecifier();
      Console.WriteLine("-------");
      hSpecifier();
      Console.WriteLine("-------");
      ParseH();
      Console.WriteLine("-------");
      ParseHH();
      Console.WriteLine("-------");
      hhSpecifier();
      Console.WriteLine("-------");
      ParseM();
      Console.WriteLine("-------");
      mSpecifier();
      Console.WriteLine("-------");
      ParseMM();
      Console.WriteLine("-------");
      mmSpecifier();
      Console.WriteLine("-------");
      sSpecifier();
      Console.WriteLine("-------");
      ParseS();
      Console.WriteLine("-------");
      ParseSS();
      Console.WriteLine("-------");
      ssSpecifier();
      Console.WriteLine("-------");
   }

   private static void dSpecifier()
   {
      // <Snippet3>
      TimeSpan ts1 = new TimeSpan(16, 4, 3, 17, 250);
      Console.WriteLine(ts1.ToString("%d"));
      // Displays 16
      // </Snippet3>
      Console.WriteLine();

      // <Snippet4>
      TimeSpan ts2 = new TimeSpan(4, 3, 17);
      Console.WriteLine(ts2.ToString(@"d\.hh\:mm\:ss"));

      TimeSpan ts3 = new TimeSpan(3, 4, 3, 17);
      Console.WriteLine(ts3.ToString(@"d\.hh\:mm\:ss"));
      // The example displays the following output:
      //       0.04:03:17
      //       3.04:03:17
      // </Snippet4>
   }

   private static void ddSpecifier()
   {
      // <Snippet5>
      TimeSpan ts1 = new TimeSpan(0, 23, 17, 47);
      TimeSpan ts2 = new TimeSpan(365, 21, 19, 45);

      for (int ctr = 2; ctr <= 8; ctr++)
      {
         string fmt = new String('d', ctr) + @"\.hh\:mm\:ss";
         Console.WriteLine($"{fmt} --> {ts1.ToString(fmt)}");
         Console.WriteLine($"{fmt} --> {ts2.ToString(fmt)}");
         Console.WriteLine();
      }
      // The example displays the following output:
      //       dd\.hh\:mm\:ss --> 00.23:17:47
      //       dd\.hh\:mm\:ss --> 365.21:19:45
      //
      //       ddd\.hh\:mm\:ss --> 000.23:17:47
      //       ddd\.hh\:mm\:ss --> 365.21:19:45
      //
      //       dddd\.hh\:mm\:ss --> 0000.23:17:47
      //       dddd\.hh\:mm\:ss --> 0365.21:19:45
      //
      //       ddddd\.hh\:mm\:ss --> 00000.23:17:47
      //       ddddd\.hh\:mm\:ss --> 00365.21:19:45
      //
      //       dddddd\.hh\:mm\:ss --> 000000.23:17:47
      //       dddddd\.hh\:mm\:ss --> 000365.21:19:45
      //
      //       ddddddd\.hh\:mm\:ss --> 0000000.23:17:47
      //       ddddddd\.hh\:mm\:ss --> 0000365.21:19:45
      //
      //       dddddddd\.hh\:mm\:ss --> 00000000.23:17:47
      //       dddddddd\.hh\:mm\:ss --> 00000365.21:19:45
      // </Snippet5>
   }

   private static void hSpecifier()
   {
      // <Snippet6>
      TimeSpan ts = new TimeSpan(3, 42, 0);
      Console.WriteLine($"{ts:%h} hours {ts:%m} minutes");
      // The example displays the following output:
      //       3 hours 42 minutes
      // </Snippet6>

      // <Snippet7>
      TimeSpan ts1 = new TimeSpan(14, 3, 17);
      Console.WriteLine(ts1.ToString(@"d\.h\:mm\:ss"));

      TimeSpan ts2 = new TimeSpan(3, 4, 3, 17);
      Console.WriteLine(ts2.ToString(@"d\.h\:mm\:ss"));
      // The example displays the following output:
      //       0.14:03:17
      //       3.4:03:17
      // </Snippet7>
   }

   private static void ParseH()
   {
      // <Snippet8>
      string value = "8";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "%h", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       08:00:00
      // </Snippet8>
   }

   private static void ParseHH()
   {
      // <Snippet9>
      string value = "08";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "hh", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       08:00:00
      // </Snippet9>
   }

   private static void hhSpecifier()
   {
      // <Snippet10>
      TimeSpan ts1 = new TimeSpan(14, 3, 17);
      Console.WriteLine(ts1.ToString(@"d\.hh\:mm\:ss"));

      TimeSpan ts2 = new TimeSpan(3, 4, 3, 17);
      Console.WriteLine(ts2.ToString(@"d\.hh\:mm\:ss"));
      // The example displays the following output:
      //       0.14:03:17
      //       3.04:03:17
      // </Snippet10>
   }

   private static void ParseM()
   {
      // <Snippet11>
      string value = "3";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "%m", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       00:03:00
      // </Snippet11>
   }

   private static void mSpecifier()
   {
      // <Snippet12>
      TimeSpan ts1 = new TimeSpan(0, 6, 32);
      Console.WriteLine($"{ts1:m\\:ss} minutes");

      TimeSpan ts2 = new TimeSpan(3, 4, 3, 17);
      Console.WriteLine($"Elapsed time: {ts2:m\\:ss}");
      // The example displays the following output:
      //       6:32 minutes
      //       Elapsed time: 18:44
      // </Snippet12>
   }

   private static void ParseMM()
   {
      // <Snippet13>
      string value = "07";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "mm", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       00:07:00
      // </Snippet13>
   }

   private static void mmSpecifier()
   {
      // <Snippet14>
      TimeSpan departTime = new TimeSpan(11, 12, 00);
      TimeSpan arriveTime = new TimeSpan(16, 28, 00);
      Console.WriteLine($"Travel time: {arriveTime - departTime:hh\\:mm}");
      // The example displays the following output:
      //       Travel time: 05:16
      // </Snippet14>
   }

   private static void sSpecifier()
   {
      // <Snippet15>
      TimeSpan ts = TimeSpan.FromSeconds(12.465);
      Console.WriteLine(ts.ToString("%s"));
      // The example displays the following output:
      //       12
      // </Snippet15>
      Console.WriteLine();

      // <Snippet16>
      TimeSpan startTime = new TimeSpan(0, 12, 30, 15, 0);
      TimeSpan endTime = new TimeSpan(0, 12, 30, 21, 3);
      Console.WriteLine(@"Elapsed Time: {0:s\:fff} seconds",
                        endTime - startTime);
      // The example displays the following output:
      //       Elapsed Time: 6:003 seconds
      // </Snippet16>
   }

   private static void ParseS()
   {
      // <Snippet17>
      string value = "9";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "%s", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       00:00:09
      // </Snippet17>
   }

   private static void ParseSS()
   {
      // <Snippet18>
      string[] values = { "49", "9", "06" };
      TimeSpan interval;
      foreach (string value in values)
      {
         if (TimeSpan.TryParseExact(value, "ss", null, out interval))
            Console.WriteLine(interval.ToString("c"));
         else
            Console.WriteLine($"Unable to convert '{value}' to a time interval");
      }
      // The example displays the following output:
      //       00:00:49
      //       Unable to convert '9' to a time interval
      //       00:00:06
      // </Snippet18>
   }

   private static void ssSpecifier()
   {
      // <Snippet19>
      TimeSpan interval1 = TimeSpan.FromSeconds(12.60);
      Console.WriteLine(interval1.ToString(@"ss\.fff"));

      TimeSpan interval2 = TimeSpan.FromSeconds(6.485);
      Console.WriteLine(interval2.ToString(@"ss\.fff"));
      // The example displays the following output:
      //       12.600
      //       06.485
      // </Snippet19>
   }
}

sample · vbsamples/snippets/visualbasic/VS_Snippets_CLR/conceptual.timespan.custom/vb/customexamples1.vb4

vb
' Visual Basic .NET Document
Option Strict On

Module Example
    Public Sub Main()
        dSpecifier()
        Console.WriteLine("-------")
        ddSpecifier()
        Console.WriteLine("-------")
        hSpecifier()
        Console.WriteLine("-------")
        ParseH()
        Console.WriteLine("-------")
        ParseHH()
        Console.WriteLine("-------")
        hhSpecifier()
        Console.WriteLine("-------")
        ParseM()
        Console.WriteLine("-------")
        mSpecifier()
        Console.WriteLine("-------")
        ParseMM()
        Console.WriteLine("-------")
        mmSpecifier()
        Console.WriteLine("-------")
        sSpecifier()
        Console.WriteLine("-------")
        ParseS()
        Console.WriteLine("-------")
        ParseSS()
        Console.WriteLine("-------")
        ssSpecifier()
        Console.WriteLine("-------")
    End Sub

    Private Sub dSpecifier()
        ' <Snippet3>
        Dim ts As New TimeSpan(16, 4, 3, 17, 250)
        Console.WriteLine(ts.ToString("%d"))
        ' Displays 16   
        ' </Snippet3>
        Console.WriteLine()

        ' <Snippet4>
        Dim ts2 As New TimeSpan(4, 3, 17)
        Console.WriteLine(ts2.ToString("d\.hh\:mm\:ss"))

        Dim ts3 As New TimeSpan(3, 4, 3, 17)
        Console.WriteLine(ts3.ToString("d\.hh\:mm\:ss"))
        ' The example displays the following output:
        '       0.04:03:17
        '       3.04:03:17      
        ' </Snippet4>
    End Sub

    Private Sub ddSpecifier()
        ' <Snippet5>
        Dim ts1 As New TimeSpan(0, 23, 17, 47)
        Dim ts2 As New TimeSpan(365, 21, 19, 45)

        For ctr As Integer = 2 To 8
            Dim fmt As String = New String("d"c, ctr) + "\.hh\:mm\:ss"
            Console.WriteLine("{0} --> {1:" + fmt + "}", fmt, ts1)
            Console.WriteLine("{0} --> {1:" + fmt + "}", fmt, ts2)
            Console.WriteLine()
        Next
        ' The example displays the following output:
        '       dd\.hh\:mm\:ss --> 00.23:17:47
        '       dd\.hh\:mm\:ss --> 365.21:19:45
        '       
        '       ddd\.hh\:mm\:ss --> 000.23:17:47
        '       ddd\.hh\:mm\:ss --> 365.21:19:45
        '       
        '       dddd\.hh\:mm\:ss --> 0000.23:17:47
        '       dddd\.hh\:mm\:ss --> 0365.21:19:45
        '       
        '       ddddd\.hh\:mm\:ss --> 00000.23:17:47
        '       ddddd\.hh\:mm\:ss --> 00365.21:19:45
        '       
        '       dddddd\.hh\:mm\:ss --> 000000.23:17:47
        '       dddddd\.hh\:mm\:ss --> 000365.21:19:45
        '       
        '       ddddddd\.hh\:mm\:ss --> 0000000.23:17:47
        '       ddddddd\.hh\:mm\:ss --> 0000365.21:19:45
        '       
        '       dddddddd\.hh\:mm\:ss --> 00000000.23:17:47
        '       dddddddd\.hh\:mm\:ss --> 00000365.21:19:45      
        ' </Snippet5>
    End Sub

    Private Sub hSpecifier()
        ' <Snippet6>
        Dim ts As New TimeSpan(3, 42, 0)
        Console.WriteLine("{0:%h} hours {0:%m} minutes", ts)
        ' The example displays the following output:
        '       3 hours 42 minutes
        ' </Snippet6>
        Console.WriteLine()

        ' <Snippet7>
        Dim ts1 As New TimeSpan(14, 3, 17)
        Console.WriteLine(ts1.ToString("d\.h\:mm\:ss"))

        Dim ts2 As New TimeSpan(3, 4, 3, 17)
        Console.WriteLine(ts2.ToString("d\.h\:mm\:ss"))
        ' The example displays the following output:
        '       0.14:03:17
        '       3.4:03:17
        ' </Snippet7>
    End Sub

    Public Sub ParseH()
        ' <Snippet8>
        Dim value As String = "8"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "%h", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       08:00:00                              
        ' </Snippet8>
    End Sub

    Public Sub ParseHH()
        ' <Snippet9>
        Dim value As String = "08"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "hh", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       08:00:00                              
        ' </Snippet9>
    End Sub

    Private Sub hhSpecifier()
        ' <Snippet10>
        Dim ts1 As New TimeSpan(14, 3, 17)
        Console.WriteLine(ts1.ToString("d\.hh\:mm\:ss"))

        Dim ts2 As New TimeSpan(3, 4, 3, 17)
        Console.WriteLine(ts2.ToString("d\.hh\:mm\:ss"))
        ' The example displays the following output:
        '       0.14:03:17
        '       3.04:03:17
        ' </Snippet10>
    End Sub

    Public Sub ParseM()
        ' <Snippet11>
        Dim value As String = "3"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "%m", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       00:03:00                              
        ' </Snippet11>
    End Sub

    Private Sub mSpecifier()
        ' <Snippet12>
        Dim ts1 As New TimeSpan(0, 6, 32)
        Console.WriteLine("{0:m\:ss} minutes", ts1)

        Dim ts2 As New TimeSpan(0, 18, 44)
        Console.WriteLine("Elapsed time: {0:m\:ss}", ts2)
        ' The example displays the following output:
        '       6:32 minutes
        '       Elapsed time: 18:44
        ' </Snippet12>
    End Sub

    Public Sub ParseMM()
        ' <Snippet13>
        Dim value As String = "05"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "mm", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       00:05:00           
        ' </Snippet13>
    End Sub

    Private Sub mmSpecifier()
        ' <Snippet14>
        Dim departTime As New TimeSpan(11, 12, 00)
        Dim arriveTime As New TimeSpan(16, 28, 00)
        Console.WriteLine("Travel time: {0:hh\:mm}",
                          arriveTime - departTime)
        ' The example displays the following output:
        '       Travel time: 05:16      
        ' </Snippet14>
    End Sub

    Private Sub sSpecifier()
        ' <Snippet15>
        Dim ts As TimeSpan = TimeSpan.FromSeconds(12.465)
        Console.WriteLine(ts.ToString("%s"))
        ' The example displays the following output:
        '       12
        ' </Snippet15>
        Console.WriteLine()

        ' <Snippet16>
        Dim startTime As New TimeSpan(0, 12, 30, 15, 0)
        Dim endTime As New TimeSpan(0, 12, 30, 21, 3)
        Console.WriteLine("Elapsed Time: {0:s\:fff} seconds",
                          endTime - startTime)
        ' The example displays the following output:
        '       Elapsed Time: 6:003 seconds      
        ' </Snippet16>
    End Sub

    Public Sub ParseS()
        ' <Snippet17>
        Dim value As String = "9"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "%s", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       00:00:09
        ' </Snippet17>
    End Sub

    Public Sub ParseSS()
        ' <Snippet18>
        Dim values() As String = {"49", "9", "06"}
        Dim interval As TimeSpan
        For Each value As String In values
            If TimeSpan.TryParseExact(value, "ss", Nothing, interval) Then
                Console.WriteLine(interval.ToString("c"))
            Else
                Console.WriteLine("Unable to convert '{0}' to a time interval",
                                  value)
            End If
        Next
        ' The example displays the following output:
        '       00:00:49
        '       Unable to convert '9' to a time interval
        '       00:00:06
        ' </Snippet18>
    End Sub

    Private Sub ssSpecifier()
        ' <Snippet19>
        Dim interval1 As TimeSpan = TimeSpan.FromSeconds(12.60)
        Console.WriteLine(interval1.ToString("ss\.fff"))
        Dim interval2 As TimeSpan = TimeSpan.FromSeconds(6.485)
        Console.WriteLine(interval2.ToString("ss\.fff"))
        ' The example displays the following output:
        '       12.600
        '       06.485
        ' </Snippet19>
    End Sub
End Module

Back to table

The "dd"-"dddddddd" custom format specifiers

The "dd", "ddd", "dddd", "ddddd", "dddddd", "ddddddd", and "dddddddd" custom format specifiers output the value of the System.TimeSpan.Days property, which represents the number of whole days in the time interval.

The output string includes a minimum number of digits specified by the number of d characters in the format specifier, and it's padded with leading zeros as needed. If the digits in the number of days exceed the number of d characters in the format specifier, the full number of days is output in the result string.

The following example uses these format specifiers to display the string representation of two System.TimeSpan values. The value of the days component of the first time interval is zero; the value of the days component of the second is 365.

sample · csharpsamples/snippets/csharp/VS_Snippets_CLR/conceptual.timespan.custom/cs/customexamples1.cs5

csharp
using System;

public class Example
{
   public static void Main()
   {
      dSpecifier();
      Console.WriteLine("-------");
      ddSpecifier();
      Console.WriteLine("-------");
      hSpecifier();
      Console.WriteLine("-------");
      ParseH();
      Console.WriteLine("-------");
      ParseHH();
      Console.WriteLine("-------");
      hhSpecifier();
      Console.WriteLine("-------");
      ParseM();
      Console.WriteLine("-------");
      mSpecifier();
      Console.WriteLine("-------");
      ParseMM();
      Console.WriteLine("-------");
      mmSpecifier();
      Console.WriteLine("-------");
      sSpecifier();
      Console.WriteLine("-------");
      ParseS();
      Console.WriteLine("-------");
      ParseSS();
      Console.WriteLine("-------");
      ssSpecifier();
      Console.WriteLine("-------");
   }

   private static void dSpecifier()
   {
      // <Snippet3>
      TimeSpan ts1 = new TimeSpan(16, 4, 3, 17, 250);
      Console.WriteLine(ts1.ToString("%d"));
      // Displays 16
      // </Snippet3>
      Console.WriteLine();

      // <Snippet4>
      TimeSpan ts2 = new TimeSpan(4, 3, 17);
      Console.WriteLine(ts2.ToString(@"d\.hh\:mm\:ss"));

      TimeSpan ts3 = new TimeSpan(3, 4, 3, 17);
      Console.WriteLine(ts3.ToString(@"d\.hh\:mm\:ss"));
      // The example displays the following output:
      //       0.04:03:17
      //       3.04:03:17
      // </Snippet4>
   }

   private static void ddSpecifier()
   {
      // <Snippet5>
      TimeSpan ts1 = new TimeSpan(0, 23, 17, 47);
      TimeSpan ts2 = new TimeSpan(365, 21, 19, 45);

      for (int ctr = 2; ctr <= 8; ctr++)
      {
         string fmt = new String('d', ctr) + @"\.hh\:mm\:ss";
         Console.WriteLine($"{fmt} --> {ts1.ToString(fmt)}");
         Console.WriteLine($"{fmt} --> {ts2.ToString(fmt)}");
         Console.WriteLine();
      }
      // The example displays the following output:
      //       dd\.hh\:mm\:ss --> 00.23:17:47
      //       dd\.hh\:mm\:ss --> 365.21:19:45
      //
      //       ddd\.hh\:mm\:ss --> 000.23:17:47
      //       ddd\.hh\:mm\:ss --> 365.21:19:45
      //
      //       dddd\.hh\:mm\:ss --> 0000.23:17:47
      //       dddd\.hh\:mm\:ss --> 0365.21:19:45
      //
      //       ddddd\.hh\:mm\:ss --> 00000.23:17:47
      //       ddddd\.hh\:mm\:ss --> 00365.21:19:45
      //
      //       dddddd\.hh\:mm\:ss --> 000000.23:17:47
      //       dddddd\.hh\:mm\:ss --> 000365.21:19:45
      //
      //       ddddddd\.hh\:mm\:ss --> 0000000.23:17:47
      //       ddddddd\.hh\:mm\:ss --> 0000365.21:19:45
      //
      //       dddddddd\.hh\:mm\:ss --> 00000000.23:17:47
      //       dddddddd\.hh\:mm\:ss --> 00000365.21:19:45
      // </Snippet5>
   }

   private static void hSpecifier()
   {
      // <Snippet6>
      TimeSpan ts = new TimeSpan(3, 42, 0);
      Console.WriteLine($"{ts:%h} hours {ts:%m} minutes");
      // The example displays the following output:
      //       3 hours 42 minutes
      // </Snippet6>

      // <Snippet7>
      TimeSpan ts1 = new TimeSpan(14, 3, 17);
      Console.WriteLine(ts1.ToString(@"d\.h\:mm\:ss"));

      TimeSpan ts2 = new TimeSpan(3, 4, 3, 17);
      Console.WriteLine(ts2.ToString(@"d\.h\:mm\:ss"));
      // The example displays the following output:
      //       0.14:03:17
      //       3.4:03:17
      // </Snippet7>
   }

   private static void ParseH()
   {
      // <Snippet8>
      string value = "8";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "%h", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       08:00:00
      // </Snippet8>
   }

   private static void ParseHH()
   {
      // <Snippet9>
      string value = "08";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "hh", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       08:00:00
      // </Snippet9>
   }

   private static void hhSpecifier()
   {
      // <Snippet10>
      TimeSpan ts1 = new TimeSpan(14, 3, 17);
      Console.WriteLine(ts1.ToString(@"d\.hh\:mm\:ss"));

      TimeSpan ts2 = new TimeSpan(3, 4, 3, 17);
      Console.WriteLine(ts2.ToString(@"d\.hh\:mm\:ss"));
      // The example displays the following output:
      //       0.14:03:17
      //       3.04:03:17
      // </Snippet10>
   }

   private static void ParseM()
   {
      // <Snippet11>
      string value = "3";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "%m", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       00:03:00
      // </Snippet11>
   }

   private static void mSpecifier()
   {
      // <Snippet12>
      TimeSpan ts1 = new TimeSpan(0, 6, 32);
      Console.WriteLine($"{ts1:m\\:ss} minutes");

      TimeSpan ts2 = new TimeSpan(3, 4, 3, 17);
      Console.WriteLine($"Elapsed time: {ts2:m\\:ss}");
      // The example displays the following output:
      //       6:32 minutes
      //       Elapsed time: 18:44
      // </Snippet12>
   }

   private static void ParseMM()
   {
      // <Snippet13>
      string value = "07";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "mm", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       00:07:00
      // </Snippet13>
   }

   private static void mmSpecifier()
   {
      // <Snippet14>
      TimeSpan departTime = new TimeSpan(11, 12, 00);
      TimeSpan arriveTime = new TimeSpan(16, 28, 00);
      Console.WriteLine($"Travel time: {arriveTime - departTime:hh\\:mm}");
      // The example displays the following output:
      //       Travel time: 05:16
      // </Snippet14>
   }

   private static void sSpecifier()
   {
      // <Snippet15>
      TimeSpan ts = TimeSpan.FromSeconds(12.465);
      Console.WriteLine(ts.ToString("%s"));
      // The example displays the following output:
      //       12
      // </Snippet15>
      Console.WriteLine();

      // <Snippet16>
      TimeSpan startTime = new TimeSpan(0, 12, 30, 15, 0);
      TimeSpan endTime = new TimeSpan(0, 12, 30, 21, 3);
      Console.WriteLine(@"Elapsed Time: {0:s\:fff} seconds",
                        endTime - startTime);
      // The example displays the following output:
      //       Elapsed Time: 6:003 seconds
      // </Snippet16>
   }

   private static void ParseS()
   {
      // <Snippet17>
      string value = "9";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "%s", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       00:00:09
      // </Snippet17>
   }

   private static void ParseSS()
   {
      // <Snippet18>
      string[] values = { "49", "9", "06" };
      TimeSpan interval;
      foreach (string value in values)
      {
         if (TimeSpan.TryParseExact(value, "ss", null, out interval))
            Console.WriteLine(interval.ToString("c"));
         else
            Console.WriteLine($"Unable to convert '{value}' to a time interval");
      }
      // The example displays the following output:
      //       00:00:49
      //       Unable to convert '9' to a time interval
      //       00:00:06
      // </Snippet18>
   }

   private static void ssSpecifier()
   {
      // <Snippet19>
      TimeSpan interval1 = TimeSpan.FromSeconds(12.60);
      Console.WriteLine(interval1.ToString(@"ss\.fff"));

      TimeSpan interval2 = TimeSpan.FromSeconds(6.485);
      Console.WriteLine(interval2.ToString(@"ss\.fff"));
      // The example displays the following output:
      //       12.600
      //       06.485
      // </Snippet19>
   }
}

sample · vbsamples/snippets/visualbasic/VS_Snippets_CLR/conceptual.timespan.custom/vb/customexamples1.vb5

vb
' Visual Basic .NET Document
Option Strict On

Module Example
    Public Sub Main()
        dSpecifier()
        Console.WriteLine("-------")
        ddSpecifier()
        Console.WriteLine("-------")
        hSpecifier()
        Console.WriteLine("-------")
        ParseH()
        Console.WriteLine("-------")
        ParseHH()
        Console.WriteLine("-------")
        hhSpecifier()
        Console.WriteLine("-------")
        ParseM()
        Console.WriteLine("-------")
        mSpecifier()
        Console.WriteLine("-------")
        ParseMM()
        Console.WriteLine("-------")
        mmSpecifier()
        Console.WriteLine("-------")
        sSpecifier()
        Console.WriteLine("-------")
        ParseS()
        Console.WriteLine("-------")
        ParseSS()
        Console.WriteLine("-------")
        ssSpecifier()
        Console.WriteLine("-------")
    End Sub

    Private Sub dSpecifier()
        ' <Snippet3>
        Dim ts As New TimeSpan(16, 4, 3, 17, 250)
        Console.WriteLine(ts.ToString("%d"))
        ' Displays 16   
        ' </Snippet3>
        Console.WriteLine()

        ' <Snippet4>
        Dim ts2 As New TimeSpan(4, 3, 17)
        Console.WriteLine(ts2.ToString("d\.hh\:mm\:ss"))

        Dim ts3 As New TimeSpan(3, 4, 3, 17)
        Console.WriteLine(ts3.ToString("d\.hh\:mm\:ss"))
        ' The example displays the following output:
        '       0.04:03:17
        '       3.04:03:17      
        ' </Snippet4>
    End Sub

    Private Sub ddSpecifier()
        ' <Snippet5>
        Dim ts1 As New TimeSpan(0, 23, 17, 47)
        Dim ts2 As New TimeSpan(365, 21, 19, 45)

        For ctr As Integer = 2 To 8
            Dim fmt As String = New String("d"c, ctr) + "\.hh\:mm\:ss"
            Console.WriteLine("{0} --> {1:" + fmt + "}", fmt, ts1)
            Console.WriteLine("{0} --> {1:" + fmt + "}", fmt, ts2)
            Console.WriteLine()
        Next
        ' The example displays the following output:
        '       dd\.hh\:mm\:ss --> 00.23:17:47
        '       dd\.hh\:mm\:ss --> 365.21:19:45
        '       
        '       ddd\.hh\:mm\:ss --> 000.23:17:47
        '       ddd\.hh\:mm\:ss --> 365.21:19:45
        '       
        '       dddd\.hh\:mm\:ss --> 0000.23:17:47
        '       dddd\.hh\:mm\:ss --> 0365.21:19:45
        '       
        '       ddddd\.hh\:mm\:ss --> 00000.23:17:47
        '       ddddd\.hh\:mm\:ss --> 00365.21:19:45
        '       
        '       dddddd\.hh\:mm\:ss --> 000000.23:17:47
        '       dddddd\.hh\:mm\:ss --> 000365.21:19:45
        '       
        '       ddddddd\.hh\:mm\:ss --> 0000000.23:17:47
        '       ddddddd\.hh\:mm\:ss --> 0000365.21:19:45
        '       
        '       dddddddd\.hh\:mm\:ss --> 00000000.23:17:47
        '       dddddddd\.hh\:mm\:ss --> 00000365.21:19:45      
        ' </Snippet5>
    End Sub

    Private Sub hSpecifier()
        ' <Snippet6>
        Dim ts As New TimeSpan(3, 42, 0)
        Console.WriteLine("{0:%h} hours {0:%m} minutes", ts)
        ' The example displays the following output:
        '       3 hours 42 minutes
        ' </Snippet6>
        Console.WriteLine()

        ' <Snippet7>
        Dim ts1 As New TimeSpan(14, 3, 17)
        Console.WriteLine(ts1.ToString("d\.h\:mm\:ss"))

        Dim ts2 As New TimeSpan(3, 4, 3, 17)
        Console.WriteLine(ts2.ToString("d\.h\:mm\:ss"))
        ' The example displays the following output:
        '       0.14:03:17
        '       3.4:03:17
        ' </Snippet7>
    End Sub

    Public Sub ParseH()
        ' <Snippet8>
        Dim value As String = "8"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "%h", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       08:00:00                              
        ' </Snippet8>
    End Sub

    Public Sub ParseHH()
        ' <Snippet9>
        Dim value As String = "08"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "hh", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       08:00:00                              
        ' </Snippet9>
    End Sub

    Private Sub hhSpecifier()
        ' <Snippet10>
        Dim ts1 As New TimeSpan(14, 3, 17)
        Console.WriteLine(ts1.ToString("d\.hh\:mm\:ss"))

        Dim ts2 As New TimeSpan(3, 4, 3, 17)
        Console.WriteLine(ts2.ToString("d\.hh\:mm\:ss"))
        ' The example displays the following output:
        '       0.14:03:17
        '       3.04:03:17
        ' </Snippet10>
    End Sub

    Public Sub ParseM()
        ' <Snippet11>
        Dim value As String = "3"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "%m", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       00:03:00                              
        ' </Snippet11>
    End Sub

    Private Sub mSpecifier()
        ' <Snippet12>
        Dim ts1 As New TimeSpan(0, 6, 32)
        Console.WriteLine("{0:m\:ss} minutes", ts1)

        Dim ts2 As New TimeSpan(0, 18, 44)
        Console.WriteLine("Elapsed time: {0:m\:ss}", ts2)
        ' The example displays the following output:
        '       6:32 minutes
        '       Elapsed time: 18:44
        ' </Snippet12>
    End Sub

    Public Sub ParseMM()
        ' <Snippet13>
        Dim value As String = "05"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "mm", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       00:05:00           
        ' </Snippet13>
    End Sub

    Private Sub mmSpecifier()
        ' <Snippet14>
        Dim departTime As New TimeSpan(11, 12, 00)
        Dim arriveTime As New TimeSpan(16, 28, 00)
        Console.WriteLine("Travel time: {0:hh\:mm}",
                          arriveTime - departTime)
        ' The example displays the following output:
        '       Travel time: 05:16      
        ' </Snippet14>
    End Sub

    Private Sub sSpecifier()
        ' <Snippet15>
        Dim ts As TimeSpan = TimeSpan.FromSeconds(12.465)
        Console.WriteLine(ts.ToString("%s"))
        ' The example displays the following output:
        '       12
        ' </Snippet15>
        Console.WriteLine()

        ' <Snippet16>
        Dim startTime As New TimeSpan(0, 12, 30, 15, 0)
        Dim endTime As New TimeSpan(0, 12, 30, 21, 3)
        Console.WriteLine("Elapsed Time: {0:s\:fff} seconds",
                          endTime - startTime)
        ' The example displays the following output:
        '       Elapsed Time: 6:003 seconds      
        ' </Snippet16>
    End Sub

    Public Sub ParseS()
        ' <Snippet17>
        Dim value As String = "9"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "%s", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       00:00:09
        ' </Snippet17>
    End Sub

    Public Sub ParseSS()
        ' <Snippet18>
        Dim values() As String = {"49", "9", "06"}
        Dim interval As TimeSpan
        For Each value As String In values
            If TimeSpan.TryParseExact(value, "ss", Nothing, interval) Then
                Console.WriteLine(interval.ToString("c"))
            Else
                Console.WriteLine("Unable to convert '{0}' to a time interval",
                                  value)
            End If
        Next
        ' The example displays the following output:
        '       00:00:49
        '       Unable to convert '9' to a time interval
        '       00:00:06
        ' </Snippet18>
    End Sub

    Private Sub ssSpecifier()
        ' <Snippet19>
        Dim interval1 As TimeSpan = TimeSpan.FromSeconds(12.60)
        Console.WriteLine(interval1.ToString("ss\.fff"))
        Dim interval2 As TimeSpan = TimeSpan.FromSeconds(6.485)
        Console.WriteLine(interval2.ToString("ss\.fff"))
        ' The example displays the following output:
        '       12.600
        '       06.485
        ' </Snippet19>
    End Sub
End Module

Back to table

The "h" custom format specifier

The "h" custom format specifier outputs the value of the System.TimeSpan.Hours property, which represents the number of whole hours in the time interval that isn't counted as part of its day component. It returns a one-digit string value if the value of the System.TimeSpan.Hours property is 0 through 9, and it returns a two-digit string value if the value of the System.TimeSpan.Hours property ranges from 10 to 23.

If the "h" custom format specifier is used alone, specify "%h" so that it isn't misinterpreted as a standard format string. The following example provides an illustration.

sample · csharpsamples/snippets/csharp/VS_Snippets_CLR/conceptual.timespan.custom/cs/customexamples1.cs6

csharp
using System;

public class Example
{
   public static void Main()
   {
      dSpecifier();
      Console.WriteLine("-------");
      ddSpecifier();
      Console.WriteLine("-------");
      hSpecifier();
      Console.WriteLine("-------");
      ParseH();
      Console.WriteLine("-------");
      ParseHH();
      Console.WriteLine("-------");
      hhSpecifier();
      Console.WriteLine("-------");
      ParseM();
      Console.WriteLine("-------");
      mSpecifier();
      Console.WriteLine("-------");
      ParseMM();
      Console.WriteLine("-------");
      mmSpecifier();
      Console.WriteLine("-------");
      sSpecifier();
      Console.WriteLine("-------");
      ParseS();
      Console.WriteLine("-------");
      ParseSS();
      Console.WriteLine("-------");
      ssSpecifier();
      Console.WriteLine("-------");
   }

   private static void dSpecifier()
   {
      // <Snippet3>
      TimeSpan ts1 = new TimeSpan(16, 4, 3, 17, 250);
      Console.WriteLine(ts1.ToString("%d"));
      // Displays 16
      // </Snippet3>
      Console.WriteLine();

      // <Snippet4>
      TimeSpan ts2 = new TimeSpan(4, 3, 17);
      Console.WriteLine(ts2.ToString(@"d\.hh\:mm\:ss"));

      TimeSpan ts3 = new TimeSpan(3, 4, 3, 17);
      Console.WriteLine(ts3.ToString(@"d\.hh\:mm\:ss"));
      // The example displays the following output:
      //       0.04:03:17
      //       3.04:03:17
      // </Snippet4>
   }

   private static void ddSpecifier()
   {
      // <Snippet5>
      TimeSpan ts1 = new TimeSpan(0, 23, 17, 47);
      TimeSpan ts2 = new TimeSpan(365, 21, 19, 45);

      for (int ctr = 2; ctr <= 8; ctr++)
      {
         string fmt = new String('d', ctr) + @"\.hh\:mm\:ss";
         Console.WriteLine($"{fmt} --> {ts1.ToString(fmt)}");
         Console.WriteLine($"{fmt} --> {ts2.ToString(fmt)}");
         Console.WriteLine();
      }
      // The example displays the following output:
      //       dd\.hh\:mm\:ss --> 00.23:17:47
      //       dd\.hh\:mm\:ss --> 365.21:19:45
      //
      //       ddd\.hh\:mm\:ss --> 000.23:17:47
      //       ddd\.hh\:mm\:ss --> 365.21:19:45
      //
      //       dddd\.hh\:mm\:ss --> 0000.23:17:47
      //       dddd\.hh\:mm\:ss --> 0365.21:19:45
      //
      //       ddddd\.hh\:mm\:ss --> 00000.23:17:47
      //       ddddd\.hh\:mm\:ss --> 00365.21:19:45
      //
      //       dddddd\.hh\:mm\:ss --> 000000.23:17:47
      //       dddddd\.hh\:mm\:ss --> 000365.21:19:45
      //
      //       ddddddd\.hh\:mm\:ss --> 0000000.23:17:47
      //       ddddddd\.hh\:mm\:ss --> 0000365.21:19:45
      //
      //       dddddddd\.hh\:mm\:ss --> 00000000.23:17:47
      //       dddddddd\.hh\:mm\:ss --> 00000365.21:19:45
      // </Snippet5>
   }

   private static void hSpecifier()
   {
      // <Snippet6>
      TimeSpan ts = new TimeSpan(3, 42, 0);
      Console.WriteLine($"{ts:%h} hours {ts:%m} minutes");
      // The example displays the following output:
      //       3 hours 42 minutes
      // </Snippet6>

      // <Snippet7>
      TimeSpan ts1 = new TimeSpan(14, 3, 17);
      Console.WriteLine(ts1.ToString(@"d\.h\:mm\:ss"));

      TimeSpan ts2 = new TimeSpan(3, 4, 3, 17);
      Console.WriteLine(ts2.ToString(@"d\.h\:mm\:ss"));
      // The example displays the following output:
      //       0.14:03:17
      //       3.4:03:17
      // </Snippet7>
   }

   private static void ParseH()
   {
      // <Snippet8>
      string value = "8";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "%h", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       08:00:00
      // </Snippet8>
   }

   private static void ParseHH()
   {
      // <Snippet9>
      string value = "08";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "hh", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       08:00:00
      // </Snippet9>
   }

   private static void hhSpecifier()
   {
      // <Snippet10>
      TimeSpan ts1 = new TimeSpan(14, 3, 17);
      Console.WriteLine(ts1.ToString(@"d\.hh\:mm\:ss"));

      TimeSpan ts2 = new TimeSpan(3, 4, 3, 17);
      Console.WriteLine(ts2.ToString(@"d\.hh\:mm\:ss"));
      // The example displays the following output:
      //       0.14:03:17
      //       3.04:03:17
      // </Snippet10>
   }

   private static void ParseM()
   {
      // <Snippet11>
      string value = "3";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "%m", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       00:03:00
      // </Snippet11>
   }

   private static void mSpecifier()
   {
      // <Snippet12>
      TimeSpan ts1 = new TimeSpan(0, 6, 32);
      Console.WriteLine($"{ts1:m\\:ss} minutes");

      TimeSpan ts2 = new TimeSpan(3, 4, 3, 17);
      Console.WriteLine($"Elapsed time: {ts2:m\\:ss}");
      // The example displays the following output:
      //       6:32 minutes
      //       Elapsed time: 18:44
      // </Snippet12>
   }

   private static void ParseMM()
   {
      // <Snippet13>
      string value = "07";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "mm", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       00:07:00
      // </Snippet13>
   }

   private static void mmSpecifier()
   {
      // <Snippet14>
      TimeSpan departTime = new TimeSpan(11, 12, 00);
      TimeSpan arriveTime = new TimeSpan(16, 28, 00);
      Console.WriteLine($"Travel time: {arriveTime - departTime:hh\\:mm}");
      // The example displays the following output:
      //       Travel time: 05:16
      // </Snippet14>
   }

   private static void sSpecifier()
   {
      // <Snippet15>
      TimeSpan ts = TimeSpan.FromSeconds(12.465);
      Console.WriteLine(ts.ToString("%s"));
      // The example displays the following output:
      //       12
      // </Snippet15>
      Console.WriteLine();

      // <Snippet16>
      TimeSpan startTime = new TimeSpan(0, 12, 30, 15, 0);
      TimeSpan endTime = new TimeSpan(0, 12, 30, 21, 3);
      Console.WriteLine(@"Elapsed Time: {0:s\:fff} seconds",
                        endTime - startTime);
      // The example displays the following output:
      //       Elapsed Time: 6:003 seconds
      // </Snippet16>
   }

   private static void ParseS()
   {
      // <Snippet17>
      string value = "9";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "%s", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       00:00:09
      // </Snippet17>
   }

   private static void ParseSS()
   {
      // <Snippet18>
      string[] values = { "49", "9", "06" };
      TimeSpan interval;
      foreach (string value in values)
      {
         if (TimeSpan.TryParseExact(value, "ss", null, out interval))
            Console.WriteLine(interval.ToString("c"));
         else
            Console.WriteLine($"Unable to convert '{value}' to a time interval");
      }
      // The example displays the following output:
      //       00:00:49
      //       Unable to convert '9' to a time interval
      //       00:00:06
      // </Snippet18>
   }

   private static void ssSpecifier()
   {
      // <Snippet19>
      TimeSpan interval1 = TimeSpan.FromSeconds(12.60);
      Console.WriteLine(interval1.ToString(@"ss\.fff"));

      TimeSpan interval2 = TimeSpan.FromSeconds(6.485);
      Console.WriteLine(interval2.ToString(@"ss\.fff"));
      // The example displays the following output:
      //       12.600
      //       06.485
      // </Snippet19>
   }
}

sample · vbsamples/snippets/visualbasic/VS_Snippets_CLR/conceptual.timespan.custom/vb/customexamples1.vb6

vb
' Visual Basic .NET Document
Option Strict On

Module Example
    Public Sub Main()
        dSpecifier()
        Console.WriteLine("-------")
        ddSpecifier()
        Console.WriteLine("-------")
        hSpecifier()
        Console.WriteLine("-------")
        ParseH()
        Console.WriteLine("-------")
        ParseHH()
        Console.WriteLine("-------")
        hhSpecifier()
        Console.WriteLine("-------")
        ParseM()
        Console.WriteLine("-------")
        mSpecifier()
        Console.WriteLine("-------")
        ParseMM()
        Console.WriteLine("-------")
        mmSpecifier()
        Console.WriteLine("-------")
        sSpecifier()
        Console.WriteLine("-------")
        ParseS()
        Console.WriteLine("-------")
        ParseSS()
        Console.WriteLine("-------")
        ssSpecifier()
        Console.WriteLine("-------")
    End Sub

    Private Sub dSpecifier()
        ' <Snippet3>
        Dim ts As New TimeSpan(16, 4, 3, 17, 250)
        Console.WriteLine(ts.ToString("%d"))
        ' Displays 16   
        ' </Snippet3>
        Console.WriteLine()

        ' <Snippet4>
        Dim ts2 As New TimeSpan(4, 3, 17)
        Console.WriteLine(ts2.ToString("d\.hh\:mm\:ss"))

        Dim ts3 As New TimeSpan(3, 4, 3, 17)
        Console.WriteLine(ts3.ToString("d\.hh\:mm\:ss"))
        ' The example displays the following output:
        '       0.04:03:17
        '       3.04:03:17      
        ' </Snippet4>
    End Sub

    Private Sub ddSpecifier()
        ' <Snippet5>
        Dim ts1 As New TimeSpan(0, 23, 17, 47)
        Dim ts2 As New TimeSpan(365, 21, 19, 45)

        For ctr As Integer = 2 To 8
            Dim fmt As String = New String("d"c, ctr) + "\.hh\:mm\:ss"
            Console.WriteLine("{0} --> {1:" + fmt + "}", fmt, ts1)
            Console.WriteLine("{0} --> {1:" + fmt + "}", fmt, ts2)
            Console.WriteLine()
        Next
        ' The example displays the following output:
        '       dd\.hh\:mm\:ss --> 00.23:17:47
        '       dd\.hh\:mm\:ss --> 365.21:19:45
        '       
        '       ddd\.hh\:mm\:ss --> 000.23:17:47
        '       ddd\.hh\:mm\:ss --> 365.21:19:45
        '       
        '       dddd\.hh\:mm\:ss --> 0000.23:17:47
        '       dddd\.hh\:mm\:ss --> 0365.21:19:45
        '       
        '       ddddd\.hh\:mm\:ss --> 00000.23:17:47
        '       ddddd\.hh\:mm\:ss --> 00365.21:19:45
        '       
        '       dddddd\.hh\:mm\:ss --> 000000.23:17:47
        '       dddddd\.hh\:mm\:ss --> 000365.21:19:45
        '       
        '       ddddddd\.hh\:mm\:ss --> 0000000.23:17:47
        '       ddddddd\.hh\:mm\:ss --> 0000365.21:19:45
        '       
        '       dddddddd\.hh\:mm\:ss --> 00000000.23:17:47
        '       dddddddd\.hh\:mm\:ss --> 00000365.21:19:45      
        ' </Snippet5>
    End Sub

    Private Sub hSpecifier()
        ' <Snippet6>
        Dim ts As New TimeSpan(3, 42, 0)
        Console.WriteLine("{0:%h} hours {0:%m} minutes", ts)
        ' The example displays the following output:
        '       3 hours 42 minutes
        ' </Snippet6>
        Console.WriteLine()

        ' <Snippet7>
        Dim ts1 As New TimeSpan(14, 3, 17)
        Console.WriteLine(ts1.ToString("d\.h\:mm\:ss"))

        Dim ts2 As New TimeSpan(3, 4, 3, 17)
        Console.WriteLine(ts2.ToString("d\.h\:mm\:ss"))
        ' The example displays the following output:
        '       0.14:03:17
        '       3.4:03:17
        ' </Snippet7>
    End Sub

    Public Sub ParseH()
        ' <Snippet8>
        Dim value As String = "8"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "%h", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       08:00:00                              
        ' </Snippet8>
    End Sub

    Public Sub ParseHH()
        ' <Snippet9>
        Dim value As String = "08"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "hh", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       08:00:00                              
        ' </Snippet9>
    End Sub

    Private Sub hhSpecifier()
        ' <Snippet10>
        Dim ts1 As New TimeSpan(14, 3, 17)
        Console.WriteLine(ts1.ToString("d\.hh\:mm\:ss"))

        Dim ts2 As New TimeSpan(3, 4, 3, 17)
        Console.WriteLine(ts2.ToString("d\.hh\:mm\:ss"))
        ' The example displays the following output:
        '       0.14:03:17
        '       3.04:03:17
        ' </Snippet10>
    End Sub

    Public Sub ParseM()
        ' <Snippet11>
        Dim value As String = "3"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "%m", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       00:03:00                              
        ' </Snippet11>
    End Sub

    Private Sub mSpecifier()
        ' <Snippet12>
        Dim ts1 As New TimeSpan(0, 6, 32)
        Console.WriteLine("{0:m\:ss} minutes", ts1)

        Dim ts2 As New TimeSpan(0, 18, 44)
        Console.WriteLine("Elapsed time: {0:m\:ss}", ts2)
        ' The example displays the following output:
        '       6:32 minutes
        '       Elapsed time: 18:44
        ' </Snippet12>
    End Sub

    Public Sub ParseMM()
        ' <Snippet13>
        Dim value As String = "05"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "mm", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       00:05:00           
        ' </Snippet13>
    End Sub

    Private Sub mmSpecifier()
        ' <Snippet14>
        Dim departTime As New TimeSpan(11, 12, 00)
        Dim arriveTime As New TimeSpan(16, 28, 00)
        Console.WriteLine("Travel time: {0:hh\:mm}",
                          arriveTime - departTime)
        ' The example displays the following output:
        '       Travel time: 05:16      
        ' </Snippet14>
    End Sub

    Private Sub sSpecifier()
        ' <Snippet15>
        Dim ts As TimeSpan = TimeSpan.FromSeconds(12.465)
        Console.WriteLine(ts.ToString("%s"))
        ' The example displays the following output:
        '       12
        ' </Snippet15>
        Console.WriteLine()

        ' <Snippet16>
        Dim startTime As New TimeSpan(0, 12, 30, 15, 0)
        Dim endTime As New TimeSpan(0, 12, 30, 21, 3)
        Console.WriteLine("Elapsed Time: {0:s\:fff} seconds",
                          endTime - startTime)
        ' The example displays the following output:
        '       Elapsed Time: 6:003 seconds      
        ' </Snippet16>
    End Sub

    Public Sub ParseS()
        ' <Snippet17>
        Dim value As String = "9"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "%s", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       00:00:09
        ' </Snippet17>
    End Sub

    Public Sub ParseSS()
        ' <Snippet18>
        Dim values() As String = {"49", "9", "06"}
        Dim interval As TimeSpan
        For Each value As String In values
            If TimeSpan.TryParseExact(value, "ss", Nothing, interval) Then
                Console.WriteLine(interval.ToString("c"))
            Else
                Console.WriteLine("Unable to convert '{0}' to a time interval",
                                  value)
            End If
        Next
        ' The example displays the following output:
        '       00:00:49
        '       Unable to convert '9' to a time interval
        '       00:00:06
        ' </Snippet18>
    End Sub

    Private Sub ssSpecifier()
        ' <Snippet19>
        Dim interval1 As TimeSpan = TimeSpan.FromSeconds(12.60)
        Console.WriteLine(interval1.ToString("ss\.fff"))
        Dim interval2 As TimeSpan = TimeSpan.FromSeconds(6.485)
        Console.WriteLine(interval2.ToString("ss\.fff"))
        ' The example displays the following output:
        '       12.600
        '       06.485
        ' </Snippet19>
    End Sub
End Module

Ordinarily, in a parsing operation, an input string that includes only a single number is interpreted as the number of days. You can use the "%h" custom format specifier instead to interpret the numeric string as the number of hours. The following example provides an illustration.

sample · csharpsamples/snippets/csharp/VS_Snippets_CLR/conceptual.timespan.custom/cs/customexamples1.cs8

csharp
using System;

public class Example
{
   public static void Main()
   {
      dSpecifier();
      Console.WriteLine("-------");
      ddSpecifier();
      Console.WriteLine("-------");
      hSpecifier();
      Console.WriteLine("-------");
      ParseH();
      Console.WriteLine("-------");
      ParseHH();
      Console.WriteLine("-------");
      hhSpecifier();
      Console.WriteLine("-------");
      ParseM();
      Console.WriteLine("-------");
      mSpecifier();
      Console.WriteLine("-------");
      ParseMM();
      Console.WriteLine("-------");
      mmSpecifier();
      Console.WriteLine("-------");
      sSpecifier();
      Console.WriteLine("-------");
      ParseS();
      Console.WriteLine("-------");
      ParseSS();
      Console.WriteLine("-------");
      ssSpecifier();
      Console.WriteLine("-------");
   }

   private static void dSpecifier()
   {
      // <Snippet3>
      TimeSpan ts1 = new TimeSpan(16, 4, 3, 17, 250);
      Console.WriteLine(ts1.ToString("%d"));
      // Displays 16
      // </Snippet3>
      Console.WriteLine();

      // <Snippet4>
      TimeSpan ts2 = new TimeSpan(4, 3, 17);
      Console.WriteLine(ts2.ToString(@"d\.hh\:mm\:ss"));

      TimeSpan ts3 = new TimeSpan(3, 4, 3, 17);
      Console.WriteLine(ts3.ToString(@"d\.hh\:mm\:ss"));
      // The example displays the following output:
      //       0.04:03:17
      //       3.04:03:17
      // </Snippet4>
   }

   private static void ddSpecifier()
   {
      // <Snippet5>
      TimeSpan ts1 = new TimeSpan(0, 23, 17, 47);
      TimeSpan ts2 = new TimeSpan(365, 21, 19, 45);

      for (int ctr = 2; ctr <= 8; ctr++)
      {
         string fmt = new String('d', ctr) + @"\.hh\:mm\:ss";
         Console.WriteLine($"{fmt} --> {ts1.ToString(fmt)}");
         Console.WriteLine($"{fmt} --> {ts2.ToString(fmt)}");
         Console.WriteLine();
      }
      // The example displays the following output:
      //       dd\.hh\:mm\:ss --> 00.23:17:47
      //       dd\.hh\:mm\:ss --> 365.21:19:45
      //
      //       ddd\.hh\:mm\:ss --> 000.23:17:47
      //       ddd\.hh\:mm\:ss --> 365.21:19:45
      //
      //       dddd\.hh\:mm\:ss --> 0000.23:17:47
      //       dddd\.hh\:mm\:ss --> 0365.21:19:45
      //
      //       ddddd\.hh\:mm\:ss --> 00000.23:17:47
      //       ddddd\.hh\:mm\:ss --> 00365.21:19:45
      //
      //       dddddd\.hh\:mm\:ss --> 000000.23:17:47
      //       dddddd\.hh\:mm\:ss --> 000365.21:19:45
      //
      //       ddddddd\.hh\:mm\:ss --> 0000000.23:17:47
      //       ddddddd\.hh\:mm\:ss --> 0000365.21:19:45
      //
      //       dddddddd\.hh\:mm\:ss --> 00000000.23:17:47
      //       dddddddd\.hh\:mm\:ss --> 00000365.21:19:45
      // </Snippet5>
   }

   private static void hSpecifier()
   {
      // <Snippet6>
      TimeSpan ts = new TimeSpan(3, 42, 0);
      Console.WriteLine($"{ts:%h} hours {ts:%m} minutes");
      // The example displays the following output:
      //       3 hours 42 minutes
      // </Snippet6>

      // <Snippet7>
      TimeSpan ts1 = new TimeSpan(14, 3, 17);
      Console.WriteLine(ts1.ToString(@"d\.h\:mm\:ss"));

      TimeSpan ts2 = new TimeSpan(3, 4, 3, 17);
      Console.WriteLine(ts2.ToString(@"d\.h\:mm\:ss"));
      // The example displays the following output:
      //       0.14:03:17
      //       3.4:03:17
      // </Snippet7>
   }

   private static void ParseH()
   {
      // <Snippet8>
      string value = "8";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "%h", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       08:00:00
      // </Snippet8>
   }

   private static void ParseHH()
   {
      // <Snippet9>
      string value = "08";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "hh", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       08:00:00
      // </Snippet9>
   }

   private static void hhSpecifier()
   {
      // <Snippet10>
      TimeSpan ts1 = new TimeSpan(14, 3, 17);
      Console.WriteLine(ts1.ToString(@"d\.hh\:mm\:ss"));

      TimeSpan ts2 = new TimeSpan(3, 4, 3, 17);
      Console.WriteLine(ts2.ToString(@"d\.hh\:mm\:ss"));
      // The example displays the following output:
      //       0.14:03:17
      //       3.04:03:17
      // </Snippet10>
   }

   private static void ParseM()
   {
      // <Snippet11>
      string value = "3";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "%m", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       00:03:00
      // </Snippet11>
   }

   private static void mSpecifier()
   {
      // <Snippet12>
      TimeSpan ts1 = new TimeSpan(0, 6, 32);
      Console.WriteLine($"{ts1:m\\:ss} minutes");

      TimeSpan ts2 = new TimeSpan(3, 4, 3, 17);
      Console.WriteLine($"Elapsed time: {ts2:m\\:ss}");
      // The example displays the following output:
      //       6:32 minutes
      //       Elapsed time: 18:44
      // </Snippet12>
   }

   private static void ParseMM()
   {
      // <Snippet13>
      string value = "07";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "mm", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       00:07:00
      // </Snippet13>
   }

   private static void mmSpecifier()
   {
      // <Snippet14>
      TimeSpan departTime = new TimeSpan(11, 12, 00);
      TimeSpan arriveTime = new TimeSpan(16, 28, 00);
      Console.WriteLine($"Travel time: {arriveTime - departTime:hh\\:mm}");
      // The example displays the following output:
      //       Travel time: 05:16
      // </Snippet14>
   }

   private static void sSpecifier()
   {
      // <Snippet15>
      TimeSpan ts = TimeSpan.FromSeconds(12.465);
      Console.WriteLine(ts.ToString("%s"));
      // The example displays the following output:
      //       12
      // </Snippet15>
      Console.WriteLine();

      // <Snippet16>
      TimeSpan startTime = new TimeSpan(0, 12, 30, 15, 0);
      TimeSpan endTime = new TimeSpan(0, 12, 30, 21, 3);
      Console.WriteLine(@"Elapsed Time: {0:s\:fff} seconds",
                        endTime - startTime);
      // The example displays the following output:
      //       Elapsed Time: 6:003 seconds
      // </Snippet16>
   }

   private static void ParseS()
   {
      // <Snippet17>
      string value = "9";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "%s", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       00:00:09
      // </Snippet17>
   }

   private static void ParseSS()
   {
      // <Snippet18>
      string[] values = { "49", "9", "06" };
      TimeSpan interval;
      foreach (string value in values)
      {
         if (TimeSpan.TryParseExact(value, "ss", null, out interval))
            Console.WriteLine(interval.ToString("c"));
         else
            Console.WriteLine($"Unable to convert '{value}' to a time interval");
      }
      // The example displays the following output:
      //       00:00:49
      //       Unable to convert '9' to a time interval
      //       00:00:06
      // </Snippet18>
   }

   private static void ssSpecifier()
   {
      // <Snippet19>
      TimeSpan interval1 = TimeSpan.FromSeconds(12.60);
      Console.WriteLine(interval1.ToString(@"ss\.fff"));

      TimeSpan interval2 = TimeSpan.FromSeconds(6.485);
      Console.WriteLine(interval2.ToString(@"ss\.fff"));
      // The example displays the following output:
      //       12.600
      //       06.485
      // </Snippet19>
   }
}

sample · vbsamples/snippets/visualbasic/VS_Snippets_CLR/conceptual.timespan.custom/vb/customexamples1.vb8

vb
' Visual Basic .NET Document
Option Strict On

Module Example
    Public Sub Main()
        dSpecifier()
        Console.WriteLine("-------")
        ddSpecifier()
        Console.WriteLine("-------")
        hSpecifier()
        Console.WriteLine("-------")
        ParseH()
        Console.WriteLine("-------")
        ParseHH()
        Console.WriteLine("-------")
        hhSpecifier()
        Console.WriteLine("-------")
        ParseM()
        Console.WriteLine("-------")
        mSpecifier()
        Console.WriteLine("-------")
        ParseMM()
        Console.WriteLine("-------")
        mmSpecifier()
        Console.WriteLine("-------")
        sSpecifier()
        Console.WriteLine("-------")
        ParseS()
        Console.WriteLine("-------")
        ParseSS()
        Console.WriteLine("-------")
        ssSpecifier()
        Console.WriteLine("-------")
    End Sub

    Private Sub dSpecifier()
        ' <Snippet3>
        Dim ts As New TimeSpan(16, 4, 3, 17, 250)
        Console.WriteLine(ts.ToString("%d"))
        ' Displays 16   
        ' </Snippet3>
        Console.WriteLine()

        ' <Snippet4>
        Dim ts2 As New TimeSpan(4, 3, 17)
        Console.WriteLine(ts2.ToString("d\.hh\:mm\:ss"))

        Dim ts3 As New TimeSpan(3, 4, 3, 17)
        Console.WriteLine(ts3.ToString("d\.hh\:mm\:ss"))
        ' The example displays the following output:
        '       0.04:03:17
        '       3.04:03:17      
        ' </Snippet4>
    End Sub

    Private Sub ddSpecifier()
        ' <Snippet5>
        Dim ts1 As New TimeSpan(0, 23, 17, 47)
        Dim ts2 As New TimeSpan(365, 21, 19, 45)

        For ctr As Integer = 2 To 8
            Dim fmt As String = New String("d"c, ctr) + "\.hh\:mm\:ss"
            Console.WriteLine("{0} --> {1:" + fmt + "}", fmt, ts1)
            Console.WriteLine("{0} --> {1:" + fmt + "}", fmt, ts2)
            Console.WriteLine()
        Next
        ' The example displays the following output:
        '       dd\.hh\:mm\:ss --> 00.23:17:47
        '       dd\.hh\:mm\:ss --> 365.21:19:45
        '       
        '       ddd\.hh\:mm\:ss --> 000.23:17:47
        '       ddd\.hh\:mm\:ss --> 365.21:19:45
        '       
        '       dddd\.hh\:mm\:ss --> 0000.23:17:47
        '       dddd\.hh\:mm\:ss --> 0365.21:19:45
        '       
        '       ddddd\.hh\:mm\:ss --> 00000.23:17:47
        '       ddddd\.hh\:mm\:ss --> 00365.21:19:45
        '       
        '       dddddd\.hh\:mm\:ss --> 000000.23:17:47
        '       dddddd\.hh\:mm\:ss --> 000365.21:19:45
        '       
        '       ddddddd\.hh\:mm\:ss --> 0000000.23:17:47
        '       ddddddd\.hh\:mm\:ss --> 0000365.21:19:45
        '       
        '       dddddddd\.hh\:mm\:ss --> 00000000.23:17:47
        '       dddddddd\.hh\:mm\:ss --> 00000365.21:19:45      
        ' </Snippet5>
    End Sub

    Private Sub hSpecifier()
        ' <Snippet6>
        Dim ts As New TimeSpan(3, 42, 0)
        Console.WriteLine("{0:%h} hours {0:%m} minutes", ts)
        ' The example displays the following output:
        '       3 hours 42 minutes
        ' </Snippet6>
        Console.WriteLine()

        ' <Snippet7>
        Dim ts1 As New TimeSpan(14, 3, 17)
        Console.WriteLine(ts1.ToString("d\.h\:mm\:ss"))

        Dim ts2 As New TimeSpan(3, 4, 3, 17)
        Console.WriteLine(ts2.ToString("d\.h\:mm\:ss"))
        ' The example displays the following output:
        '       0.14:03:17
        '       3.4:03:17
        ' </Snippet7>
    End Sub

    Public Sub ParseH()
        ' <Snippet8>
        Dim value As String = "8"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "%h", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       08:00:00                              
        ' </Snippet8>
    End Sub

    Public Sub ParseHH()
        ' <Snippet9>
        Dim value As String = "08"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "hh", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       08:00:00                              
        ' </Snippet9>
    End Sub

    Private Sub hhSpecifier()
        ' <Snippet10>
        Dim ts1 As New TimeSpan(14, 3, 17)
        Console.WriteLine(ts1.ToString("d\.hh\:mm\:ss"))

        Dim ts2 As New TimeSpan(3, 4, 3, 17)
        Console.WriteLine(ts2.ToString("d\.hh\:mm\:ss"))
        ' The example displays the following output:
        '       0.14:03:17
        '       3.04:03:17
        ' </Snippet10>
    End Sub

    Public Sub ParseM()
        ' <Snippet11>
        Dim value As String = "3"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "%m", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       00:03:00                              
        ' </Snippet11>
    End Sub

    Private Sub mSpecifier()
        ' <Snippet12>
        Dim ts1 As New TimeSpan(0, 6, 32)
        Console.WriteLine("{0:m\:ss} minutes", ts1)

        Dim ts2 As New TimeSpan(0, 18, 44)
        Console.WriteLine("Elapsed time: {0:m\:ss}", ts2)
        ' The example displays the following output:
        '       6:32 minutes
        '       Elapsed time: 18:44
        ' </Snippet12>
    End Sub

    Public Sub ParseMM()
        ' <Snippet13>
        Dim value As String = "05"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "mm", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       00:05:00           
        ' </Snippet13>
    End Sub

    Private Sub mmSpecifier()
        ' <Snippet14>
        Dim departTime As New TimeSpan(11, 12, 00)
        Dim arriveTime As New TimeSpan(16, 28, 00)
        Console.WriteLine("Travel time: {0:hh\:mm}",
                          arriveTime - departTime)
        ' The example displays the following output:
        '       Travel time: 05:16      
        ' </Snippet14>
    End Sub

    Private Sub sSpecifier()
        ' <Snippet15>
        Dim ts As TimeSpan = TimeSpan.FromSeconds(12.465)
        Console.WriteLine(ts.ToString("%s"))
        ' The example displays the following output:
        '       12
        ' </Snippet15>
        Console.WriteLine()

        ' <Snippet16>
        Dim startTime As New TimeSpan(0, 12, 30, 15, 0)
        Dim endTime As New TimeSpan(0, 12, 30, 21, 3)
        Console.WriteLine("Elapsed Time: {0:s\:fff} seconds",
                          endTime - startTime)
        ' The example displays the following output:
        '       Elapsed Time: 6:003 seconds      
        ' </Snippet16>
    End Sub

    Public Sub ParseS()
        ' <Snippet17>
        Dim value As String = "9"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "%s", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       00:00:09
        ' </Snippet17>
    End Sub

    Public Sub ParseSS()
        ' <Snippet18>
        Dim values() As String = {"49", "9", "06"}
        Dim interval As TimeSpan
        For Each value As String In values
            If TimeSpan.TryParseExact(value, "ss", Nothing, interval) Then
                Console.WriteLine(interval.ToString("c"))
            Else
                Console.WriteLine("Unable to convert '{0}' to a time interval",
                                  value)
            End If
        Next
        ' The example displays the following output:
        '       00:00:49
        '       Unable to convert '9' to a time interval
        '       00:00:06
        ' </Snippet18>
    End Sub

    Private Sub ssSpecifier()
        ' <Snippet19>
        Dim interval1 As TimeSpan = TimeSpan.FromSeconds(12.60)
        Console.WriteLine(interval1.ToString("ss\.fff"))
        Dim interval2 As TimeSpan = TimeSpan.FromSeconds(6.485)
        Console.WriteLine(interval2.ToString("ss\.fff"))
        ' The example displays the following output:
        '       12.600
        '       06.485
        ' </Snippet19>
    End Sub
End Module

The following example illustrates the use of the "h" custom format specifier.

sample · csharpsamples/snippets/csharp/VS_Snippets_CLR/conceptual.timespan.custom/cs/customexamples1.cs7

csharp
using System;

public class Example
{
   public static void Main()
   {
      dSpecifier();
      Console.WriteLine("-------");
      ddSpecifier();
      Console.WriteLine("-------");
      hSpecifier();
      Console.WriteLine("-------");
      ParseH();
      Console.WriteLine("-------");
      ParseHH();
      Console.WriteLine("-------");
      hhSpecifier();
      Console.WriteLine("-------");
      ParseM();
      Console.WriteLine("-------");
      mSpecifier();
      Console.WriteLine("-------");
      ParseMM();
      Console.WriteLine("-------");
      mmSpecifier();
      Console.WriteLine("-------");
      sSpecifier();
      Console.WriteLine("-------");
      ParseS();
      Console.WriteLine("-------");
      ParseSS();
      Console.WriteLine("-------");
      ssSpecifier();
      Console.WriteLine("-------");
   }

   private static void dSpecifier()
   {
      // <Snippet3>
      TimeSpan ts1 = new TimeSpan(16, 4, 3, 17, 250);
      Console.WriteLine(ts1.ToString("%d"));
      // Displays 16
      // </Snippet3>
      Console.WriteLine();

      // <Snippet4>
      TimeSpan ts2 = new TimeSpan(4, 3, 17);
      Console.WriteLine(ts2.ToString(@"d\.hh\:mm\:ss"));

      TimeSpan ts3 = new TimeSpan(3, 4, 3, 17);
      Console.WriteLine(ts3.ToString(@"d\.hh\:mm\:ss"));
      // The example displays the following output:
      //       0.04:03:17
      //       3.04:03:17
      // </Snippet4>
   }

   private static void ddSpecifier()
   {
      // <Snippet5>
      TimeSpan ts1 = new TimeSpan(0, 23, 17, 47);
      TimeSpan ts2 = new TimeSpan(365, 21, 19, 45);

      for (int ctr = 2; ctr <= 8; ctr++)
      {
         string fmt = new String('d', ctr) + @"\.hh\:mm\:ss";
         Console.WriteLine($"{fmt} --> {ts1.ToString(fmt)}");
         Console.WriteLine($"{fmt} --> {ts2.ToString(fmt)}");
         Console.WriteLine();
      }
      // The example displays the following output:
      //       dd\.hh\:mm\:ss --> 00.23:17:47
      //       dd\.hh\:mm\:ss --> 365.21:19:45
      //
      //       ddd\.hh\:mm\:ss --> 000.23:17:47
      //       ddd\.hh\:mm\:ss --> 365.21:19:45
      //
      //       dddd\.hh\:mm\:ss --> 0000.23:17:47
      //       dddd\.hh\:mm\:ss --> 0365.21:19:45
      //
      //       ddddd\.hh\:mm\:ss --> 00000.23:17:47
      //       ddddd\.hh\:mm\:ss --> 00365.21:19:45
      //
      //       dddddd\.hh\:mm\:ss --> 000000.23:17:47
      //       dddddd\.hh\:mm\:ss --> 000365.21:19:45
      //
      //       ddddddd\.hh\:mm\:ss --> 0000000.23:17:47
      //       ddddddd\.hh\:mm\:ss --> 0000365.21:19:45
      //
      //       dddddddd\.hh\:mm\:ss --> 00000000.23:17:47
      //       dddddddd\.hh\:mm\:ss --> 00000365.21:19:45
      // </Snippet5>
   }

   private static void hSpecifier()
   {
      // <Snippet6>
      TimeSpan ts = new TimeSpan(3, 42, 0);
      Console.WriteLine($"{ts:%h} hours {ts:%m} minutes");
      // The example displays the following output:
      //       3 hours 42 minutes
      // </Snippet6>

      // <Snippet7>
      TimeSpan ts1 = new TimeSpan(14, 3, 17);
      Console.WriteLine(ts1.ToString(@"d\.h\:mm\:ss"));

      TimeSpan ts2 = new TimeSpan(3, 4, 3, 17);
      Console.WriteLine(ts2.ToString(@"d\.h\:mm\:ss"));
      // The example displays the following output:
      //       0.14:03:17
      //       3.4:03:17
      // </Snippet7>
   }

   private static void ParseH()
   {
      // <Snippet8>
      string value = "8";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "%h", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       08:00:00
      // </Snippet8>
   }

   private static void ParseHH()
   {
      // <Snippet9>
      string value = "08";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "hh", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       08:00:00
      // </Snippet9>
   }

   private static void hhSpecifier()
   {
      // <Snippet10>
      TimeSpan ts1 = new TimeSpan(14, 3, 17);
      Console.WriteLine(ts1.ToString(@"d\.hh\:mm\:ss"));

      TimeSpan ts2 = new TimeSpan(3, 4, 3, 17);
      Console.WriteLine(ts2.ToString(@"d\.hh\:mm\:ss"));
      // The example displays the following output:
      //       0.14:03:17
      //       3.04:03:17
      // </Snippet10>
   }

   private static void ParseM()
   {
      // <Snippet11>
      string value = "3";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "%m", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       00:03:00
      // </Snippet11>
   }

   private static void mSpecifier()
   {
      // <Snippet12>
      TimeSpan ts1 = new TimeSpan(0, 6, 32);
      Console.WriteLine($"{ts1:m\\:ss} minutes");

      TimeSpan ts2 = new TimeSpan(3, 4, 3, 17);
      Console.WriteLine($"Elapsed time: {ts2:m\\:ss}");
      // The example displays the following output:
      //       6:32 minutes
      //       Elapsed time: 18:44
      // </Snippet12>
   }

   private static void ParseMM()
   {
      // <Snippet13>
      string value = "07";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "mm", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       00:07:00
      // </Snippet13>
   }

   private static void mmSpecifier()
   {
      // <Snippet14>
      TimeSpan departTime = new TimeSpan(11, 12, 00);
      TimeSpan arriveTime = new TimeSpan(16, 28, 00);
      Console.WriteLine($"Travel time: {arriveTime - departTime:hh\\:mm}");
      // The example displays the following output:
      //       Travel time: 05:16
      // </Snippet14>
   }

   private static void sSpecifier()
   {
      // <Snippet15>
      TimeSpan ts = TimeSpan.FromSeconds(12.465);
      Console.WriteLine(ts.ToString("%s"));
      // The example displays the following output:
      //       12
      // </Snippet15>
      Console.WriteLine();

      // <Snippet16>
      TimeSpan startTime = new TimeSpan(0, 12, 30, 15, 0);
      TimeSpan endTime = new TimeSpan(0, 12, 30, 21, 3);
      Console.WriteLine(@"Elapsed Time: {0:s\:fff} seconds",
                        endTime - startTime);
      // The example displays the following output:
      //       Elapsed Time: 6:003 seconds
      // </Snippet16>
   }

   private static void ParseS()
   {
      // <Snippet17>
      string value = "9";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "%s", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       00:00:09
      // </Snippet17>
   }

   private static void ParseSS()
   {
      // <Snippet18>
      string[] values = { "49", "9", "06" };
      TimeSpan interval;
      foreach (string value in values)
      {
         if (TimeSpan.TryParseExact(value, "ss", null, out interval))
            Console.WriteLine(interval.ToString("c"));
         else
            Console.WriteLine($"Unable to convert '{value}' to a time interval");
      }
      // The example displays the following output:
      //       00:00:49
      //       Unable to convert '9' to a time interval
      //       00:00:06
      // </Snippet18>
   }

   private static void ssSpecifier()
   {
      // <Snippet19>
      TimeSpan interval1 = TimeSpan.FromSeconds(12.60);
      Console.WriteLine(interval1.ToString(@"ss\.fff"));

      TimeSpan interval2 = TimeSpan.FromSeconds(6.485);
      Console.WriteLine(interval2.ToString(@"ss\.fff"));
      // The example displays the following output:
      //       12.600
      //       06.485
      // </Snippet19>
   }
}

sample · vbsamples/snippets/visualbasic/VS_Snippets_CLR/conceptual.timespan.custom/vb/customexamples1.vb7

vb
' Visual Basic .NET Document
Option Strict On

Module Example
    Public Sub Main()
        dSpecifier()
        Console.WriteLine("-------")
        ddSpecifier()
        Console.WriteLine("-------")
        hSpecifier()
        Console.WriteLine("-------")
        ParseH()
        Console.WriteLine("-------")
        ParseHH()
        Console.WriteLine("-------")
        hhSpecifier()
        Console.WriteLine("-------")
        ParseM()
        Console.WriteLine("-------")
        mSpecifier()
        Console.WriteLine("-------")
        ParseMM()
        Console.WriteLine("-------")
        mmSpecifier()
        Console.WriteLine("-------")
        sSpecifier()
        Console.WriteLine("-------")
        ParseS()
        Console.WriteLine("-------")
        ParseSS()
        Console.WriteLine("-------")
        ssSpecifier()
        Console.WriteLine("-------")
    End Sub

    Private Sub dSpecifier()
        ' <Snippet3>
        Dim ts As New TimeSpan(16, 4, 3, 17, 250)
        Console.WriteLine(ts.ToString("%d"))
        ' Displays 16   
        ' </Snippet3>
        Console.WriteLine()

        ' <Snippet4>
        Dim ts2 As New TimeSpan(4, 3, 17)
        Console.WriteLine(ts2.ToString("d\.hh\:mm\:ss"))

        Dim ts3 As New TimeSpan(3, 4, 3, 17)
        Console.WriteLine(ts3.ToString("d\.hh\:mm\:ss"))
        ' The example displays the following output:
        '       0.04:03:17
        '       3.04:03:17      
        ' </Snippet4>
    End Sub

    Private Sub ddSpecifier()
        ' <Snippet5>
        Dim ts1 As New TimeSpan(0, 23, 17, 47)
        Dim ts2 As New TimeSpan(365, 21, 19, 45)

        For ctr As Integer = 2 To 8
            Dim fmt As String = New String("d"c, ctr) + "\.hh\:mm\:ss"
            Console.WriteLine("{0} --> {1:" + fmt + "}", fmt, ts1)
            Console.WriteLine("{0} --> {1:" + fmt + "}", fmt, ts2)
            Console.WriteLine()
        Next
        ' The example displays the following output:
        '       dd\.hh\:mm\:ss --> 00.23:17:47
        '       dd\.hh\:mm\:ss --> 365.21:19:45
        '       
        '       ddd\.hh\:mm\:ss --> 000.23:17:47
        '       ddd\.hh\:mm\:ss --> 365.21:19:45
        '       
        '       dddd\.hh\:mm\:ss --> 0000.23:17:47
        '       dddd\.hh\:mm\:ss --> 0365.21:19:45
        '       
        '       ddddd\.hh\:mm\:ss --> 00000.23:17:47
        '       ddddd\.hh\:mm\:ss --> 00365.21:19:45
        '       
        '       dddddd\.hh\:mm\:ss --> 000000.23:17:47
        '       dddddd\.hh\:mm\:ss --> 000365.21:19:45
        '       
        '       ddddddd\.hh\:mm\:ss --> 0000000.23:17:47
        '       ddddddd\.hh\:mm\:ss --> 0000365.21:19:45
        '       
        '       dddddddd\.hh\:mm\:ss --> 00000000.23:17:47
        '       dddddddd\.hh\:mm\:ss --> 00000365.21:19:45      
        ' </Snippet5>
    End Sub

    Private Sub hSpecifier()
        ' <Snippet6>
        Dim ts As New TimeSpan(3, 42, 0)
        Console.WriteLine("{0:%h} hours {0:%m} minutes", ts)
        ' The example displays the following output:
        '       3 hours 42 minutes
        ' </Snippet6>
        Console.WriteLine()

        ' <Snippet7>
        Dim ts1 As New TimeSpan(14, 3, 17)
        Console.WriteLine(ts1.ToString("d\.h\:mm\:ss"))

        Dim ts2 As New TimeSpan(3, 4, 3, 17)
        Console.WriteLine(ts2.ToString("d\.h\:mm\:ss"))
        ' The example displays the following output:
        '       0.14:03:17
        '       3.4:03:17
        ' </Snippet7>
    End Sub

    Public Sub ParseH()
        ' <Snippet8>
        Dim value As String = "8"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "%h", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       08:00:00                              
        ' </Snippet8>
    End Sub

    Public Sub ParseHH()
        ' <Snippet9>
        Dim value As String = "08"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "hh", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       08:00:00                              
        ' </Snippet9>
    End Sub

    Private Sub hhSpecifier()
        ' <Snippet10>
        Dim ts1 As New TimeSpan(14, 3, 17)
        Console.WriteLine(ts1.ToString("d\.hh\:mm\:ss"))

        Dim ts2 As New TimeSpan(3, 4, 3, 17)
        Console.WriteLine(ts2.ToString("d\.hh\:mm\:ss"))
        ' The example displays the following output:
        '       0.14:03:17
        '       3.04:03:17
        ' </Snippet10>
    End Sub

    Public Sub ParseM()
        ' <Snippet11>
        Dim value As String = "3"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "%m", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       00:03:00                              
        ' </Snippet11>
    End Sub

    Private Sub mSpecifier()
        ' <Snippet12>
        Dim ts1 As New TimeSpan(0, 6, 32)
        Console.WriteLine("{0:m\:ss} minutes", ts1)

        Dim ts2 As New TimeSpan(0, 18, 44)
        Console.WriteLine("Elapsed time: {0:m\:ss}", ts2)
        ' The example displays the following output:
        '       6:32 minutes
        '       Elapsed time: 18:44
        ' </Snippet12>
    End Sub

    Public Sub ParseMM()
        ' <Snippet13>
        Dim value As String = "05"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "mm", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       00:05:00           
        ' </Snippet13>
    End Sub

    Private Sub mmSpecifier()
        ' <Snippet14>
        Dim departTime As New TimeSpan(11, 12, 00)
        Dim arriveTime As New TimeSpan(16, 28, 00)
        Console.WriteLine("Travel time: {0:hh\:mm}",
                          arriveTime - departTime)
        ' The example displays the following output:
        '       Travel time: 05:16      
        ' </Snippet14>
    End Sub

    Private Sub sSpecifier()
        ' <Snippet15>
        Dim ts As TimeSpan = TimeSpan.FromSeconds(12.465)
        Console.WriteLine(ts.ToString("%s"))
        ' The example displays the following output:
        '       12
        ' </Snippet15>
        Console.WriteLine()

        ' <Snippet16>
        Dim startTime As New TimeSpan(0, 12, 30, 15, 0)
        Dim endTime As New TimeSpan(0, 12, 30, 21, 3)
        Console.WriteLine("Elapsed Time: {0:s\:fff} seconds",
                          endTime - startTime)
        ' The example displays the following output:
        '       Elapsed Time: 6:003 seconds      
        ' </Snippet16>
    End Sub

    Public Sub ParseS()
        ' <Snippet17>
        Dim value As String = "9"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "%s", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       00:00:09
        ' </Snippet17>
    End Sub

    Public Sub ParseSS()
        ' <Snippet18>
        Dim values() As String = {"49", "9", "06"}
        Dim interval As TimeSpan
        For Each value As String In values
            If TimeSpan.TryParseExact(value, "ss", Nothing, interval) Then
                Console.WriteLine(interval.ToString("c"))
            Else
                Console.WriteLine("Unable to convert '{0}' to a time interval",
                                  value)
            End If
        Next
        ' The example displays the following output:
        '       00:00:49
        '       Unable to convert '9' to a time interval
        '       00:00:06
        ' </Snippet18>
    End Sub

    Private Sub ssSpecifier()
        ' <Snippet19>
        Dim interval1 As TimeSpan = TimeSpan.FromSeconds(12.60)
        Console.WriteLine(interval1.ToString("ss\.fff"))
        Dim interval2 As TimeSpan = TimeSpan.FromSeconds(6.485)
        Console.WriteLine(interval2.ToString("ss\.fff"))
        ' The example displays the following output:
        '       12.600
        '       06.485
        ' </Snippet19>
    End Sub
End Module

Back to table

The "hh" custom format specifier

The "hh" custom format specifier outputs the value of the System.TimeSpan.Hours property, which represents the number of whole hours in the time interval that isn't counted as part of its day component. For values from 0 through 9, the output string includes a leading zero.

Ordinarily, in a parsing operation, an input string that includes only a single number is interpreted as the number of days. You can use the "hh" custom format specifier instead to interpret the numeric string as the number of hours. The following example provides an illustration.

sample · csharpsamples/snippets/csharp/VS_Snippets_CLR/conceptual.timespan.custom/cs/customexamples1.cs9

csharp
using System;

public class Example
{
   public static void Main()
   {
      dSpecifier();
      Console.WriteLine("-------");
      ddSpecifier();
      Console.WriteLine("-------");
      hSpecifier();
      Console.WriteLine("-------");
      ParseH();
      Console.WriteLine("-------");
      ParseHH();
      Console.WriteLine("-------");
      hhSpecifier();
      Console.WriteLine("-------");
      ParseM();
      Console.WriteLine("-------");
      mSpecifier();
      Console.WriteLine("-------");
      ParseMM();
      Console.WriteLine("-------");
      mmSpecifier();
      Console.WriteLine("-------");
      sSpecifier();
      Console.WriteLine("-------");
      ParseS();
      Console.WriteLine("-------");
      ParseSS();
      Console.WriteLine("-------");
      ssSpecifier();
      Console.WriteLine("-------");
   }

   private static void dSpecifier()
   {
      // <Snippet3>
      TimeSpan ts1 = new TimeSpan(16, 4, 3, 17, 250);
      Console.WriteLine(ts1.ToString("%d"));
      // Displays 16
      // </Snippet3>
      Console.WriteLine();

      // <Snippet4>
      TimeSpan ts2 = new TimeSpan(4, 3, 17);
      Console.WriteLine(ts2.ToString(@"d\.hh\:mm\:ss"));

      TimeSpan ts3 = new TimeSpan(3, 4, 3, 17);
      Console.WriteLine(ts3.ToString(@"d\.hh\:mm\:ss"));
      // The example displays the following output:
      //       0.04:03:17
      //       3.04:03:17
      // </Snippet4>
   }

   private static void ddSpecifier()
   {
      // <Snippet5>
      TimeSpan ts1 = new TimeSpan(0, 23, 17, 47);
      TimeSpan ts2 = new TimeSpan(365, 21, 19, 45);

      for (int ctr = 2; ctr <= 8; ctr++)
      {
         string fmt = new String('d', ctr) + @"\.hh\:mm\:ss";
         Console.WriteLine($"{fmt} --> {ts1.ToString(fmt)}");
         Console.WriteLine($"{fmt} --> {ts2.ToString(fmt)}");
         Console.WriteLine();
      }
      // The example displays the following output:
      //       dd\.hh\:mm\:ss --> 00.23:17:47
      //       dd\.hh\:mm\:ss --> 365.21:19:45
      //
      //       ddd\.hh\:mm\:ss --> 000.23:17:47
      //       ddd\.hh\:mm\:ss --> 365.21:19:45
      //
      //       dddd\.hh\:mm\:ss --> 0000.23:17:47
      //       dddd\.hh\:mm\:ss --> 0365.21:19:45
      //
      //       ddddd\.hh\:mm\:ss --> 00000.23:17:47
      //       ddddd\.hh\:mm\:ss --> 00365.21:19:45
      //
      //       dddddd\.hh\:mm\:ss --> 000000.23:17:47
      //       dddddd\.hh\:mm\:ss --> 000365.21:19:45
      //
      //       ddddddd\.hh\:mm\:ss --> 0000000.23:17:47
      //       ddddddd\.hh\:mm\:ss --> 0000365.21:19:45
      //
      //       dddddddd\.hh\:mm\:ss --> 00000000.23:17:47
      //       dddddddd\.hh\:mm\:ss --> 00000365.21:19:45
      // </Snippet5>
   }

   private static void hSpecifier()
   {
      // <Snippet6>
      TimeSpan ts = new TimeSpan(3, 42, 0);
      Console.WriteLine($"{ts:%h} hours {ts:%m} minutes");
      // The example displays the following output:
      //       3 hours 42 minutes
      // </Snippet6>

      // <Snippet7>
      TimeSpan ts1 = new TimeSpan(14, 3, 17);
      Console.WriteLine(ts1.ToString(@"d\.h\:mm\:ss"));

      TimeSpan ts2 = new TimeSpan(3, 4, 3, 17);
      Console.WriteLine(ts2.ToString(@"d\.h\:mm\:ss"));
      // The example displays the following output:
      //       0.14:03:17
      //       3.4:03:17
      // </Snippet7>
   }

   private static void ParseH()
   {
      // <Snippet8>
      string value = "8";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "%h", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       08:00:00
      // </Snippet8>
   }

   private static void ParseHH()
   {
      // <Snippet9>
      string value = "08";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "hh", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       08:00:00
      // </Snippet9>
   }

   private static void hhSpecifier()
   {
      // <Snippet10>
      TimeSpan ts1 = new TimeSpan(14, 3, 17);
      Console.WriteLine(ts1.ToString(@"d\.hh\:mm\:ss"));

      TimeSpan ts2 = new TimeSpan(3, 4, 3, 17);
      Console.WriteLine(ts2.ToString(@"d\.hh\:mm\:ss"));
      // The example displays the following output:
      //       0.14:03:17
      //       3.04:03:17
      // </Snippet10>
   }

   private static void ParseM()
   {
      // <Snippet11>
      string value = "3";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "%m", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       00:03:00
      // </Snippet11>
   }

   private static void mSpecifier()
   {
      // <Snippet12>
      TimeSpan ts1 = new TimeSpan(0, 6, 32);
      Console.WriteLine($"{ts1:m\\:ss} minutes");

      TimeSpan ts2 = new TimeSpan(3, 4, 3, 17);
      Console.WriteLine($"Elapsed time: {ts2:m\\:ss}");
      // The example displays the following output:
      //       6:32 minutes
      //       Elapsed time: 18:44
      // </Snippet12>
   }

   private static void ParseMM()
   {
      // <Snippet13>
      string value = "07";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "mm", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       00:07:00
      // </Snippet13>
   }

   private static void mmSpecifier()
   {
      // <Snippet14>
      TimeSpan departTime = new TimeSpan(11, 12, 00);
      TimeSpan arriveTime = new TimeSpan(16, 28, 00);
      Console.WriteLine($"Travel time: {arriveTime - departTime:hh\\:mm}");
      // The example displays the following output:
      //       Travel time: 05:16
      // </Snippet14>
   }

   private static void sSpecifier()
   {
      // <Snippet15>
      TimeSpan ts = TimeSpan.FromSeconds(12.465);
      Console.WriteLine(ts.ToString("%s"));
      // The example displays the following output:
      //       12
      // </Snippet15>
      Console.WriteLine();

      // <Snippet16>
      TimeSpan startTime = new TimeSpan(0, 12, 30, 15, 0);
      TimeSpan endTime = new TimeSpan(0, 12, 30, 21, 3);
      Console.WriteLine(@"Elapsed Time: {0:s\:fff} seconds",
                        endTime - startTime);
      // The example displays the following output:
      //       Elapsed Time: 6:003 seconds
      // </Snippet16>
   }

   private static void ParseS()
   {
      // <Snippet17>
      string value = "9";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "%s", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       00:00:09
      // </Snippet17>
   }

   private static void ParseSS()
   {
      // <Snippet18>
      string[] values = { "49", "9", "06" };
      TimeSpan interval;
      foreach (string value in values)
      {
         if (TimeSpan.TryParseExact(value, "ss", null, out interval))
            Console.WriteLine(interval.ToString("c"));
         else
            Console.WriteLine($"Unable to convert '{value}' to a time interval");
      }
      // The example displays the following output:
      //       00:00:49
      //       Unable to convert '9' to a time interval
      //       00:00:06
      // </Snippet18>
   }

   private static void ssSpecifier()
   {
      // <Snippet19>
      TimeSpan interval1 = TimeSpan.FromSeconds(12.60);
      Console.WriteLine(interval1.ToString(@"ss\.fff"));

      TimeSpan interval2 = TimeSpan.FromSeconds(6.485);
      Console.WriteLine(interval2.ToString(@"ss\.fff"));
      // The example displays the following output:
      //       12.600
      //       06.485
      // </Snippet19>
   }
}

sample · vbsamples/snippets/visualbasic/VS_Snippets_CLR/conceptual.timespan.custom/vb/customexamples1.vb9

vb
' Visual Basic .NET Document
Option Strict On

Module Example
    Public Sub Main()
        dSpecifier()
        Console.WriteLine("-------")
        ddSpecifier()
        Console.WriteLine("-------")
        hSpecifier()
        Console.WriteLine("-------")
        ParseH()
        Console.WriteLine("-------")
        ParseHH()
        Console.WriteLine("-------")
        hhSpecifier()
        Console.WriteLine("-------")
        ParseM()
        Console.WriteLine("-------")
        mSpecifier()
        Console.WriteLine("-------")
        ParseMM()
        Console.WriteLine("-------")
        mmSpecifier()
        Console.WriteLine("-------")
        sSpecifier()
        Console.WriteLine("-------")
        ParseS()
        Console.WriteLine("-------")
        ParseSS()
        Console.WriteLine("-------")
        ssSpecifier()
        Console.WriteLine("-------")
    End Sub

    Private Sub dSpecifier()
        ' <Snippet3>
        Dim ts As New TimeSpan(16, 4, 3, 17, 250)
        Console.WriteLine(ts.ToString("%d"))
        ' Displays 16   
        ' </Snippet3>
        Console.WriteLine()

        ' <Snippet4>
        Dim ts2 As New TimeSpan(4, 3, 17)
        Console.WriteLine(ts2.ToString("d\.hh\:mm\:ss"))

        Dim ts3 As New TimeSpan(3, 4, 3, 17)
        Console.WriteLine(ts3.ToString("d\.hh\:mm\:ss"))
        ' The example displays the following output:
        '       0.04:03:17
        '       3.04:03:17      
        ' </Snippet4>
    End Sub

    Private Sub ddSpecifier()
        ' <Snippet5>
        Dim ts1 As New TimeSpan(0, 23, 17, 47)
        Dim ts2 As New TimeSpan(365, 21, 19, 45)

        For ctr As Integer = 2 To 8
            Dim fmt As String = New String("d"c, ctr) + "\.hh\:mm\:ss"
            Console.WriteLine("{0} --> {1:" + fmt + "}", fmt, ts1)
            Console.WriteLine("{0} --> {1:" + fmt + "}", fmt, ts2)
            Console.WriteLine()
        Next
        ' The example displays the following output:
        '       dd\.hh\:mm\:ss --> 00.23:17:47
        '       dd\.hh\:mm\:ss --> 365.21:19:45
        '       
        '       ddd\.hh\:mm\:ss --> 000.23:17:47
        '       ddd\.hh\:mm\:ss --> 365.21:19:45
        '       
        '       dddd\.hh\:mm\:ss --> 0000.23:17:47
        '       dddd\.hh\:mm\:ss --> 0365.21:19:45
        '       
        '       ddddd\.hh\:mm\:ss --> 00000.23:17:47
        '       ddddd\.hh\:mm\:ss --> 00365.21:19:45
        '       
        '       dddddd\.hh\:mm\:ss --> 000000.23:17:47
        '       dddddd\.hh\:mm\:ss --> 000365.21:19:45
        '       
        '       ddddddd\.hh\:mm\:ss --> 0000000.23:17:47
        '       ddddddd\.hh\:mm\:ss --> 0000365.21:19:45
        '       
        '       dddddddd\.hh\:mm\:ss --> 00000000.23:17:47
        '       dddddddd\.hh\:mm\:ss --> 00000365.21:19:45      
        ' </Snippet5>
    End Sub

    Private Sub hSpecifier()
        ' <Snippet6>
        Dim ts As New TimeSpan(3, 42, 0)
        Console.WriteLine("{0:%h} hours {0:%m} minutes", ts)
        ' The example displays the following output:
        '       3 hours 42 minutes
        ' </Snippet6>
        Console.WriteLine()

        ' <Snippet7>
        Dim ts1 As New TimeSpan(14, 3, 17)
        Console.WriteLine(ts1.ToString("d\.h\:mm\:ss"))

        Dim ts2 As New TimeSpan(3, 4, 3, 17)
        Console.WriteLine(ts2.ToString("d\.h\:mm\:ss"))
        ' The example displays the following output:
        '       0.14:03:17
        '       3.4:03:17
        ' </Snippet7>
    End Sub

    Public Sub ParseH()
        ' <Snippet8>
        Dim value As String = "8"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "%h", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       08:00:00                              
        ' </Snippet8>
    End Sub

    Public Sub ParseHH()
        ' <Snippet9>
        Dim value As String = "08"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "hh", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       08:00:00                              
        ' </Snippet9>
    End Sub

    Private Sub hhSpecifier()
        ' <Snippet10>
        Dim ts1 As New TimeSpan(14, 3, 17)
        Console.WriteLine(ts1.ToString("d\.hh\:mm\:ss"))

        Dim ts2 As New TimeSpan(3, 4, 3, 17)
        Console.WriteLine(ts2.ToString("d\.hh\:mm\:ss"))
        ' The example displays the following output:
        '       0.14:03:17
        '       3.04:03:17
        ' </Snippet10>
    End Sub

    Public Sub ParseM()
        ' <Snippet11>
        Dim value As String = "3"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "%m", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       00:03:00                              
        ' </Snippet11>
    End Sub

    Private Sub mSpecifier()
        ' <Snippet12>
        Dim ts1 As New TimeSpan(0, 6, 32)
        Console.WriteLine("{0:m\:ss} minutes", ts1)

        Dim ts2 As New TimeSpan(0, 18, 44)
        Console.WriteLine("Elapsed time: {0:m\:ss}", ts2)
        ' The example displays the following output:
        '       6:32 minutes
        '       Elapsed time: 18:44
        ' </Snippet12>
    End Sub

    Public Sub ParseMM()
        ' <Snippet13>
        Dim value As String = "05"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "mm", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       00:05:00           
        ' </Snippet13>
    End Sub

    Private Sub mmSpecifier()
        ' <Snippet14>
        Dim departTime As New TimeSpan(11, 12, 00)
        Dim arriveTime As New TimeSpan(16, 28, 00)
        Console.WriteLine("Travel time: {0:hh\:mm}",
                          arriveTime - departTime)
        ' The example displays the following output:
        '       Travel time: 05:16      
        ' </Snippet14>
    End Sub

    Private Sub sSpecifier()
        ' <Snippet15>
        Dim ts As TimeSpan = TimeSpan.FromSeconds(12.465)
        Console.WriteLine(ts.ToString("%s"))
        ' The example displays the following output:
        '       12
        ' </Snippet15>
        Console.WriteLine()

        ' <Snippet16>
        Dim startTime As New TimeSpan(0, 12, 30, 15, 0)
        Dim endTime As New TimeSpan(0, 12, 30, 21, 3)
        Console.WriteLine("Elapsed Time: {0:s\:fff} seconds",
                          endTime - startTime)
        ' The example displays the following output:
        '       Elapsed Time: 6:003 seconds      
        ' </Snippet16>
    End Sub

    Public Sub ParseS()
        ' <Snippet17>
        Dim value As String = "9"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "%s", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       00:00:09
        ' </Snippet17>
    End Sub

    Public Sub ParseSS()
        ' <Snippet18>
        Dim values() As String = {"49", "9", "06"}
        Dim interval As TimeSpan
        For Each value As String In values
            If TimeSpan.TryParseExact(value, "ss", Nothing, interval) Then
                Console.WriteLine(interval.ToString("c"))
            Else
                Console.WriteLine("Unable to convert '{0}' to a time interval",
                                  value)
            End If
        Next
        ' The example displays the following output:
        '       00:00:49
        '       Unable to convert '9' to a time interval
        '       00:00:06
        ' </Snippet18>
    End Sub

    Private Sub ssSpecifier()
        ' <Snippet19>
        Dim interval1 As TimeSpan = TimeSpan.FromSeconds(12.60)
        Console.WriteLine(interval1.ToString("ss\.fff"))
        Dim interval2 As TimeSpan = TimeSpan.FromSeconds(6.485)
        Console.WriteLine(interval2.ToString("ss\.fff"))
        ' The example displays the following output:
        '       12.600
        '       06.485
        ' </Snippet19>
    End Sub
End Module

The following example illustrates the use of the "hh" custom format specifier.

sample · csharpsamples/snippets/csharp/VS_Snippets_CLR/conceptual.timespan.custom/cs/customexamples1.cs10

csharp
using System;

public class Example
{
   public static void Main()
   {
      dSpecifier();
      Console.WriteLine("-------");
      ddSpecifier();
      Console.WriteLine("-------");
      hSpecifier();
      Console.WriteLine("-------");
      ParseH();
      Console.WriteLine("-------");
      ParseHH();
      Console.WriteLine("-------");
      hhSpecifier();
      Console.WriteLine("-------");
      ParseM();
      Console.WriteLine("-------");
      mSpecifier();
      Console.WriteLine("-------");
      ParseMM();
      Console.WriteLine("-------");
      mmSpecifier();
      Console.WriteLine("-------");
      sSpecifier();
      Console.WriteLine("-------");
      ParseS();
      Console.WriteLine("-------");
      ParseSS();
      Console.WriteLine("-------");
      ssSpecifier();
      Console.WriteLine("-------");
   }

   private static void dSpecifier()
   {
      // <Snippet3>
      TimeSpan ts1 = new TimeSpan(16, 4, 3, 17, 250);
      Console.WriteLine(ts1.ToString("%d"));
      // Displays 16
      // </Snippet3>
      Console.WriteLine();

      // <Snippet4>
      TimeSpan ts2 = new TimeSpan(4, 3, 17);
      Console.WriteLine(ts2.ToString(@"d\.hh\:mm\:ss"));

      TimeSpan ts3 = new TimeSpan(3, 4, 3, 17);
      Console.WriteLine(ts3.ToString(@"d\.hh\:mm\:ss"));
      // The example displays the following output:
      //       0.04:03:17
      //       3.04:03:17
      // </Snippet4>
   }

   private static void ddSpecifier()
   {
      // <Snippet5>
      TimeSpan ts1 = new TimeSpan(0, 23, 17, 47);
      TimeSpan ts2 = new TimeSpan(365, 21, 19, 45);

      for (int ctr = 2; ctr <= 8; ctr++)
      {
         string fmt = new String('d', ctr) + @"\.hh\:mm\:ss";
         Console.WriteLine($"{fmt} --> {ts1.ToString(fmt)}");
         Console.WriteLine($"{fmt} --> {ts2.ToString(fmt)}");
         Console.WriteLine();
      }
      // The example displays the following output:
      //       dd\.hh\:mm\:ss --> 00.23:17:47
      //       dd\.hh\:mm\:ss --> 365.21:19:45
      //
      //       ddd\.hh\:mm\:ss --> 000.23:17:47
      //       ddd\.hh\:mm\:ss --> 365.21:19:45
      //
      //       dddd\.hh\:mm\:ss --> 0000.23:17:47
      //       dddd\.hh\:mm\:ss --> 0365.21:19:45
      //
      //       ddddd\.hh\:mm\:ss --> 00000.23:17:47
      //       ddddd\.hh\:mm\:ss --> 00365.21:19:45
      //
      //       dddddd\.hh\:mm\:ss --> 000000.23:17:47
      //       dddddd\.hh\:mm\:ss --> 000365.21:19:45
      //
      //       ddddddd\.hh\:mm\:ss --> 0000000.23:17:47
      //       ddddddd\.hh\:mm\:ss --> 0000365.21:19:45
      //
      //       dddddddd\.hh\:mm\:ss --> 00000000.23:17:47
      //       dddddddd\.hh\:mm\:ss --> 00000365.21:19:45
      // </Snippet5>
   }

   private static void hSpecifier()
   {
      // <Snippet6>
      TimeSpan ts = new TimeSpan(3, 42, 0);
      Console.WriteLine($"{ts:%h} hours {ts:%m} minutes");
      // The example displays the following output:
      //       3 hours 42 minutes
      // </Snippet6>

      // <Snippet7>
      TimeSpan ts1 = new TimeSpan(14, 3, 17);
      Console.WriteLine(ts1.ToString(@"d\.h\:mm\:ss"));

      TimeSpan ts2 = new TimeSpan(3, 4, 3, 17);
      Console.WriteLine(ts2.ToString(@"d\.h\:mm\:ss"));
      // The example displays the following output:
      //       0.14:03:17
      //       3.4:03:17
      // </Snippet7>
   }

   private static void ParseH()
   {
      // <Snippet8>
      string value = "8";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "%h", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       08:00:00
      // </Snippet8>
   }

   private static void ParseHH()
   {
      // <Snippet9>
      string value = "08";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "hh", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       08:00:00
      // </Snippet9>
   }

   private static void hhSpecifier()
   {
      // <Snippet10>
      TimeSpan ts1 = new TimeSpan(14, 3, 17);
      Console.WriteLine(ts1.ToString(@"d\.hh\:mm\:ss"));

      TimeSpan ts2 = new TimeSpan(3, 4, 3, 17);
      Console.WriteLine(ts2.ToString(@"d\.hh\:mm\:ss"));
      // The example displays the following output:
      //       0.14:03:17
      //       3.04:03:17
      // </Snippet10>
   }

   private static void ParseM()
   {
      // <Snippet11>
      string value = "3";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "%m", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       00:03:00
      // </Snippet11>
   }

   private static void mSpecifier()
   {
      // <Snippet12>
      TimeSpan ts1 = new TimeSpan(0, 6, 32);
      Console.WriteLine($"{ts1:m\\:ss} minutes");

      TimeSpan ts2 = new TimeSpan(3, 4, 3, 17);
      Console.WriteLine($"Elapsed time: {ts2:m\\:ss}");
      // The example displays the following output:
      //       6:32 minutes
      //       Elapsed time: 18:44
      // </Snippet12>
   }

   private static void ParseMM()
   {
      // <Snippet13>
      string value = "07";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "mm", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       00:07:00
      // </Snippet13>
   }

   private static void mmSpecifier()
   {
      // <Snippet14>
      TimeSpan departTime = new TimeSpan(11, 12, 00);
      TimeSpan arriveTime = new TimeSpan(16, 28, 00);
      Console.WriteLine($"Travel time: {arriveTime - departTime:hh\\:mm}");
      // The example displays the following output:
      //       Travel time: 05:16
      // </Snippet14>
   }

   private static void sSpecifier()
   {
      // <Snippet15>
      TimeSpan ts = TimeSpan.FromSeconds(12.465);
      Console.WriteLine(ts.ToString("%s"));
      // The example displays the following output:
      //       12
      // </Snippet15>
      Console.WriteLine();

      // <Snippet16>
      TimeSpan startTime = new TimeSpan(0, 12, 30, 15, 0);
      TimeSpan endTime = new TimeSpan(0, 12, 30, 21, 3);
      Console.WriteLine(@"Elapsed Time: {0:s\:fff} seconds",
                        endTime - startTime);
      // The example displays the following output:
      //       Elapsed Time: 6:003 seconds
      // </Snippet16>
   }

   private static void ParseS()
   {
      // <Snippet17>
      string value = "9";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "%s", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       00:00:09
      // </Snippet17>
   }

   private static void ParseSS()
   {
      // <Snippet18>
      string[] values = { "49", "9", "06" };
      TimeSpan interval;
      foreach (string value in values)
      {
         if (TimeSpan.TryParseExact(value, "ss", null, out interval))
            Console.WriteLine(interval.ToString("c"));
         else
            Console.WriteLine($"Unable to convert '{value}' to a time interval");
      }
      // The example displays the following output:
      //       00:00:49
      //       Unable to convert '9' to a time interval
      //       00:00:06
      // </Snippet18>
   }

   private static void ssSpecifier()
   {
      // <Snippet19>
      TimeSpan interval1 = TimeSpan.FromSeconds(12.60);
      Console.WriteLine(interval1.ToString(@"ss\.fff"));

      TimeSpan interval2 = TimeSpan.FromSeconds(6.485);
      Console.WriteLine(interval2.ToString(@"ss\.fff"));
      // The example displays the following output:
      //       12.600
      //       06.485
      // </Snippet19>
   }
}

sample · vbsamples/snippets/visualbasic/VS_Snippets_CLR/conceptual.timespan.custom/vb/customexamples1.vb10

vb
' Visual Basic .NET Document
Option Strict On

Module Example
    Public Sub Main()
        dSpecifier()
        Console.WriteLine("-------")
        ddSpecifier()
        Console.WriteLine("-------")
        hSpecifier()
        Console.WriteLine("-------")
        ParseH()
        Console.WriteLine("-------")
        ParseHH()
        Console.WriteLine("-------")
        hhSpecifier()
        Console.WriteLine("-------")
        ParseM()
        Console.WriteLine("-------")
        mSpecifier()
        Console.WriteLine("-------")
        ParseMM()
        Console.WriteLine("-------")
        mmSpecifier()
        Console.WriteLine("-------")
        sSpecifier()
        Console.WriteLine("-------")
        ParseS()
        Console.WriteLine("-------")
        ParseSS()
        Console.WriteLine("-------")
        ssSpecifier()
        Console.WriteLine("-------")
    End Sub

    Private Sub dSpecifier()
        ' <Snippet3>
        Dim ts As New TimeSpan(16, 4, 3, 17, 250)
        Console.WriteLine(ts.ToString("%d"))
        ' Displays 16   
        ' </Snippet3>
        Console.WriteLine()

        ' <Snippet4>
        Dim ts2 As New TimeSpan(4, 3, 17)
        Console.WriteLine(ts2.ToString("d\.hh\:mm\:ss"))

        Dim ts3 As New TimeSpan(3, 4, 3, 17)
        Console.WriteLine(ts3.ToString("d\.hh\:mm\:ss"))
        ' The example displays the following output:
        '       0.04:03:17
        '       3.04:03:17      
        ' </Snippet4>
    End Sub

    Private Sub ddSpecifier()
        ' <Snippet5>
        Dim ts1 As New TimeSpan(0, 23, 17, 47)
        Dim ts2 As New TimeSpan(365, 21, 19, 45)

        For ctr As Integer = 2 To 8
            Dim fmt As String = New String("d"c, ctr) + "\.hh\:mm\:ss"
            Console.WriteLine("{0} --> {1:" + fmt + "}", fmt, ts1)
            Console.WriteLine("{0} --> {1:" + fmt + "}", fmt, ts2)
            Console.WriteLine()
        Next
        ' The example displays the following output:
        '       dd\.hh\:mm\:ss --> 00.23:17:47
        '       dd\.hh\:mm\:ss --> 365.21:19:45
        '       
        '       ddd\.hh\:mm\:ss --> 000.23:17:47
        '       ddd\.hh\:mm\:ss --> 365.21:19:45
        '       
        '       dddd\.hh\:mm\:ss --> 0000.23:17:47
        '       dddd\.hh\:mm\:ss --> 0365.21:19:45
        '       
        '       ddddd\.hh\:mm\:ss --> 00000.23:17:47
        '       ddddd\.hh\:mm\:ss --> 00365.21:19:45
        '       
        '       dddddd\.hh\:mm\:ss --> 000000.23:17:47
        '       dddddd\.hh\:mm\:ss --> 000365.21:19:45
        '       
        '       ddddddd\.hh\:mm\:ss --> 0000000.23:17:47
        '       ddddddd\.hh\:mm\:ss --> 0000365.21:19:45
        '       
        '       dddddddd\.hh\:mm\:ss --> 00000000.23:17:47
        '       dddddddd\.hh\:mm\:ss --> 00000365.21:19:45      
        ' </Snippet5>
    End Sub

    Private Sub hSpecifier()
        ' <Snippet6>
        Dim ts As New TimeSpan(3, 42, 0)
        Console.WriteLine("{0:%h} hours {0:%m} minutes", ts)
        ' The example displays the following output:
        '       3 hours 42 minutes
        ' </Snippet6>
        Console.WriteLine()

        ' <Snippet7>
        Dim ts1 As New TimeSpan(14, 3, 17)
        Console.WriteLine(ts1.ToString("d\.h\:mm\:ss"))

        Dim ts2 As New TimeSpan(3, 4, 3, 17)
        Console.WriteLine(ts2.ToString("d\.h\:mm\:ss"))
        ' The example displays the following output:
        '       0.14:03:17
        '       3.4:03:17
        ' </Snippet7>
    End Sub

    Public Sub ParseH()
        ' <Snippet8>
        Dim value As String = "8"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "%h", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       08:00:00                              
        ' </Snippet8>
    End Sub

    Public Sub ParseHH()
        ' <Snippet9>
        Dim value As String = "08"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "hh", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       08:00:00                              
        ' </Snippet9>
    End Sub

    Private Sub hhSpecifier()
        ' <Snippet10>
        Dim ts1 As New TimeSpan(14, 3, 17)
        Console.WriteLine(ts1.ToString("d\.hh\:mm\:ss"))

        Dim ts2 As New TimeSpan(3, 4, 3, 17)
        Console.WriteLine(ts2.ToString("d\.hh\:mm\:ss"))
        ' The example displays the following output:
        '       0.14:03:17
        '       3.04:03:17
        ' </Snippet10>
    End Sub

    Public Sub ParseM()
        ' <Snippet11>
        Dim value As String = "3"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "%m", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       00:03:00                              
        ' </Snippet11>
    End Sub

    Private Sub mSpecifier()
        ' <Snippet12>
        Dim ts1 As New TimeSpan(0, 6, 32)
        Console.WriteLine("{0:m\:ss} minutes", ts1)

        Dim ts2 As New TimeSpan(0, 18, 44)
        Console.WriteLine("Elapsed time: {0:m\:ss}", ts2)
        ' The example displays the following output:
        '       6:32 minutes
        '       Elapsed time: 18:44
        ' </Snippet12>
    End Sub

    Public Sub ParseMM()
        ' <Snippet13>
        Dim value As String = "05"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "mm", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       00:05:00           
        ' </Snippet13>
    End Sub

    Private Sub mmSpecifier()
        ' <Snippet14>
        Dim departTime As New TimeSpan(11, 12, 00)
        Dim arriveTime As New TimeSpan(16, 28, 00)
        Console.WriteLine("Travel time: {0:hh\:mm}",
                          arriveTime - departTime)
        ' The example displays the following output:
        '       Travel time: 05:16      
        ' </Snippet14>
    End Sub

    Private Sub sSpecifier()
        ' <Snippet15>
        Dim ts As TimeSpan = TimeSpan.FromSeconds(12.465)
        Console.WriteLine(ts.ToString("%s"))
        ' The example displays the following output:
        '       12
        ' </Snippet15>
        Console.WriteLine()

        ' <Snippet16>
        Dim startTime As New TimeSpan(0, 12, 30, 15, 0)
        Dim endTime As New TimeSpan(0, 12, 30, 21, 3)
        Console.WriteLine("Elapsed Time: {0:s\:fff} seconds",
                          endTime - startTime)
        ' The example displays the following output:
        '       Elapsed Time: 6:003 seconds      
        ' </Snippet16>
    End Sub

    Public Sub ParseS()
        ' <Snippet17>
        Dim value As String = "9"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "%s", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       00:00:09
        ' </Snippet17>
    End Sub

    Public Sub ParseSS()
        ' <Snippet18>
        Dim values() As String = {"49", "9", "06"}
        Dim interval As TimeSpan
        For Each value As String In values
            If TimeSpan.TryParseExact(value, "ss", Nothing, interval) Then
                Console.WriteLine(interval.ToString("c"))
            Else
                Console.WriteLine("Unable to convert '{0}' to a time interval",
                                  value)
            End If
        Next
        ' The example displays the following output:
        '       00:00:49
        '       Unable to convert '9' to a time interval
        '       00:00:06
        ' </Snippet18>
    End Sub

    Private Sub ssSpecifier()
        ' <Snippet19>
        Dim interval1 As TimeSpan = TimeSpan.FromSeconds(12.60)
        Console.WriteLine(interval1.ToString("ss\.fff"))
        Dim interval2 As TimeSpan = TimeSpan.FromSeconds(6.485)
        Console.WriteLine(interval2.ToString("ss\.fff"))
        ' The example displays the following output:
        '       12.600
        '       06.485
        ' </Snippet19>
    End Sub
End Module

Back to table

The "m" custom format specifier

The "m" custom format specifier outputs the value of the System.TimeSpan.Minutes property, which represents the number of whole minutes in the time interval that isn't counted as part of its day component. It returns a one-digit string value if the value of the System.TimeSpan.Minutes property is 0 through 9, and it returns a two-digit string value if the value of the System.TimeSpan.Minutes property ranges from 10 to 59.

If the "m" custom format specifier is used alone, specify "%m" so that it isn't misinterpreted as a standard format string. The following example provides an illustration.

sample · csharpsamples/snippets/csharp/VS_Snippets_CLR/conceptual.timespan.custom/cs/customexamples1.cs6

csharp
using System;

public class Example
{
   public static void Main()
   {
      dSpecifier();
      Console.WriteLine("-------");
      ddSpecifier();
      Console.WriteLine("-------");
      hSpecifier();
      Console.WriteLine("-------");
      ParseH();
      Console.WriteLine("-------");
      ParseHH();
      Console.WriteLine("-------");
      hhSpecifier();
      Console.WriteLine("-------");
      ParseM();
      Console.WriteLine("-------");
      mSpecifier();
      Console.WriteLine("-------");
      ParseMM();
      Console.WriteLine("-------");
      mmSpecifier();
      Console.WriteLine("-------");
      sSpecifier();
      Console.WriteLine("-------");
      ParseS();
      Console.WriteLine("-------");
      ParseSS();
      Console.WriteLine("-------");
      ssSpecifier();
      Console.WriteLine("-------");
   }

   private static void dSpecifier()
   {
      // <Snippet3>
      TimeSpan ts1 = new TimeSpan(16, 4, 3, 17, 250);
      Console.WriteLine(ts1.ToString("%d"));
      // Displays 16
      // </Snippet3>
      Console.WriteLine();

      // <Snippet4>
      TimeSpan ts2 = new TimeSpan(4, 3, 17);
      Console.WriteLine(ts2.ToString(@"d\.hh\:mm\:ss"));

      TimeSpan ts3 = new TimeSpan(3, 4, 3, 17);
      Console.WriteLine(ts3.ToString(@"d\.hh\:mm\:ss"));
      // The example displays the following output:
      //       0.04:03:17
      //       3.04:03:17
      // </Snippet4>
   }

   private static void ddSpecifier()
   {
      // <Snippet5>
      TimeSpan ts1 = new TimeSpan(0, 23, 17, 47);
      TimeSpan ts2 = new TimeSpan(365, 21, 19, 45);

      for (int ctr = 2; ctr <= 8; ctr++)
      {
         string fmt = new String('d', ctr) + @"\.hh\:mm\:ss";
         Console.WriteLine($"{fmt} --> {ts1.ToString(fmt)}");
         Console.WriteLine($"{fmt} --> {ts2.ToString(fmt)}");
         Console.WriteLine();
      }
      // The example displays the following output:
      //       dd\.hh\:mm\:ss --> 00.23:17:47
      //       dd\.hh\:mm\:ss --> 365.21:19:45
      //
      //       ddd\.hh\:mm\:ss --> 000.23:17:47
      //       ddd\.hh\:mm\:ss --> 365.21:19:45
      //
      //       dddd\.hh\:mm\:ss --> 0000.23:17:47
      //       dddd\.hh\:mm\:ss --> 0365.21:19:45
      //
      //       ddddd\.hh\:mm\:ss --> 00000.23:17:47
      //       ddddd\.hh\:mm\:ss --> 00365.21:19:45
      //
      //       dddddd\.hh\:mm\:ss --> 000000.23:17:47
      //       dddddd\.hh\:mm\:ss --> 000365.21:19:45
      //
      //       ddddddd\.hh\:mm\:ss --> 0000000.23:17:47
      //       ddddddd\.hh\:mm\:ss --> 0000365.21:19:45
      //
      //       dddddddd\.hh\:mm\:ss --> 00000000.23:17:47
      //       dddddddd\.hh\:mm\:ss --> 00000365.21:19:45
      // </Snippet5>
   }

   private static void hSpecifier()
   {
      // <Snippet6>
      TimeSpan ts = new TimeSpan(3, 42, 0);
      Console.WriteLine($"{ts:%h} hours {ts:%m} minutes");
      // The example displays the following output:
      //       3 hours 42 minutes
      // </Snippet6>

      // <Snippet7>
      TimeSpan ts1 = new TimeSpan(14, 3, 17);
      Console.WriteLine(ts1.ToString(@"d\.h\:mm\:ss"));

      TimeSpan ts2 = new TimeSpan(3, 4, 3, 17);
      Console.WriteLine(ts2.ToString(@"d\.h\:mm\:ss"));
      // The example displays the following output:
      //       0.14:03:17
      //       3.4:03:17
      // </Snippet7>
   }

   private static void ParseH()
   {
      // <Snippet8>
      string value = "8";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "%h", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       08:00:00
      // </Snippet8>
   }

   private static void ParseHH()
   {
      // <Snippet9>
      string value = "08";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "hh", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       08:00:00
      // </Snippet9>
   }

   private static void hhSpecifier()
   {
      // <Snippet10>
      TimeSpan ts1 = new TimeSpan(14, 3, 17);
      Console.WriteLine(ts1.ToString(@"d\.hh\:mm\:ss"));

      TimeSpan ts2 = new TimeSpan(3, 4, 3, 17);
      Console.WriteLine(ts2.ToString(@"d\.hh\:mm\:ss"));
      // The example displays the following output:
      //       0.14:03:17
      //       3.04:03:17
      // </Snippet10>
   }

   private static void ParseM()
   {
      // <Snippet11>
      string value = "3";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "%m", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       00:03:00
      // </Snippet11>
   }

   private static void mSpecifier()
   {
      // <Snippet12>
      TimeSpan ts1 = new TimeSpan(0, 6, 32);
      Console.WriteLine($"{ts1:m\\:ss} minutes");

      TimeSpan ts2 = new TimeSpan(3, 4, 3, 17);
      Console.WriteLine($"Elapsed time: {ts2:m\\:ss}");
      // The example displays the following output:
      //       6:32 minutes
      //       Elapsed time: 18:44
      // </Snippet12>
   }

   private static void ParseMM()
   {
      // <Snippet13>
      string value = "07";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "mm", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       00:07:00
      // </Snippet13>
   }

   private static void mmSpecifier()
   {
      // <Snippet14>
      TimeSpan departTime = new TimeSpan(11, 12, 00);
      TimeSpan arriveTime = new TimeSpan(16, 28, 00);
      Console.WriteLine($"Travel time: {arriveTime - departTime:hh\\:mm}");
      // The example displays the following output:
      //       Travel time: 05:16
      // </Snippet14>
   }

   private static void sSpecifier()
   {
      // <Snippet15>
      TimeSpan ts = TimeSpan.FromSeconds(12.465);
      Console.WriteLine(ts.ToString("%s"));
      // The example displays the following output:
      //       12
      // </Snippet15>
      Console.WriteLine();

      // <Snippet16>
      TimeSpan startTime = new TimeSpan(0, 12, 30, 15, 0);
      TimeSpan endTime = new TimeSpan(0, 12, 30, 21, 3);
      Console.WriteLine(@"Elapsed Time: {0:s\:fff} seconds",
                        endTime - startTime);
      // The example displays the following output:
      //       Elapsed Time: 6:003 seconds
      // </Snippet16>
   }

   private static void ParseS()
   {
      // <Snippet17>
      string value = "9";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "%s", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       00:00:09
      // </Snippet17>
   }

   private static void ParseSS()
   {
      // <Snippet18>
      string[] values = { "49", "9", "06" };
      TimeSpan interval;
      foreach (string value in values)
      {
         if (TimeSpan.TryParseExact(value, "ss", null, out interval))
            Console.WriteLine(interval.ToString("c"));
         else
            Console.WriteLine($"Unable to convert '{value}' to a time interval");
      }
      // The example displays the following output:
      //       00:00:49
      //       Unable to convert '9' to a time interval
      //       00:00:06
      // </Snippet18>
   }

   private static void ssSpecifier()
   {
      // <Snippet19>
      TimeSpan interval1 = TimeSpan.FromSeconds(12.60);
      Console.WriteLine(interval1.ToString(@"ss\.fff"));

      TimeSpan interval2 = TimeSpan.FromSeconds(6.485);
      Console.WriteLine(interval2.ToString(@"ss\.fff"));
      // The example displays the following output:
      //       12.600
      //       06.485
      // </Snippet19>
   }
}

sample · vbsamples/snippets/visualbasic/VS_Snippets_CLR/conceptual.timespan.custom/vb/customexamples1.vb6

vb
' Visual Basic .NET Document
Option Strict On

Module Example
    Public Sub Main()
        dSpecifier()
        Console.WriteLine("-------")
        ddSpecifier()
        Console.WriteLine("-------")
        hSpecifier()
        Console.WriteLine("-------")
        ParseH()
        Console.WriteLine("-------")
        ParseHH()
        Console.WriteLine("-------")
        hhSpecifier()
        Console.WriteLine("-------")
        ParseM()
        Console.WriteLine("-------")
        mSpecifier()
        Console.WriteLine("-------")
        ParseMM()
        Console.WriteLine("-------")
        mmSpecifier()
        Console.WriteLine("-------")
        sSpecifier()
        Console.WriteLine("-------")
        ParseS()
        Console.WriteLine("-------")
        ParseSS()
        Console.WriteLine("-------")
        ssSpecifier()
        Console.WriteLine("-------")
    End Sub

    Private Sub dSpecifier()
        ' <Snippet3>
        Dim ts As New TimeSpan(16, 4, 3, 17, 250)
        Console.WriteLine(ts.ToString("%d"))
        ' Displays 16   
        ' </Snippet3>
        Console.WriteLine()

        ' <Snippet4>
        Dim ts2 As New TimeSpan(4, 3, 17)
        Console.WriteLine(ts2.ToString("d\.hh\:mm\:ss"))

        Dim ts3 As New TimeSpan(3, 4, 3, 17)
        Console.WriteLine(ts3.ToString("d\.hh\:mm\:ss"))
        ' The example displays the following output:
        '       0.04:03:17
        '       3.04:03:17      
        ' </Snippet4>
    End Sub

    Private Sub ddSpecifier()
        ' <Snippet5>
        Dim ts1 As New TimeSpan(0, 23, 17, 47)
        Dim ts2 As New TimeSpan(365, 21, 19, 45)

        For ctr As Integer = 2 To 8
            Dim fmt As String = New String("d"c, ctr) + "\.hh\:mm\:ss"
            Console.WriteLine("{0} --> {1:" + fmt + "}", fmt, ts1)
            Console.WriteLine("{0} --> {1:" + fmt + "}", fmt, ts2)
            Console.WriteLine()
        Next
        ' The example displays the following output:
        '       dd\.hh\:mm\:ss --> 00.23:17:47
        '       dd\.hh\:mm\:ss --> 365.21:19:45
        '       
        '       ddd\.hh\:mm\:ss --> 000.23:17:47
        '       ddd\.hh\:mm\:ss --> 365.21:19:45
        '       
        '       dddd\.hh\:mm\:ss --> 0000.23:17:47
        '       dddd\.hh\:mm\:ss --> 0365.21:19:45
        '       
        '       ddddd\.hh\:mm\:ss --> 00000.23:17:47
        '       ddddd\.hh\:mm\:ss --> 00365.21:19:45
        '       
        '       dddddd\.hh\:mm\:ss --> 000000.23:17:47
        '       dddddd\.hh\:mm\:ss --> 000365.21:19:45
        '       
        '       ddddddd\.hh\:mm\:ss --> 0000000.23:17:47
        '       ddddddd\.hh\:mm\:ss --> 0000365.21:19:45
        '       
        '       dddddddd\.hh\:mm\:ss --> 00000000.23:17:47
        '       dddddddd\.hh\:mm\:ss --> 00000365.21:19:45      
        ' </Snippet5>
    End Sub

    Private Sub hSpecifier()
        ' <Snippet6>
        Dim ts As New TimeSpan(3, 42, 0)
        Console.WriteLine("{0:%h} hours {0:%m} minutes", ts)
        ' The example displays the following output:
        '       3 hours 42 minutes
        ' </Snippet6>
        Console.WriteLine()

        ' <Snippet7>
        Dim ts1 As New TimeSpan(14, 3, 17)
        Console.WriteLine(ts1.ToString("d\.h\:mm\:ss"))

        Dim ts2 As New TimeSpan(3, 4, 3, 17)
        Console.WriteLine(ts2.ToString("d\.h\:mm\:ss"))
        ' The example displays the following output:
        '       0.14:03:17
        '       3.4:03:17
        ' </Snippet7>
    End Sub

    Public Sub ParseH()
        ' <Snippet8>
        Dim value As String = "8"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "%h", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       08:00:00                              
        ' </Snippet8>
    End Sub

    Public Sub ParseHH()
        ' <Snippet9>
        Dim value As String = "08"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "hh", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       08:00:00                              
        ' </Snippet9>
    End Sub

    Private Sub hhSpecifier()
        ' <Snippet10>
        Dim ts1 As New TimeSpan(14, 3, 17)
        Console.WriteLine(ts1.ToString("d\.hh\:mm\:ss"))

        Dim ts2 As New TimeSpan(3, 4, 3, 17)
        Console.WriteLine(ts2.ToString("d\.hh\:mm\:ss"))
        ' The example displays the following output:
        '       0.14:03:17
        '       3.04:03:17
        ' </Snippet10>
    End Sub

    Public Sub ParseM()
        ' <Snippet11>
        Dim value As String = "3"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "%m", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       00:03:00                              
        ' </Snippet11>
    End Sub

    Private Sub mSpecifier()
        ' <Snippet12>
        Dim ts1 As New TimeSpan(0, 6, 32)
        Console.WriteLine("{0:m\:ss} minutes", ts1)

        Dim ts2 As New TimeSpan(0, 18, 44)
        Console.WriteLine("Elapsed time: {0:m\:ss}", ts2)
        ' The example displays the following output:
        '       6:32 minutes
        '       Elapsed time: 18:44
        ' </Snippet12>
    End Sub

    Public Sub ParseMM()
        ' <Snippet13>
        Dim value As String = "05"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "mm", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       00:05:00           
        ' </Snippet13>
    End Sub

    Private Sub mmSpecifier()
        ' <Snippet14>
        Dim departTime As New TimeSpan(11, 12, 00)
        Dim arriveTime As New TimeSpan(16, 28, 00)
        Console.WriteLine("Travel time: {0:hh\:mm}",
                          arriveTime - departTime)
        ' The example displays the following output:
        '       Travel time: 05:16      
        ' </Snippet14>
    End Sub

    Private Sub sSpecifier()
        ' <Snippet15>
        Dim ts As TimeSpan = TimeSpan.FromSeconds(12.465)
        Console.WriteLine(ts.ToString("%s"))
        ' The example displays the following output:
        '       12
        ' </Snippet15>
        Console.WriteLine()

        ' <Snippet16>
        Dim startTime As New TimeSpan(0, 12, 30, 15, 0)
        Dim endTime As New TimeSpan(0, 12, 30, 21, 3)
        Console.WriteLine("Elapsed Time: {0:s\:fff} seconds",
                          endTime - startTime)
        ' The example displays the following output:
        '       Elapsed Time: 6:003 seconds      
        ' </Snippet16>
    End Sub

    Public Sub ParseS()
        ' <Snippet17>
        Dim value As String = "9"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "%s", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       00:00:09
        ' </Snippet17>
    End Sub

    Public Sub ParseSS()
        ' <Snippet18>
        Dim values() As String = {"49", "9", "06"}
        Dim interval As TimeSpan
        For Each value As String In values
            If TimeSpan.TryParseExact(value, "ss", Nothing, interval) Then
                Console.WriteLine(interval.ToString("c"))
            Else
                Console.WriteLine("Unable to convert '{0}' to a time interval",
                                  value)
            End If
        Next
        ' The example displays the following output:
        '       00:00:49
        '       Unable to convert '9' to a time interval
        '       00:00:06
        ' </Snippet18>
    End Sub

    Private Sub ssSpecifier()
        ' <Snippet19>
        Dim interval1 As TimeSpan = TimeSpan.FromSeconds(12.60)
        Console.WriteLine(interval1.ToString("ss\.fff"))
        Dim interval2 As TimeSpan = TimeSpan.FromSeconds(6.485)
        Console.WriteLine(interval2.ToString("ss\.fff"))
        ' The example displays the following output:
        '       12.600
        '       06.485
        ' </Snippet19>
    End Sub
End Module

Ordinarily, in a parsing operation, an input string that includes only a single number is interpreted as the number of days. You can use the "%m" custom format specifier instead to interpret the numeric string as the number of minutes. The following example provides an illustration.

sample · csharpsamples/snippets/csharp/VS_Snippets_CLR/conceptual.timespan.custom/cs/customexamples1.cs11

csharp
using System;

public class Example
{
   public static void Main()
   {
      dSpecifier();
      Console.WriteLine("-------");
      ddSpecifier();
      Console.WriteLine("-------");
      hSpecifier();
      Console.WriteLine("-------");
      ParseH();
      Console.WriteLine("-------");
      ParseHH();
      Console.WriteLine("-------");
      hhSpecifier();
      Console.WriteLine("-------");
      ParseM();
      Console.WriteLine("-------");
      mSpecifier();
      Console.WriteLine("-------");
      ParseMM();
      Console.WriteLine("-------");
      mmSpecifier();
      Console.WriteLine("-------");
      sSpecifier();
      Console.WriteLine("-------");
      ParseS();
      Console.WriteLine("-------");
      ParseSS();
      Console.WriteLine("-------");
      ssSpecifier();
      Console.WriteLine("-------");
   }

   private static void dSpecifier()
   {
      // <Snippet3>
      TimeSpan ts1 = new TimeSpan(16, 4, 3, 17, 250);
      Console.WriteLine(ts1.ToString("%d"));
      // Displays 16
      // </Snippet3>
      Console.WriteLine();

      // <Snippet4>
      TimeSpan ts2 = new TimeSpan(4, 3, 17);
      Console.WriteLine(ts2.ToString(@"d\.hh\:mm\:ss"));

      TimeSpan ts3 = new TimeSpan(3, 4, 3, 17);
      Console.WriteLine(ts3.ToString(@"d\.hh\:mm\:ss"));
      // The example displays the following output:
      //       0.04:03:17
      //       3.04:03:17
      // </Snippet4>
   }

   private static void ddSpecifier()
   {
      // <Snippet5>
      TimeSpan ts1 = new TimeSpan(0, 23, 17, 47);
      TimeSpan ts2 = new TimeSpan(365, 21, 19, 45);

      for (int ctr = 2; ctr <= 8; ctr++)
      {
         string fmt = new String('d', ctr) + @"\.hh\:mm\:ss";
         Console.WriteLine($"{fmt} --> {ts1.ToString(fmt)}");
         Console.WriteLine($"{fmt} --> {ts2.ToString(fmt)}");
         Console.WriteLine();
      }
      // The example displays the following output:
      //       dd\.hh\:mm\:ss --> 00.23:17:47
      //       dd\.hh\:mm\:ss --> 365.21:19:45
      //
      //       ddd\.hh\:mm\:ss --> 000.23:17:47
      //       ddd\.hh\:mm\:ss --> 365.21:19:45
      //
      //       dddd\.hh\:mm\:ss --> 0000.23:17:47
      //       dddd\.hh\:mm\:ss --> 0365.21:19:45
      //
      //       ddddd\.hh\:mm\:ss --> 00000.23:17:47
      //       ddddd\.hh\:mm\:ss --> 00365.21:19:45
      //
      //       dddddd\.hh\:mm\:ss --> 000000.23:17:47
      //       dddddd\.hh\:mm\:ss --> 000365.21:19:45
      //
      //       ddddddd\.hh\:mm\:ss --> 0000000.23:17:47
      //       ddddddd\.hh\:mm\:ss --> 0000365.21:19:45
      //
      //       dddddddd\.hh\:mm\:ss --> 00000000.23:17:47
      //       dddddddd\.hh\:mm\:ss --> 00000365.21:19:45
      // </Snippet5>
   }

   private static void hSpecifier()
   {
      // <Snippet6>
      TimeSpan ts = new TimeSpan(3, 42, 0);
      Console.WriteLine($"{ts:%h} hours {ts:%m} minutes");
      // The example displays the following output:
      //       3 hours 42 minutes
      // </Snippet6>

      // <Snippet7>
      TimeSpan ts1 = new TimeSpan(14, 3, 17);
      Console.WriteLine(ts1.ToString(@"d\.h\:mm\:ss"));

      TimeSpan ts2 = new TimeSpan(3, 4, 3, 17);
      Console.WriteLine(ts2.ToString(@"d\.h\:mm\:ss"));
      // The example displays the following output:
      //       0.14:03:17
      //       3.4:03:17
      // </Snippet7>
   }

   private static void ParseH()
   {
      // <Snippet8>
      string value = "8";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "%h", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       08:00:00
      // </Snippet8>
   }

   private static void ParseHH()
   {
      // <Snippet9>
      string value = "08";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "hh", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       08:00:00
      // </Snippet9>
   }

   private static void hhSpecifier()
   {
      // <Snippet10>
      TimeSpan ts1 = new TimeSpan(14, 3, 17);
      Console.WriteLine(ts1.ToString(@"d\.hh\:mm\:ss"));

      TimeSpan ts2 = new TimeSpan(3, 4, 3, 17);
      Console.WriteLine(ts2.ToString(@"d\.hh\:mm\:ss"));
      // The example displays the following output:
      //       0.14:03:17
      //       3.04:03:17
      // </Snippet10>
   }

   private static void ParseM()
   {
      // <Snippet11>
      string value = "3";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "%m", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       00:03:00
      // </Snippet11>
   }

   private static void mSpecifier()
   {
      // <Snippet12>
      TimeSpan ts1 = new TimeSpan(0, 6, 32);
      Console.WriteLine($"{ts1:m\\:ss} minutes");

      TimeSpan ts2 = new TimeSpan(3, 4, 3, 17);
      Console.WriteLine($"Elapsed time: {ts2:m\\:ss}");
      // The example displays the following output:
      //       6:32 minutes
      //       Elapsed time: 18:44
      // </Snippet12>
   }

   private static void ParseMM()
   {
      // <Snippet13>
      string value = "07";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "mm", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       00:07:00
      // </Snippet13>
   }

   private static void mmSpecifier()
   {
      // <Snippet14>
      TimeSpan departTime = new TimeSpan(11, 12, 00);
      TimeSpan arriveTime = new TimeSpan(16, 28, 00);
      Console.WriteLine($"Travel time: {arriveTime - departTime:hh\\:mm}");
      // The example displays the following output:
      //       Travel time: 05:16
      // </Snippet14>
   }

   private static void sSpecifier()
   {
      // <Snippet15>
      TimeSpan ts = TimeSpan.FromSeconds(12.465);
      Console.WriteLine(ts.ToString("%s"));
      // The example displays the following output:
      //       12
      // </Snippet15>
      Console.WriteLine();

      // <Snippet16>
      TimeSpan startTime = new TimeSpan(0, 12, 30, 15, 0);
      TimeSpan endTime = new TimeSpan(0, 12, 30, 21, 3);
      Console.WriteLine(@"Elapsed Time: {0:s\:fff} seconds",
                        endTime - startTime);
      // The example displays the following output:
      //       Elapsed Time: 6:003 seconds
      // </Snippet16>
   }

   private static void ParseS()
   {
      // <Snippet17>
      string value = "9";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "%s", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       00:00:09
      // </Snippet17>
   }

   private static void ParseSS()
   {
      // <Snippet18>
      string[] values = { "49", "9", "06" };
      TimeSpan interval;
      foreach (string value in values)
      {
         if (TimeSpan.TryParseExact(value, "ss", null, out interval))
            Console.WriteLine(interval.ToString("c"));
         else
            Console.WriteLine($"Unable to convert '{value}' to a time interval");
      }
      // The example displays the following output:
      //       00:00:49
      //       Unable to convert '9' to a time interval
      //       00:00:06
      // </Snippet18>
   }

   private static void ssSpecifier()
   {
      // <Snippet19>
      TimeSpan interval1 = TimeSpan.FromSeconds(12.60);
      Console.WriteLine(interval1.ToString(@"ss\.fff"));

      TimeSpan interval2 = TimeSpan.FromSeconds(6.485);
      Console.WriteLine(interval2.ToString(@"ss\.fff"));
      // The example displays the following output:
      //       12.600
      //       06.485
      // </Snippet19>
   }
}

sample · vbsamples/snippets/visualbasic/VS_Snippets_CLR/conceptual.timespan.custom/vb/customexamples1.vb11

vb
' Visual Basic .NET Document
Option Strict On

Module Example
    Public Sub Main()
        dSpecifier()
        Console.WriteLine("-------")
        ddSpecifier()
        Console.WriteLine("-------")
        hSpecifier()
        Console.WriteLine("-------")
        ParseH()
        Console.WriteLine("-------")
        ParseHH()
        Console.WriteLine("-------")
        hhSpecifier()
        Console.WriteLine("-------")
        ParseM()
        Console.WriteLine("-------")
        mSpecifier()
        Console.WriteLine("-------")
        ParseMM()
        Console.WriteLine("-------")
        mmSpecifier()
        Console.WriteLine("-------")
        sSpecifier()
        Console.WriteLine("-------")
        ParseS()
        Console.WriteLine("-------")
        ParseSS()
        Console.WriteLine("-------")
        ssSpecifier()
        Console.WriteLine("-------")
    End Sub

    Private Sub dSpecifier()
        ' <Snippet3>
        Dim ts As New TimeSpan(16, 4, 3, 17, 250)
        Console.WriteLine(ts.ToString("%d"))
        ' Displays 16   
        ' </Snippet3>
        Console.WriteLine()

        ' <Snippet4>
        Dim ts2 As New TimeSpan(4, 3, 17)
        Console.WriteLine(ts2.ToString("d\.hh\:mm\:ss"))

        Dim ts3 As New TimeSpan(3, 4, 3, 17)
        Console.WriteLine(ts3.ToString("d\.hh\:mm\:ss"))
        ' The example displays the following output:
        '       0.04:03:17
        '       3.04:03:17      
        ' </Snippet4>
    End Sub

    Private Sub ddSpecifier()
        ' <Snippet5>
        Dim ts1 As New TimeSpan(0, 23, 17, 47)
        Dim ts2 As New TimeSpan(365, 21, 19, 45)

        For ctr As Integer = 2 To 8
            Dim fmt As String = New String("d"c, ctr) + "\.hh\:mm\:ss"
            Console.WriteLine("{0} --> {1:" + fmt + "}", fmt, ts1)
            Console.WriteLine("{0} --> {1:" + fmt + "}", fmt, ts2)
            Console.WriteLine()
        Next
        ' The example displays the following output:
        '       dd\.hh\:mm\:ss --> 00.23:17:47
        '       dd\.hh\:mm\:ss --> 365.21:19:45
        '       
        '       ddd\.hh\:mm\:ss --> 000.23:17:47
        '       ddd\.hh\:mm\:ss --> 365.21:19:45
        '       
        '       dddd\.hh\:mm\:ss --> 0000.23:17:47
        '       dddd\.hh\:mm\:ss --> 0365.21:19:45
        '       
        '       ddddd\.hh\:mm\:ss --> 00000.23:17:47
        '       ddddd\.hh\:mm\:ss --> 00365.21:19:45
        '       
        '       dddddd\.hh\:mm\:ss --> 000000.23:17:47
        '       dddddd\.hh\:mm\:ss --> 000365.21:19:45
        '       
        '       ddddddd\.hh\:mm\:ss --> 0000000.23:17:47
        '       ddddddd\.hh\:mm\:ss --> 0000365.21:19:45
        '       
        '       dddddddd\.hh\:mm\:ss --> 00000000.23:17:47
        '       dddddddd\.hh\:mm\:ss --> 00000365.21:19:45      
        ' </Snippet5>
    End Sub

    Private Sub hSpecifier()
        ' <Snippet6>
        Dim ts As New TimeSpan(3, 42, 0)
        Console.WriteLine("{0:%h} hours {0:%m} minutes", ts)
        ' The example displays the following output:
        '       3 hours 42 minutes
        ' </Snippet6>
        Console.WriteLine()

        ' <Snippet7>
        Dim ts1 As New TimeSpan(14, 3, 17)
        Console.WriteLine(ts1.ToString("d\.h\:mm\:ss"))

        Dim ts2 As New TimeSpan(3, 4, 3, 17)
        Console.WriteLine(ts2.ToString("d\.h\:mm\:ss"))
        ' The example displays the following output:
        '       0.14:03:17
        '       3.4:03:17
        ' </Snippet7>
    End Sub

    Public Sub ParseH()
        ' <Snippet8>
        Dim value As String = "8"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "%h", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       08:00:00                              
        ' </Snippet8>
    End Sub

    Public Sub ParseHH()
        ' <Snippet9>
        Dim value As String = "08"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "hh", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       08:00:00                              
        ' </Snippet9>
    End Sub

    Private Sub hhSpecifier()
        ' <Snippet10>
        Dim ts1 As New TimeSpan(14, 3, 17)
        Console.WriteLine(ts1.ToString("d\.hh\:mm\:ss"))

        Dim ts2 As New TimeSpan(3, 4, 3, 17)
        Console.WriteLine(ts2.ToString("d\.hh\:mm\:ss"))
        ' The example displays the following output:
        '       0.14:03:17
        '       3.04:03:17
        ' </Snippet10>
    End Sub

    Public Sub ParseM()
        ' <Snippet11>
        Dim value As String = "3"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "%m", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       00:03:00                              
        ' </Snippet11>
    End Sub

    Private Sub mSpecifier()
        ' <Snippet12>
        Dim ts1 As New TimeSpan(0, 6, 32)
        Console.WriteLine("{0:m\:ss} minutes", ts1)

        Dim ts2 As New TimeSpan(0, 18, 44)
        Console.WriteLine("Elapsed time: {0:m\:ss}", ts2)
        ' The example displays the following output:
        '       6:32 minutes
        '       Elapsed time: 18:44
        ' </Snippet12>
    End Sub

    Public Sub ParseMM()
        ' <Snippet13>
        Dim value As String = "05"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "mm", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       00:05:00           
        ' </Snippet13>
    End Sub

    Private Sub mmSpecifier()
        ' <Snippet14>
        Dim departTime As New TimeSpan(11, 12, 00)
        Dim arriveTime As New TimeSpan(16, 28, 00)
        Console.WriteLine("Travel time: {0:hh\:mm}",
                          arriveTime - departTime)
        ' The example displays the following output:
        '       Travel time: 05:16      
        ' </Snippet14>
    End Sub

    Private Sub sSpecifier()
        ' <Snippet15>
        Dim ts As TimeSpan = TimeSpan.FromSeconds(12.465)
        Console.WriteLine(ts.ToString("%s"))
        ' The example displays the following output:
        '       12
        ' </Snippet15>
        Console.WriteLine()

        ' <Snippet16>
        Dim startTime As New TimeSpan(0, 12, 30, 15, 0)
        Dim endTime As New TimeSpan(0, 12, 30, 21, 3)
        Console.WriteLine("Elapsed Time: {0:s\:fff} seconds",
                          endTime - startTime)
        ' The example displays the following output:
        '       Elapsed Time: 6:003 seconds      
        ' </Snippet16>
    End Sub

    Public Sub ParseS()
        ' <Snippet17>
        Dim value As String = "9"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "%s", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       00:00:09
        ' </Snippet17>
    End Sub

    Public Sub ParseSS()
        ' <Snippet18>
        Dim values() As String = {"49", "9", "06"}
        Dim interval As TimeSpan
        For Each value As String In values
            If TimeSpan.TryParseExact(value, "ss", Nothing, interval) Then
                Console.WriteLine(interval.ToString("c"))
            Else
                Console.WriteLine("Unable to convert '{0}' to a time interval",
                                  value)
            End If
        Next
        ' The example displays the following output:
        '       00:00:49
        '       Unable to convert '9' to a time interval
        '       00:00:06
        ' </Snippet18>
    End Sub

    Private Sub ssSpecifier()
        ' <Snippet19>
        Dim interval1 As TimeSpan = TimeSpan.FromSeconds(12.60)
        Console.WriteLine(interval1.ToString("ss\.fff"))
        Dim interval2 As TimeSpan = TimeSpan.FromSeconds(6.485)
        Console.WriteLine(interval2.ToString("ss\.fff"))
        ' The example displays the following output:
        '       12.600
        '       06.485
        ' </Snippet19>
    End Sub
End Module

The following example illustrates the use of the "m" custom format specifier.

sample · csharpsamples/snippets/csharp/VS_Snippets_CLR/conceptual.timespan.custom/cs/customexamples1.cs12

csharp
using System;

public class Example
{
   public static void Main()
   {
      dSpecifier();
      Console.WriteLine("-------");
      ddSpecifier();
      Console.WriteLine("-------");
      hSpecifier();
      Console.WriteLine("-------");
      ParseH();
      Console.WriteLine("-------");
      ParseHH();
      Console.WriteLine("-------");
      hhSpecifier();
      Console.WriteLine("-------");
      ParseM();
      Console.WriteLine("-------");
      mSpecifier();
      Console.WriteLine("-------");
      ParseMM();
      Console.WriteLine("-------");
      mmSpecifier();
      Console.WriteLine("-------");
      sSpecifier();
      Console.WriteLine("-------");
      ParseS();
      Console.WriteLine("-------");
      ParseSS();
      Console.WriteLine("-------");
      ssSpecifier();
      Console.WriteLine("-------");
   }

   private static void dSpecifier()
   {
      // <Snippet3>
      TimeSpan ts1 = new TimeSpan(16, 4, 3, 17, 250);
      Console.WriteLine(ts1.ToString("%d"));
      // Displays 16
      // </Snippet3>
      Console.WriteLine();

      // <Snippet4>
      TimeSpan ts2 = new TimeSpan(4, 3, 17);
      Console.WriteLine(ts2.ToString(@"d\.hh\:mm\:ss"));

      TimeSpan ts3 = new TimeSpan(3, 4, 3, 17);
      Console.WriteLine(ts3.ToString(@"d\.hh\:mm\:ss"));
      // The example displays the following output:
      //       0.04:03:17
      //       3.04:03:17
      // </Snippet4>
   }

   private static void ddSpecifier()
   {
      // <Snippet5>
      TimeSpan ts1 = new TimeSpan(0, 23, 17, 47);
      TimeSpan ts2 = new TimeSpan(365, 21, 19, 45);

      for (int ctr = 2; ctr <= 8; ctr++)
      {
         string fmt = new String('d', ctr) + @"\.hh\:mm\:ss";
         Console.WriteLine($"{fmt} --> {ts1.ToString(fmt)}");
         Console.WriteLine($"{fmt} --> {ts2.ToString(fmt)}");
         Console.WriteLine();
      }
      // The example displays the following output:
      //       dd\.hh\:mm\:ss --> 00.23:17:47
      //       dd\.hh\:mm\:ss --> 365.21:19:45
      //
      //       ddd\.hh\:mm\:ss --> 000.23:17:47
      //       ddd\.hh\:mm\:ss --> 365.21:19:45
      //
      //       dddd\.hh\:mm\:ss --> 0000.23:17:47
      //       dddd\.hh\:mm\:ss --> 0365.21:19:45
      //
      //       ddddd\.hh\:mm\:ss --> 00000.23:17:47
      //       ddddd\.hh\:mm\:ss --> 00365.21:19:45
      //
      //       dddddd\.hh\:mm\:ss --> 000000.23:17:47
      //       dddddd\.hh\:mm\:ss --> 000365.21:19:45
      //
      //       ddddddd\.hh\:mm\:ss --> 0000000.23:17:47
      //       ddddddd\.hh\:mm\:ss --> 0000365.21:19:45
      //
      //       dddddddd\.hh\:mm\:ss --> 00000000.23:17:47
      //       dddddddd\.hh\:mm\:ss --> 00000365.21:19:45
      // </Snippet5>
   }

   private static void hSpecifier()
   {
      // <Snippet6>
      TimeSpan ts = new TimeSpan(3, 42, 0);
      Console.WriteLine($"{ts:%h} hours {ts:%m} minutes");
      // The example displays the following output:
      //       3 hours 42 minutes
      // </Snippet6>

      // <Snippet7>
      TimeSpan ts1 = new TimeSpan(14, 3, 17);
      Console.WriteLine(ts1.ToString(@"d\.h\:mm\:ss"));

      TimeSpan ts2 = new TimeSpan(3, 4, 3, 17);
      Console.WriteLine(ts2.ToString(@"d\.h\:mm\:ss"));
      // The example displays the following output:
      //       0.14:03:17
      //       3.4:03:17
      // </Snippet7>
   }

   private static void ParseH()
   {
      // <Snippet8>
      string value = "8";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "%h", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       08:00:00
      // </Snippet8>
   }

   private static void ParseHH()
   {
      // <Snippet9>
      string value = "08";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "hh", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       08:00:00
      // </Snippet9>
   }

   private static void hhSpecifier()
   {
      // <Snippet10>
      TimeSpan ts1 = new TimeSpan(14, 3, 17);
      Console.WriteLine(ts1.ToString(@"d\.hh\:mm\:ss"));

      TimeSpan ts2 = new TimeSpan(3, 4, 3, 17);
      Console.WriteLine(ts2.ToString(@"d\.hh\:mm\:ss"));
      // The example displays the following output:
      //       0.14:03:17
      //       3.04:03:17
      // </Snippet10>
   }

   private static void ParseM()
   {
      // <Snippet11>
      string value = "3";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "%m", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       00:03:00
      // </Snippet11>
   }

   private static void mSpecifier()
   {
      // <Snippet12>
      TimeSpan ts1 = new TimeSpan(0, 6, 32);
      Console.WriteLine($"{ts1:m\\:ss} minutes");

      TimeSpan ts2 = new TimeSpan(3, 4, 3, 17);
      Console.WriteLine($"Elapsed time: {ts2:m\\:ss}");
      // The example displays the following output:
      //       6:32 minutes
      //       Elapsed time: 18:44
      // </Snippet12>
   }

   private static void ParseMM()
   {
      // <Snippet13>
      string value = "07";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "mm", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       00:07:00
      // </Snippet13>
   }

   private static void mmSpecifier()
   {
      // <Snippet14>
      TimeSpan departTime = new TimeSpan(11, 12, 00);
      TimeSpan arriveTime = new TimeSpan(16, 28, 00);
      Console.WriteLine($"Travel time: {arriveTime - departTime:hh\\:mm}");
      // The example displays the following output:
      //       Travel time: 05:16
      // </Snippet14>
   }

   private static void sSpecifier()
   {
      // <Snippet15>
      TimeSpan ts = TimeSpan.FromSeconds(12.465);
      Console.WriteLine(ts.ToString("%s"));
      // The example displays the following output:
      //       12
      // </Snippet15>
      Console.WriteLine();

      // <Snippet16>
      TimeSpan startTime = new TimeSpan(0, 12, 30, 15, 0);
      TimeSpan endTime = new TimeSpan(0, 12, 30, 21, 3);
      Console.WriteLine(@"Elapsed Time: {0:s\:fff} seconds",
                        endTime - startTime);
      // The example displays the following output:
      //       Elapsed Time: 6:003 seconds
      // </Snippet16>
   }

   private static void ParseS()
   {
      // <Snippet17>
      string value = "9";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "%s", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       00:00:09
      // </Snippet17>
   }

   private static void ParseSS()
   {
      // <Snippet18>
      string[] values = { "49", "9", "06" };
      TimeSpan interval;
      foreach (string value in values)
      {
         if (TimeSpan.TryParseExact(value, "ss", null, out interval))
            Console.WriteLine(interval.ToString("c"));
         else
            Console.WriteLine($"Unable to convert '{value}' to a time interval");
      }
      // The example displays the following output:
      //       00:00:49
      //       Unable to convert '9' to a time interval
      //       00:00:06
      // </Snippet18>
   }

   private static void ssSpecifier()
   {
      // <Snippet19>
      TimeSpan interval1 = TimeSpan.FromSeconds(12.60);
      Console.WriteLine(interval1.ToString(@"ss\.fff"));

      TimeSpan interval2 = TimeSpan.FromSeconds(6.485);
      Console.WriteLine(interval2.ToString(@"ss\.fff"));
      // The example displays the following output:
      //       12.600
      //       06.485
      // </Snippet19>
   }
}

sample · vbsamples/snippets/visualbasic/VS_Snippets_CLR/conceptual.timespan.custom/vb/customexamples1.vb12

vb
' Visual Basic .NET Document
Option Strict On

Module Example
    Public Sub Main()
        dSpecifier()
        Console.WriteLine("-------")
        ddSpecifier()
        Console.WriteLine("-------")
        hSpecifier()
        Console.WriteLine("-------")
        ParseH()
        Console.WriteLine("-------")
        ParseHH()
        Console.WriteLine("-------")
        hhSpecifier()
        Console.WriteLine("-------")
        ParseM()
        Console.WriteLine("-------")
        mSpecifier()
        Console.WriteLine("-------")
        ParseMM()
        Console.WriteLine("-------")
        mmSpecifier()
        Console.WriteLine("-------")
        sSpecifier()
        Console.WriteLine("-------")
        ParseS()
        Console.WriteLine("-------")
        ParseSS()
        Console.WriteLine("-------")
        ssSpecifier()
        Console.WriteLine("-------")
    End Sub

    Private Sub dSpecifier()
        ' <Snippet3>
        Dim ts As New TimeSpan(16, 4, 3, 17, 250)
        Console.WriteLine(ts.ToString("%d"))
        ' Displays 16   
        ' </Snippet3>
        Console.WriteLine()

        ' <Snippet4>
        Dim ts2 As New TimeSpan(4, 3, 17)
        Console.WriteLine(ts2.ToString("d\.hh\:mm\:ss"))

        Dim ts3 As New TimeSpan(3, 4, 3, 17)
        Console.WriteLine(ts3.ToString("d\.hh\:mm\:ss"))
        ' The example displays the following output:
        '       0.04:03:17
        '       3.04:03:17      
        ' </Snippet4>
    End Sub

    Private Sub ddSpecifier()
        ' <Snippet5>
        Dim ts1 As New TimeSpan(0, 23, 17, 47)
        Dim ts2 As New TimeSpan(365, 21, 19, 45)

        For ctr As Integer = 2 To 8
            Dim fmt As String = New String("d"c, ctr) + "\.hh\:mm\:ss"
            Console.WriteLine("{0} --> {1:" + fmt + "}", fmt, ts1)
            Console.WriteLine("{0} --> {1:" + fmt + "}", fmt, ts2)
            Console.WriteLine()
        Next
        ' The example displays the following output:
        '       dd\.hh\:mm\:ss --> 00.23:17:47
        '       dd\.hh\:mm\:ss --> 365.21:19:45
        '       
        '       ddd\.hh\:mm\:ss --> 000.23:17:47
        '       ddd\.hh\:mm\:ss --> 365.21:19:45
        '       
        '       dddd\.hh\:mm\:ss --> 0000.23:17:47
        '       dddd\.hh\:mm\:ss --> 0365.21:19:45
        '       
        '       ddddd\.hh\:mm\:ss --> 00000.23:17:47
        '       ddddd\.hh\:mm\:ss --> 00365.21:19:45
        '       
        '       dddddd\.hh\:mm\:ss --> 000000.23:17:47
        '       dddddd\.hh\:mm\:ss --> 000365.21:19:45
        '       
        '       ddddddd\.hh\:mm\:ss --> 0000000.23:17:47
        '       ddddddd\.hh\:mm\:ss --> 0000365.21:19:45
        '       
        '       dddddddd\.hh\:mm\:ss --> 00000000.23:17:47
        '       dddddddd\.hh\:mm\:ss --> 00000365.21:19:45      
        ' </Snippet5>
    End Sub

    Private Sub hSpecifier()
        ' <Snippet6>
        Dim ts As New TimeSpan(3, 42, 0)
        Console.WriteLine("{0:%h} hours {0:%m} minutes", ts)
        ' The example displays the following output:
        '       3 hours 42 minutes
        ' </Snippet6>
        Console.WriteLine()

        ' <Snippet7>
        Dim ts1 As New TimeSpan(14, 3, 17)
        Console.WriteLine(ts1.ToString("d\.h\:mm\:ss"))

        Dim ts2 As New TimeSpan(3, 4, 3, 17)
        Console.WriteLine(ts2.ToString("d\.h\:mm\:ss"))
        ' The example displays the following output:
        '       0.14:03:17
        '       3.4:03:17
        ' </Snippet7>
    End Sub

    Public Sub ParseH()
        ' <Snippet8>
        Dim value As String = "8"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "%h", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       08:00:00                              
        ' </Snippet8>
    End Sub

    Public Sub ParseHH()
        ' <Snippet9>
        Dim value As String = "08"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "hh", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       08:00:00                              
        ' </Snippet9>
    End Sub

    Private Sub hhSpecifier()
        ' <Snippet10>
        Dim ts1 As New TimeSpan(14, 3, 17)
        Console.WriteLine(ts1.ToString("d\.hh\:mm\:ss"))

        Dim ts2 As New TimeSpan(3, 4, 3, 17)
        Console.WriteLine(ts2.ToString("d\.hh\:mm\:ss"))
        ' The example displays the following output:
        '       0.14:03:17
        '       3.04:03:17
        ' </Snippet10>
    End Sub

    Public Sub ParseM()
        ' <Snippet11>
        Dim value As String = "3"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "%m", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       00:03:00                              
        ' </Snippet11>
    End Sub

    Private Sub mSpecifier()
        ' <Snippet12>
        Dim ts1 As New TimeSpan(0, 6, 32)
        Console.WriteLine("{0:m\:ss} minutes", ts1)

        Dim ts2 As New TimeSpan(0, 18, 44)
        Console.WriteLine("Elapsed time: {0:m\:ss}", ts2)
        ' The example displays the following output:
        '       6:32 minutes
        '       Elapsed time: 18:44
        ' </Snippet12>
    End Sub

    Public Sub ParseMM()
        ' <Snippet13>
        Dim value As String = "05"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "mm", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       00:05:00           
        ' </Snippet13>
    End Sub

    Private Sub mmSpecifier()
        ' <Snippet14>
        Dim departTime As New TimeSpan(11, 12, 00)
        Dim arriveTime As New TimeSpan(16, 28, 00)
        Console.WriteLine("Travel time: {0:hh\:mm}",
                          arriveTime - departTime)
        ' The example displays the following output:
        '       Travel time: 05:16      
        ' </Snippet14>
    End Sub

    Private Sub sSpecifier()
        ' <Snippet15>
        Dim ts As TimeSpan = TimeSpan.FromSeconds(12.465)
        Console.WriteLine(ts.ToString("%s"))
        ' The example displays the following output:
        '       12
        ' </Snippet15>
        Console.WriteLine()

        ' <Snippet16>
        Dim startTime As New TimeSpan(0, 12, 30, 15, 0)
        Dim endTime As New TimeSpan(0, 12, 30, 21, 3)
        Console.WriteLine("Elapsed Time: {0:s\:fff} seconds",
                          endTime - startTime)
        ' The example displays the following output:
        '       Elapsed Time: 6:003 seconds      
        ' </Snippet16>
    End Sub

    Public Sub ParseS()
        ' <Snippet17>
        Dim value As String = "9"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "%s", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       00:00:09
        ' </Snippet17>
    End Sub

    Public Sub ParseSS()
        ' <Snippet18>
        Dim values() As String = {"49", "9", "06"}
        Dim interval As TimeSpan
        For Each value As String In values
            If TimeSpan.TryParseExact(value, "ss", Nothing, interval) Then
                Console.WriteLine(interval.ToString("c"))
            Else
                Console.WriteLine("Unable to convert '{0}' to a time interval",
                                  value)
            End If
        Next
        ' The example displays the following output:
        '       00:00:49
        '       Unable to convert '9' to a time interval
        '       00:00:06
        ' </Snippet18>
    End Sub

    Private Sub ssSpecifier()
        ' <Snippet19>
        Dim interval1 As TimeSpan = TimeSpan.FromSeconds(12.60)
        Console.WriteLine(interval1.ToString("ss\.fff"))
        Dim interval2 As TimeSpan = TimeSpan.FromSeconds(6.485)
        Console.WriteLine(interval2.ToString("ss\.fff"))
        ' The example displays the following output:
        '       12.600
        '       06.485
        ' </Snippet19>
    End Sub
End Module

Back to table

The "mm" custom format specifier

The "mm" custom format specifier outputs the value of the System.TimeSpan.Minutes property, which represents the number of whole minutes in the time interval that isn't included as part of its hours or days component. For values from 0 through 9, the output string includes a leading zero.

Ordinarily, in a parsing operation, an input string that includes only a single number is interpreted as the number of days. You can use the "mm" custom format specifier instead to interpret the numeric string as the number of minutes. The following example provides an illustration.

sample · csharpsamples/snippets/csharp/VS_Snippets_CLR/conceptual.timespan.custom/cs/customexamples1.cs13

csharp
using System;

public class Example
{
   public static void Main()
   {
      dSpecifier();
      Console.WriteLine("-------");
      ddSpecifier();
      Console.WriteLine("-------");
      hSpecifier();
      Console.WriteLine("-------");
      ParseH();
      Console.WriteLine("-------");
      ParseHH();
      Console.WriteLine("-------");
      hhSpecifier();
      Console.WriteLine("-------");
      ParseM();
      Console.WriteLine("-------");
      mSpecifier();
      Console.WriteLine("-------");
      ParseMM();
      Console.WriteLine("-------");
      mmSpecifier();
      Console.WriteLine("-------");
      sSpecifier();
      Console.WriteLine("-------");
      ParseS();
      Console.WriteLine("-------");
      ParseSS();
      Console.WriteLine("-------");
      ssSpecifier();
      Console.WriteLine("-------");
   }

   private static void dSpecifier()
   {
      // <Snippet3>
      TimeSpan ts1 = new TimeSpan(16, 4, 3, 17, 250);
      Console.WriteLine(ts1.ToString("%d"));
      // Displays 16
      // </Snippet3>
      Console.WriteLine();

      // <Snippet4>
      TimeSpan ts2 = new TimeSpan(4, 3, 17);
      Console.WriteLine(ts2.ToString(@"d\.hh\:mm\:ss"));

      TimeSpan ts3 = new TimeSpan(3, 4, 3, 17);
      Console.WriteLine(ts3.ToString(@"d\.hh\:mm\:ss"));
      // The example displays the following output:
      //       0.04:03:17
      //       3.04:03:17
      // </Snippet4>
   }

   private static void ddSpecifier()
   {
      // <Snippet5>
      TimeSpan ts1 = new TimeSpan(0, 23, 17, 47);
      TimeSpan ts2 = new TimeSpan(365, 21, 19, 45);

      for (int ctr = 2; ctr <= 8; ctr++)
      {
         string fmt = new String('d', ctr) + @"\.hh\:mm\:ss";
         Console.WriteLine($"{fmt} --> {ts1.ToString(fmt)}");
         Console.WriteLine($"{fmt} --> {ts2.ToString(fmt)}");
         Console.WriteLine();
      }
      // The example displays the following output:
      //       dd\.hh\:mm\:ss --> 00.23:17:47
      //       dd\.hh\:mm\:ss --> 365.21:19:45
      //
      //       ddd\.hh\:mm\:ss --> 000.23:17:47
      //       ddd\.hh\:mm\:ss --> 365.21:19:45
      //
      //       dddd\.hh\:mm\:ss --> 0000.23:17:47
      //       dddd\.hh\:mm\:ss --> 0365.21:19:45
      //
      //       ddddd\.hh\:mm\:ss --> 00000.23:17:47
      //       ddddd\.hh\:mm\:ss --> 00365.21:19:45
      //
      //       dddddd\.hh\:mm\:ss --> 000000.23:17:47
      //       dddddd\.hh\:mm\:ss --> 000365.21:19:45
      //
      //       ddddddd\.hh\:mm\:ss --> 0000000.23:17:47
      //       ddddddd\.hh\:mm\:ss --> 0000365.21:19:45
      //
      //       dddddddd\.hh\:mm\:ss --> 00000000.23:17:47
      //       dddddddd\.hh\:mm\:ss --> 00000365.21:19:45
      // </Snippet5>
   }

   private static void hSpecifier()
   {
      // <Snippet6>
      TimeSpan ts = new TimeSpan(3, 42, 0);
      Console.WriteLine($"{ts:%h} hours {ts:%m} minutes");
      // The example displays the following output:
      //       3 hours 42 minutes
      // </Snippet6>

      // <Snippet7>
      TimeSpan ts1 = new TimeSpan(14, 3, 17);
      Console.WriteLine(ts1.ToString(@"d\.h\:mm\:ss"));

      TimeSpan ts2 = new TimeSpan(3, 4, 3, 17);
      Console.WriteLine(ts2.ToString(@"d\.h\:mm\:ss"));
      // The example displays the following output:
      //       0.14:03:17
      //       3.4:03:17
      // </Snippet7>
   }

   private static void ParseH()
   {
      // <Snippet8>
      string value = "8";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "%h", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       08:00:00
      // </Snippet8>
   }

   private static void ParseHH()
   {
      // <Snippet9>
      string value = "08";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "hh", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       08:00:00
      // </Snippet9>
   }

   private static void hhSpecifier()
   {
      // <Snippet10>
      TimeSpan ts1 = new TimeSpan(14, 3, 17);
      Console.WriteLine(ts1.ToString(@"d\.hh\:mm\:ss"));

      TimeSpan ts2 = new TimeSpan(3, 4, 3, 17);
      Console.WriteLine(ts2.ToString(@"d\.hh\:mm\:ss"));
      // The example displays the following output:
      //       0.14:03:17
      //       3.04:03:17
      // </Snippet10>
   }

   private static void ParseM()
   {
      // <Snippet11>
      string value = "3";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "%m", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       00:03:00
      // </Snippet11>
   }

   private static void mSpecifier()
   {
      // <Snippet12>
      TimeSpan ts1 = new TimeSpan(0, 6, 32);
      Console.WriteLine($"{ts1:m\\:ss} minutes");

      TimeSpan ts2 = new TimeSpan(3, 4, 3, 17);
      Console.WriteLine($"Elapsed time: {ts2:m\\:ss}");
      // The example displays the following output:
      //       6:32 minutes
      //       Elapsed time: 18:44
      // </Snippet12>
   }

   private static void ParseMM()
   {
      // <Snippet13>
      string value = "07";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "mm", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       00:07:00
      // </Snippet13>
   }

   private static void mmSpecifier()
   {
      // <Snippet14>
      TimeSpan departTime = new TimeSpan(11, 12, 00);
      TimeSpan arriveTime = new TimeSpan(16, 28, 00);
      Console.WriteLine($"Travel time: {arriveTime - departTime:hh\\:mm}");
      // The example displays the following output:
      //       Travel time: 05:16
      // </Snippet14>
   }

   private static void sSpecifier()
   {
      // <Snippet15>
      TimeSpan ts = TimeSpan.FromSeconds(12.465);
      Console.WriteLine(ts.ToString("%s"));
      // The example displays the following output:
      //       12
      // </Snippet15>
      Console.WriteLine();

      // <Snippet16>
      TimeSpan startTime = new TimeSpan(0, 12, 30, 15, 0);
      TimeSpan endTime = new TimeSpan(0, 12, 30, 21, 3);
      Console.WriteLine(@"Elapsed Time: {0:s\:fff} seconds",
                        endTime - startTime);
      // The example displays the following output:
      //       Elapsed Time: 6:003 seconds
      // </Snippet16>
   }

   private static void ParseS()
   {
      // <Snippet17>
      string value = "9";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "%s", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       00:00:09
      // </Snippet17>
   }

   private static void ParseSS()
   {
      // <Snippet18>
      string[] values = { "49", "9", "06" };
      TimeSpan interval;
      foreach (string value in values)
      {
         if (TimeSpan.TryParseExact(value, "ss", null, out interval))
            Console.WriteLine(interval.ToString("c"));
         else
            Console.WriteLine($"Unable to convert '{value}' to a time interval");
      }
      // The example displays the following output:
      //       00:00:49
      //       Unable to convert '9' to a time interval
      //       00:00:06
      // </Snippet18>
   }

   private static void ssSpecifier()
   {
      // <Snippet19>
      TimeSpan interval1 = TimeSpan.FromSeconds(12.60);
      Console.WriteLine(interval1.ToString(@"ss\.fff"));

      TimeSpan interval2 = TimeSpan.FromSeconds(6.485);
      Console.WriteLine(interval2.ToString(@"ss\.fff"));
      // The example displays the following output:
      //       12.600
      //       06.485
      // </Snippet19>
   }
}

sample · vbsamples/snippets/visualbasic/VS_Snippets_CLR/conceptual.timespan.custom/vb/customexamples1.vb13

vb
' Visual Basic .NET Document
Option Strict On

Module Example
    Public Sub Main()
        dSpecifier()
        Console.WriteLine("-------")
        ddSpecifier()
        Console.WriteLine("-------")
        hSpecifier()
        Console.WriteLine("-------")
        ParseH()
        Console.WriteLine("-------")
        ParseHH()
        Console.WriteLine("-------")
        hhSpecifier()
        Console.WriteLine("-------")
        ParseM()
        Console.WriteLine("-------")
        mSpecifier()
        Console.WriteLine("-------")
        ParseMM()
        Console.WriteLine("-------")
        mmSpecifier()
        Console.WriteLine("-------")
        sSpecifier()
        Console.WriteLine("-------")
        ParseS()
        Console.WriteLine("-------")
        ParseSS()
        Console.WriteLine("-------")
        ssSpecifier()
        Console.WriteLine("-------")
    End Sub

    Private Sub dSpecifier()
        ' <Snippet3>
        Dim ts As New TimeSpan(16, 4, 3, 17, 250)
        Console.WriteLine(ts.ToString("%d"))
        ' Displays 16   
        ' </Snippet3>
        Console.WriteLine()

        ' <Snippet4>
        Dim ts2 As New TimeSpan(4, 3, 17)
        Console.WriteLine(ts2.ToString("d\.hh\:mm\:ss"))

        Dim ts3 As New TimeSpan(3, 4, 3, 17)
        Console.WriteLine(ts3.ToString("d\.hh\:mm\:ss"))
        ' The example displays the following output:
        '       0.04:03:17
        '       3.04:03:17      
        ' </Snippet4>
    End Sub

    Private Sub ddSpecifier()
        ' <Snippet5>
        Dim ts1 As New TimeSpan(0, 23, 17, 47)
        Dim ts2 As New TimeSpan(365, 21, 19, 45)

        For ctr As Integer = 2 To 8
            Dim fmt As String = New String("d"c, ctr) + "\.hh\:mm\:ss"
            Console.WriteLine("{0} --> {1:" + fmt + "}", fmt, ts1)
            Console.WriteLine("{0} --> {1:" + fmt + "}", fmt, ts2)
            Console.WriteLine()
        Next
        ' The example displays the following output:
        '       dd\.hh\:mm\:ss --> 00.23:17:47
        '       dd\.hh\:mm\:ss --> 365.21:19:45
        '       
        '       ddd\.hh\:mm\:ss --> 000.23:17:47
        '       ddd\.hh\:mm\:ss --> 365.21:19:45
        '       
        '       dddd\.hh\:mm\:ss --> 0000.23:17:47
        '       dddd\.hh\:mm\:ss --> 0365.21:19:45
        '       
        '       ddddd\.hh\:mm\:ss --> 00000.23:17:47
        '       ddddd\.hh\:mm\:ss --> 00365.21:19:45
        '       
        '       dddddd\.hh\:mm\:ss --> 000000.23:17:47
        '       dddddd\.hh\:mm\:ss --> 000365.21:19:45
        '       
        '       ddddddd\.hh\:mm\:ss --> 0000000.23:17:47
        '       ddddddd\.hh\:mm\:ss --> 0000365.21:19:45
        '       
        '       dddddddd\.hh\:mm\:ss --> 00000000.23:17:47
        '       dddddddd\.hh\:mm\:ss --> 00000365.21:19:45      
        ' </Snippet5>
    End Sub

    Private Sub hSpecifier()
        ' <Snippet6>
        Dim ts As New TimeSpan(3, 42, 0)
        Console.WriteLine("{0:%h} hours {0:%m} minutes", ts)
        ' The example displays the following output:
        '       3 hours 42 minutes
        ' </Snippet6>
        Console.WriteLine()

        ' <Snippet7>
        Dim ts1 As New TimeSpan(14, 3, 17)
        Console.WriteLine(ts1.ToString("d\.h\:mm\:ss"))

        Dim ts2 As New TimeSpan(3, 4, 3, 17)
        Console.WriteLine(ts2.ToString("d\.h\:mm\:ss"))
        ' The example displays the following output:
        '       0.14:03:17
        '       3.4:03:17
        ' </Snippet7>
    End Sub

    Public Sub ParseH()
        ' <Snippet8>
        Dim value As String = "8"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "%h", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       08:00:00                              
        ' </Snippet8>
    End Sub

    Public Sub ParseHH()
        ' <Snippet9>
        Dim value As String = "08"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "hh", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       08:00:00                              
        ' </Snippet9>
    End Sub

    Private Sub hhSpecifier()
        ' <Snippet10>
        Dim ts1 As New TimeSpan(14, 3, 17)
        Console.WriteLine(ts1.ToString("d\.hh\:mm\:ss"))

        Dim ts2 As New TimeSpan(3, 4, 3, 17)
        Console.WriteLine(ts2.ToString("d\.hh\:mm\:ss"))
        ' The example displays the following output:
        '       0.14:03:17
        '       3.04:03:17
        ' </Snippet10>
    End Sub

    Public Sub ParseM()
        ' <Snippet11>
        Dim value As String = "3"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "%m", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       00:03:00                              
        ' </Snippet11>
    End Sub

    Private Sub mSpecifier()
        ' <Snippet12>
        Dim ts1 As New TimeSpan(0, 6, 32)
        Console.WriteLine("{0:m\:ss} minutes", ts1)

        Dim ts2 As New TimeSpan(0, 18, 44)
        Console.WriteLine("Elapsed time: {0:m\:ss}", ts2)
        ' The example displays the following output:
        '       6:32 minutes
        '       Elapsed time: 18:44
        ' </Snippet12>
    End Sub

    Public Sub ParseMM()
        ' <Snippet13>
        Dim value As String = "05"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "mm", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       00:05:00           
        ' </Snippet13>
    End Sub

    Private Sub mmSpecifier()
        ' <Snippet14>
        Dim departTime As New TimeSpan(11, 12, 00)
        Dim arriveTime As New TimeSpan(16, 28, 00)
        Console.WriteLine("Travel time: {0:hh\:mm}",
                          arriveTime - departTime)
        ' The example displays the following output:
        '       Travel time: 05:16      
        ' </Snippet14>
    End Sub

    Private Sub sSpecifier()
        ' <Snippet15>
        Dim ts As TimeSpan = TimeSpan.FromSeconds(12.465)
        Console.WriteLine(ts.ToString("%s"))
        ' The example displays the following output:
        '       12
        ' </Snippet15>
        Console.WriteLine()

        ' <Snippet16>
        Dim startTime As New TimeSpan(0, 12, 30, 15, 0)
        Dim endTime As New TimeSpan(0, 12, 30, 21, 3)
        Console.WriteLine("Elapsed Time: {0:s\:fff} seconds",
                          endTime - startTime)
        ' The example displays the following output:
        '       Elapsed Time: 6:003 seconds      
        ' </Snippet16>
    End Sub

    Public Sub ParseS()
        ' <Snippet17>
        Dim value As String = "9"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "%s", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       00:00:09
        ' </Snippet17>
    End Sub

    Public Sub ParseSS()
        ' <Snippet18>
        Dim values() As String = {"49", "9", "06"}
        Dim interval As TimeSpan
        For Each value As String In values
            If TimeSpan.TryParseExact(value, "ss", Nothing, interval) Then
                Console.WriteLine(interval.ToString("c"))
            Else
                Console.WriteLine("Unable to convert '{0}' to a time interval",
                                  value)
            End If
        Next
        ' The example displays the following output:
        '       00:00:49
        '       Unable to convert '9' to a time interval
        '       00:00:06
        ' </Snippet18>
    End Sub

    Private Sub ssSpecifier()
        ' <Snippet19>
        Dim interval1 As TimeSpan = TimeSpan.FromSeconds(12.60)
        Console.WriteLine(interval1.ToString("ss\.fff"))
        Dim interval2 As TimeSpan = TimeSpan.FromSeconds(6.485)
        Console.WriteLine(interval2.ToString("ss\.fff"))
        ' The example displays the following output:
        '       12.600
        '       06.485
        ' </Snippet19>
    End Sub
End Module

The following example illustrates the use of the "mm" custom format specifier.

sample · csharpsamples/snippets/csharp/VS_Snippets_CLR/conceptual.timespan.custom/cs/customexamples1.cs14

csharp
using System;

public class Example
{
   public static void Main()
   {
      dSpecifier();
      Console.WriteLine("-------");
      ddSpecifier();
      Console.WriteLine("-------");
      hSpecifier();
      Console.WriteLine("-------");
      ParseH();
      Console.WriteLine("-------");
      ParseHH();
      Console.WriteLine("-------");
      hhSpecifier();
      Console.WriteLine("-------");
      ParseM();
      Console.WriteLine("-------");
      mSpecifier();
      Console.WriteLine("-------");
      ParseMM();
      Console.WriteLine("-------");
      mmSpecifier();
      Console.WriteLine("-------");
      sSpecifier();
      Console.WriteLine("-------");
      ParseS();
      Console.WriteLine("-------");
      ParseSS();
      Console.WriteLine("-------");
      ssSpecifier();
      Console.WriteLine("-------");
   }

   private static void dSpecifier()
   {
      // <Snippet3>
      TimeSpan ts1 = new TimeSpan(16, 4, 3, 17, 250);
      Console.WriteLine(ts1.ToString("%d"));
      // Displays 16
      // </Snippet3>
      Console.WriteLine();

      // <Snippet4>
      TimeSpan ts2 = new TimeSpan(4, 3, 17);
      Console.WriteLine(ts2.ToString(@"d\.hh\:mm\:ss"));

      TimeSpan ts3 = new TimeSpan(3, 4, 3, 17);
      Console.WriteLine(ts3.ToString(@"d\.hh\:mm\:ss"));
      // The example displays the following output:
      //       0.04:03:17
      //       3.04:03:17
      // </Snippet4>
   }

   private static void ddSpecifier()
   {
      // <Snippet5>
      TimeSpan ts1 = new TimeSpan(0, 23, 17, 47);
      TimeSpan ts2 = new TimeSpan(365, 21, 19, 45);

      for (int ctr = 2; ctr <= 8; ctr++)
      {
         string fmt = new String('d', ctr) + @"\.hh\:mm\:ss";
         Console.WriteLine($"{fmt} --> {ts1.ToString(fmt)}");
         Console.WriteLine($"{fmt} --> {ts2.ToString(fmt)}");
         Console.WriteLine();
      }
      // The example displays the following output:
      //       dd\.hh\:mm\:ss --> 00.23:17:47
      //       dd\.hh\:mm\:ss --> 365.21:19:45
      //
      //       ddd\.hh\:mm\:ss --> 000.23:17:47
      //       ddd\.hh\:mm\:ss --> 365.21:19:45
      //
      //       dddd\.hh\:mm\:ss --> 0000.23:17:47
      //       dddd\.hh\:mm\:ss --> 0365.21:19:45
      //
      //       ddddd\.hh\:mm\:ss --> 00000.23:17:47
      //       ddddd\.hh\:mm\:ss --> 00365.21:19:45
      //
      //       dddddd\.hh\:mm\:ss --> 000000.23:17:47
      //       dddddd\.hh\:mm\:ss --> 000365.21:19:45
      //
      //       ddddddd\.hh\:mm\:ss --> 0000000.23:17:47
      //       ddddddd\.hh\:mm\:ss --> 0000365.21:19:45
      //
      //       dddddddd\.hh\:mm\:ss --> 00000000.23:17:47
      //       dddddddd\.hh\:mm\:ss --> 00000365.21:19:45
      // </Snippet5>
   }

   private static void hSpecifier()
   {
      // <Snippet6>
      TimeSpan ts = new TimeSpan(3, 42, 0);
      Console.WriteLine($"{ts:%h} hours {ts:%m} minutes");
      // The example displays the following output:
      //       3 hours 42 minutes
      // </Snippet6>

      // <Snippet7>
      TimeSpan ts1 = new TimeSpan(14, 3, 17);
      Console.WriteLine(ts1.ToString(@"d\.h\:mm\:ss"));

      TimeSpan ts2 = new TimeSpan(3, 4, 3, 17);
      Console.WriteLine(ts2.ToString(@"d\.h\:mm\:ss"));
      // The example displays the following output:
      //       0.14:03:17
      //       3.4:03:17
      // </Snippet7>
   }

   private static void ParseH()
   {
      // <Snippet8>
      string value = "8";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "%h", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       08:00:00
      // </Snippet8>
   }

   private static void ParseHH()
   {
      // <Snippet9>
      string value = "08";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "hh", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       08:00:00
      // </Snippet9>
   }

   private static void hhSpecifier()
   {
      // <Snippet10>
      TimeSpan ts1 = new TimeSpan(14, 3, 17);
      Console.WriteLine(ts1.ToString(@"d\.hh\:mm\:ss"));

      TimeSpan ts2 = new TimeSpan(3, 4, 3, 17);
      Console.WriteLine(ts2.ToString(@"d\.hh\:mm\:ss"));
      // The example displays the following output:
      //       0.14:03:17
      //       3.04:03:17
      // </Snippet10>
   }

   private static void ParseM()
   {
      // <Snippet11>
      string value = "3";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "%m", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       00:03:00
      // </Snippet11>
   }

   private static void mSpecifier()
   {
      // <Snippet12>
      TimeSpan ts1 = new TimeSpan(0, 6, 32);
      Console.WriteLine($"{ts1:m\\:ss} minutes");

      TimeSpan ts2 = new TimeSpan(3, 4, 3, 17);
      Console.WriteLine($"Elapsed time: {ts2:m\\:ss}");
      // The example displays the following output:
      //       6:32 minutes
      //       Elapsed time: 18:44
      // </Snippet12>
   }

   private static void ParseMM()
   {
      // <Snippet13>
      string value = "07";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "mm", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       00:07:00
      // </Snippet13>
   }

   private static void mmSpecifier()
   {
      // <Snippet14>
      TimeSpan departTime = new TimeSpan(11, 12, 00);
      TimeSpan arriveTime = new TimeSpan(16, 28, 00);
      Console.WriteLine($"Travel time: {arriveTime - departTime:hh\\:mm}");
      // The example displays the following output:
      //       Travel time: 05:16
      // </Snippet14>
   }

   private static void sSpecifier()
   {
      // <Snippet15>
      TimeSpan ts = TimeSpan.FromSeconds(12.465);
      Console.WriteLine(ts.ToString("%s"));
      // The example displays the following output:
      //       12
      // </Snippet15>
      Console.WriteLine();

      // <Snippet16>
      TimeSpan startTime = new TimeSpan(0, 12, 30, 15, 0);
      TimeSpan endTime = new TimeSpan(0, 12, 30, 21, 3);
      Console.WriteLine(@"Elapsed Time: {0:s\:fff} seconds",
                        endTime - startTime);
      // The example displays the following output:
      //       Elapsed Time: 6:003 seconds
      // </Snippet16>
   }

   private static void ParseS()
   {
      // <Snippet17>
      string value = "9";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "%s", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       00:00:09
      // </Snippet17>
   }

   private static void ParseSS()
   {
      // <Snippet18>
      string[] values = { "49", "9", "06" };
      TimeSpan interval;
      foreach (string value in values)
      {
         if (TimeSpan.TryParseExact(value, "ss", null, out interval))
            Console.WriteLine(interval.ToString("c"));
         else
            Console.WriteLine($"Unable to convert '{value}' to a time interval");
      }
      // The example displays the following output:
      //       00:00:49
      //       Unable to convert '9' to a time interval
      //       00:00:06
      // </Snippet18>
   }

   private static void ssSpecifier()
   {
      // <Snippet19>
      TimeSpan interval1 = TimeSpan.FromSeconds(12.60);
      Console.WriteLine(interval1.ToString(@"ss\.fff"));

      TimeSpan interval2 = TimeSpan.FromSeconds(6.485);
      Console.WriteLine(interval2.ToString(@"ss\.fff"));
      // The example displays the following output:
      //       12.600
      //       06.485
      // </Snippet19>
   }
}

sample · vbsamples/snippets/visualbasic/VS_Snippets_CLR/conceptual.timespan.custom/vb/customexamples1.vb14

vb
' Visual Basic .NET Document
Option Strict On

Module Example
    Public Sub Main()
        dSpecifier()
        Console.WriteLine("-------")
        ddSpecifier()
        Console.WriteLine("-------")
        hSpecifier()
        Console.WriteLine("-------")
        ParseH()
        Console.WriteLine("-------")
        ParseHH()
        Console.WriteLine("-------")
        hhSpecifier()
        Console.WriteLine("-------")
        ParseM()
        Console.WriteLine("-------")
        mSpecifier()
        Console.WriteLine("-------")
        ParseMM()
        Console.WriteLine("-------")
        mmSpecifier()
        Console.WriteLine("-------")
        sSpecifier()
        Console.WriteLine("-------")
        ParseS()
        Console.WriteLine("-------")
        ParseSS()
        Console.WriteLine("-------")
        ssSpecifier()
        Console.WriteLine("-------")
    End Sub

    Private Sub dSpecifier()
        ' <Snippet3>
        Dim ts As New TimeSpan(16, 4, 3, 17, 250)
        Console.WriteLine(ts.ToString("%d"))
        ' Displays 16   
        ' </Snippet3>
        Console.WriteLine()

        ' <Snippet4>
        Dim ts2 As New TimeSpan(4, 3, 17)
        Console.WriteLine(ts2.ToString("d\.hh\:mm\:ss"))

        Dim ts3 As New TimeSpan(3, 4, 3, 17)
        Console.WriteLine(ts3.ToString("d\.hh\:mm\:ss"))
        ' The example displays the following output:
        '       0.04:03:17
        '       3.04:03:17      
        ' </Snippet4>
    End Sub

    Private Sub ddSpecifier()
        ' <Snippet5>
        Dim ts1 As New TimeSpan(0, 23, 17, 47)
        Dim ts2 As New TimeSpan(365, 21, 19, 45)

        For ctr As Integer = 2 To 8
            Dim fmt As String = New String("d"c, ctr) + "\.hh\:mm\:ss"
            Console.WriteLine("{0} --> {1:" + fmt + "}", fmt, ts1)
            Console.WriteLine("{0} --> {1:" + fmt + "}", fmt, ts2)
            Console.WriteLine()
        Next
        ' The example displays the following output:
        '       dd\.hh\:mm\:ss --> 00.23:17:47
        '       dd\.hh\:mm\:ss --> 365.21:19:45
        '       
        '       ddd\.hh\:mm\:ss --> 000.23:17:47
        '       ddd\.hh\:mm\:ss --> 365.21:19:45
        '       
        '       dddd\.hh\:mm\:ss --> 0000.23:17:47
        '       dddd\.hh\:mm\:ss --> 0365.21:19:45
        '       
        '       ddddd\.hh\:mm\:ss --> 00000.23:17:47
        '       ddddd\.hh\:mm\:ss --> 00365.21:19:45
        '       
        '       dddddd\.hh\:mm\:ss --> 000000.23:17:47
        '       dddddd\.hh\:mm\:ss --> 000365.21:19:45
        '       
        '       ddddddd\.hh\:mm\:ss --> 0000000.23:17:47
        '       ddddddd\.hh\:mm\:ss --> 0000365.21:19:45
        '       
        '       dddddddd\.hh\:mm\:ss --> 00000000.23:17:47
        '       dddddddd\.hh\:mm\:ss --> 00000365.21:19:45      
        ' </Snippet5>
    End Sub

    Private Sub hSpecifier()
        ' <Snippet6>
        Dim ts As New TimeSpan(3, 42, 0)
        Console.WriteLine("{0:%h} hours {0:%m} minutes", ts)
        ' The example displays the following output:
        '       3 hours 42 minutes
        ' </Snippet6>
        Console.WriteLine()

        ' <Snippet7>
        Dim ts1 As New TimeSpan(14, 3, 17)
        Console.WriteLine(ts1.ToString("d\.h\:mm\:ss"))

        Dim ts2 As New TimeSpan(3, 4, 3, 17)
        Console.WriteLine(ts2.ToString("d\.h\:mm\:ss"))
        ' The example displays the following output:
        '       0.14:03:17
        '       3.4:03:17
        ' </Snippet7>
    End Sub

    Public Sub ParseH()
        ' <Snippet8>
        Dim value As String = "8"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "%h", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       08:00:00                              
        ' </Snippet8>
    End Sub

    Public Sub ParseHH()
        ' <Snippet9>
        Dim value As String = "08"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "hh", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       08:00:00                              
        ' </Snippet9>
    End Sub

    Private Sub hhSpecifier()
        ' <Snippet10>
        Dim ts1 As New TimeSpan(14, 3, 17)
        Console.WriteLine(ts1.ToString("d\.hh\:mm\:ss"))

        Dim ts2 As New TimeSpan(3, 4, 3, 17)
        Console.WriteLine(ts2.ToString("d\.hh\:mm\:ss"))
        ' The example displays the following output:
        '       0.14:03:17
        '       3.04:03:17
        ' </Snippet10>
    End Sub

    Public Sub ParseM()
        ' <Snippet11>
        Dim value As String = "3"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "%m", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       00:03:00                              
        ' </Snippet11>
    End Sub

    Private Sub mSpecifier()
        ' <Snippet12>
        Dim ts1 As New TimeSpan(0, 6, 32)
        Console.WriteLine("{0:m\:ss} minutes", ts1)

        Dim ts2 As New TimeSpan(0, 18, 44)
        Console.WriteLine("Elapsed time: {0:m\:ss}", ts2)
        ' The example displays the following output:
        '       6:32 minutes
        '       Elapsed time: 18:44
        ' </Snippet12>
    End Sub

    Public Sub ParseMM()
        ' <Snippet13>
        Dim value As String = "05"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "mm", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       00:05:00           
        ' </Snippet13>
    End Sub

    Private Sub mmSpecifier()
        ' <Snippet14>
        Dim departTime As New TimeSpan(11, 12, 00)
        Dim arriveTime As New TimeSpan(16, 28, 00)
        Console.WriteLine("Travel time: {0:hh\:mm}",
                          arriveTime - departTime)
        ' The example displays the following output:
        '       Travel time: 05:16      
        ' </Snippet14>
    End Sub

    Private Sub sSpecifier()
        ' <Snippet15>
        Dim ts As TimeSpan = TimeSpan.FromSeconds(12.465)
        Console.WriteLine(ts.ToString("%s"))
        ' The example displays the following output:
        '       12
        ' </Snippet15>
        Console.WriteLine()

        ' <Snippet16>
        Dim startTime As New TimeSpan(0, 12, 30, 15, 0)
        Dim endTime As New TimeSpan(0, 12, 30, 21, 3)
        Console.WriteLine("Elapsed Time: {0:s\:fff} seconds",
                          endTime - startTime)
        ' The example displays the following output:
        '       Elapsed Time: 6:003 seconds      
        ' </Snippet16>
    End Sub

    Public Sub ParseS()
        ' <Snippet17>
        Dim value As String = "9"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "%s", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       00:00:09
        ' </Snippet17>
    End Sub

    Public Sub ParseSS()
        ' <Snippet18>
        Dim values() As String = {"49", "9", "06"}
        Dim interval As TimeSpan
        For Each value As String In values
            If TimeSpan.TryParseExact(value, "ss", Nothing, interval) Then
                Console.WriteLine(interval.ToString("c"))
            Else
                Console.WriteLine("Unable to convert '{0}' to a time interval",
                                  value)
            End If
        Next
        ' The example displays the following output:
        '       00:00:49
        '       Unable to convert '9' to a time interval
        '       00:00:06
        ' </Snippet18>
    End Sub

    Private Sub ssSpecifier()
        ' <Snippet19>
        Dim interval1 As TimeSpan = TimeSpan.FromSeconds(12.60)
        Console.WriteLine(interval1.ToString("ss\.fff"))
        Dim interval2 As TimeSpan = TimeSpan.FromSeconds(6.485)
        Console.WriteLine(interval2.ToString("ss\.fff"))
        ' The example displays the following output:
        '       12.600
        '       06.485
        ' </Snippet19>
    End Sub
End Module

Back to table

The "s" custom format specifier

The "s" custom format specifier outputs the value of the System.TimeSpan.Seconds property, which represents the number of whole seconds in the time interval that isn't included as part of its hours, days, or minutes component. It returns a one-digit string value if the value of the System.TimeSpan.Seconds property is 0 through 9, and it returns a two-digit string value if the value of the System.TimeSpan.Seconds property ranges from 10 to 59.

If the "s" custom format specifier is used alone, specify "%s" so that it isn't misinterpreted as a standard format string. The following example provides an illustration.

sample · csharpsamples/snippets/csharp/VS_Snippets_CLR/conceptual.timespan.custom/cs/customexamples1.cs15

csharp
using System;

public class Example
{
   public static void Main()
   {
      dSpecifier();
      Console.WriteLine("-------");
      ddSpecifier();
      Console.WriteLine("-------");
      hSpecifier();
      Console.WriteLine("-------");
      ParseH();
      Console.WriteLine("-------");
      ParseHH();
      Console.WriteLine("-------");
      hhSpecifier();
      Console.WriteLine("-------");
      ParseM();
      Console.WriteLine("-------");
      mSpecifier();
      Console.WriteLine("-------");
      ParseMM();
      Console.WriteLine("-------");
      mmSpecifier();
      Console.WriteLine("-------");
      sSpecifier();
      Console.WriteLine("-------");
      ParseS();
      Console.WriteLine("-------");
      ParseSS();
      Console.WriteLine("-------");
      ssSpecifier();
      Console.WriteLine("-------");
   }

   private static void dSpecifier()
   {
      // <Snippet3>
      TimeSpan ts1 = new TimeSpan(16, 4, 3, 17, 250);
      Console.WriteLine(ts1.ToString("%d"));
      // Displays 16
      // </Snippet3>
      Console.WriteLine();

      // <Snippet4>
      TimeSpan ts2 = new TimeSpan(4, 3, 17);
      Console.WriteLine(ts2.ToString(@"d\.hh\:mm\:ss"));

      TimeSpan ts3 = new TimeSpan(3, 4, 3, 17);
      Console.WriteLine(ts3.ToString(@"d\.hh\:mm\:ss"));
      // The example displays the following output:
      //       0.04:03:17
      //       3.04:03:17
      // </Snippet4>
   }

   private static void ddSpecifier()
   {
      // <Snippet5>
      TimeSpan ts1 = new TimeSpan(0, 23, 17, 47);
      TimeSpan ts2 = new TimeSpan(365, 21, 19, 45);

      for (int ctr = 2; ctr <= 8; ctr++)
      {
         string fmt = new String('d', ctr) + @"\.hh\:mm\:ss";
         Console.WriteLine($"{fmt} --> {ts1.ToString(fmt)}");
         Console.WriteLine($"{fmt} --> {ts2.ToString(fmt)}");
         Console.WriteLine();
      }
      // The example displays the following output:
      //       dd\.hh\:mm\:ss --> 00.23:17:47
      //       dd\.hh\:mm\:ss --> 365.21:19:45
      //
      //       ddd\.hh\:mm\:ss --> 000.23:17:47
      //       ddd\.hh\:mm\:ss --> 365.21:19:45
      //
      //       dddd\.hh\:mm\:ss --> 0000.23:17:47
      //       dddd\.hh\:mm\:ss --> 0365.21:19:45
      //
      //       ddddd\.hh\:mm\:ss --> 00000.23:17:47
      //       ddddd\.hh\:mm\:ss --> 00365.21:19:45
      //
      //       dddddd\.hh\:mm\:ss --> 000000.23:17:47
      //       dddddd\.hh\:mm\:ss --> 000365.21:19:45
      //
      //       ddddddd\.hh\:mm\:ss --> 0000000.23:17:47
      //       ddddddd\.hh\:mm\:ss --> 0000365.21:19:45
      //
      //       dddddddd\.hh\:mm\:ss --> 00000000.23:17:47
      //       dddddddd\.hh\:mm\:ss --> 00000365.21:19:45
      // </Snippet5>
   }

   private static void hSpecifier()
   {
      // <Snippet6>
      TimeSpan ts = new TimeSpan(3, 42, 0);
      Console.WriteLine($"{ts:%h} hours {ts:%m} minutes");
      // The example displays the following output:
      //       3 hours 42 minutes
      // </Snippet6>

      // <Snippet7>
      TimeSpan ts1 = new TimeSpan(14, 3, 17);
      Console.WriteLine(ts1.ToString(@"d\.h\:mm\:ss"));

      TimeSpan ts2 = new TimeSpan(3, 4, 3, 17);
      Console.WriteLine(ts2.ToString(@"d\.h\:mm\:ss"));
      // The example displays the following output:
      //       0.14:03:17
      //       3.4:03:17
      // </Snippet7>
   }

   private static void ParseH()
   {
      // <Snippet8>
      string value = "8";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "%h", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       08:00:00
      // </Snippet8>
   }

   private static void ParseHH()
   {
      // <Snippet9>
      string value = "08";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "hh", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       08:00:00
      // </Snippet9>
   }

   private static void hhSpecifier()
   {
      // <Snippet10>
      TimeSpan ts1 = new TimeSpan(14, 3, 17);
      Console.WriteLine(ts1.ToString(@"d\.hh\:mm\:ss"));

      TimeSpan ts2 = new TimeSpan(3, 4, 3, 17);
      Console.WriteLine(ts2.ToString(@"d\.hh\:mm\:ss"));
      // The example displays the following output:
      //       0.14:03:17
      //       3.04:03:17
      // </Snippet10>
   }

   private static void ParseM()
   {
      // <Snippet11>
      string value = "3";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "%m", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       00:03:00
      // </Snippet11>
   }

   private static void mSpecifier()
   {
      // <Snippet12>
      TimeSpan ts1 = new TimeSpan(0, 6, 32);
      Console.WriteLine($"{ts1:m\\:ss} minutes");

      TimeSpan ts2 = new TimeSpan(3, 4, 3, 17);
      Console.WriteLine($"Elapsed time: {ts2:m\\:ss}");
      // The example displays the following output:
      //       6:32 minutes
      //       Elapsed time: 18:44
      // </Snippet12>
   }

   private static void ParseMM()
   {
      // <Snippet13>
      string value = "07";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "mm", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       00:07:00
      // </Snippet13>
   }

   private static void mmSpecifier()
   {
      // <Snippet14>
      TimeSpan departTime = new TimeSpan(11, 12, 00);
      TimeSpan arriveTime = new TimeSpan(16, 28, 00);
      Console.WriteLine($"Travel time: {arriveTime - departTime:hh\\:mm}");
      // The example displays the following output:
      //       Travel time: 05:16
      // </Snippet14>
   }

   private static void sSpecifier()
   {
      // <Snippet15>
      TimeSpan ts = TimeSpan.FromSeconds(12.465);
      Console.WriteLine(ts.ToString("%s"));
      // The example displays the following output:
      //       12
      // </Snippet15>
      Console.WriteLine();

      // <Snippet16>
      TimeSpan startTime = new TimeSpan(0, 12, 30, 15, 0);
      TimeSpan endTime = new TimeSpan(0, 12, 30, 21, 3);
      Console.WriteLine(@"Elapsed Time: {0:s\:fff} seconds",
                        endTime - startTime);
      // The example displays the following output:
      //       Elapsed Time: 6:003 seconds
      // </Snippet16>
   }

   private static void ParseS()
   {
      // <Snippet17>
      string value = "9";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "%s", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       00:00:09
      // </Snippet17>
   }

   private static void ParseSS()
   {
      // <Snippet18>
      string[] values = { "49", "9", "06" };
      TimeSpan interval;
      foreach (string value in values)
      {
         if (TimeSpan.TryParseExact(value, "ss", null, out interval))
            Console.WriteLine(interval.ToString("c"));
         else
            Console.WriteLine($"Unable to convert '{value}' to a time interval");
      }
      // The example displays the following output:
      //       00:00:49
      //       Unable to convert '9' to a time interval
      //       00:00:06
      // </Snippet18>
   }

   private static void ssSpecifier()
   {
      // <Snippet19>
      TimeSpan interval1 = TimeSpan.FromSeconds(12.60);
      Console.WriteLine(interval1.ToString(@"ss\.fff"));

      TimeSpan interval2 = TimeSpan.FromSeconds(6.485);
      Console.WriteLine(interval2.ToString(@"ss\.fff"));
      // The example displays the following output:
      //       12.600
      //       06.485
      // </Snippet19>
   }
}

sample · vbsamples/snippets/visualbasic/VS_Snippets_CLR/conceptual.timespan.custom/vb/customexamples1.vb15

vb
' Visual Basic .NET Document
Option Strict On

Module Example
    Public Sub Main()
        dSpecifier()
        Console.WriteLine("-------")
        ddSpecifier()
        Console.WriteLine("-------")
        hSpecifier()
        Console.WriteLine("-------")
        ParseH()
        Console.WriteLine("-------")
        ParseHH()
        Console.WriteLine("-------")
        hhSpecifier()
        Console.WriteLine("-------")
        ParseM()
        Console.WriteLine("-------")
        mSpecifier()
        Console.WriteLine("-------")
        ParseMM()
        Console.WriteLine("-------")
        mmSpecifier()
        Console.WriteLine("-------")
        sSpecifier()
        Console.WriteLine("-------")
        ParseS()
        Console.WriteLine("-------")
        ParseSS()
        Console.WriteLine("-------")
        ssSpecifier()
        Console.WriteLine("-------")
    End Sub

    Private Sub dSpecifier()
        ' <Snippet3>
        Dim ts As New TimeSpan(16, 4, 3, 17, 250)
        Console.WriteLine(ts.ToString("%d"))
        ' Displays 16   
        ' </Snippet3>
        Console.WriteLine()

        ' <Snippet4>
        Dim ts2 As New TimeSpan(4, 3, 17)
        Console.WriteLine(ts2.ToString("d\.hh\:mm\:ss"))

        Dim ts3 As New TimeSpan(3, 4, 3, 17)
        Console.WriteLine(ts3.ToString("d\.hh\:mm\:ss"))
        ' The example displays the following output:
        '       0.04:03:17
        '       3.04:03:17      
        ' </Snippet4>
    End Sub

    Private Sub ddSpecifier()
        ' <Snippet5>
        Dim ts1 As New TimeSpan(0, 23, 17, 47)
        Dim ts2 As New TimeSpan(365, 21, 19, 45)

        For ctr As Integer = 2 To 8
            Dim fmt As String = New String("d"c, ctr) + "\.hh\:mm\:ss"
            Console.WriteLine("{0} --> {1:" + fmt + "}", fmt, ts1)
            Console.WriteLine("{0} --> {1:" + fmt + "}", fmt, ts2)
            Console.WriteLine()
        Next
        ' The example displays the following output:
        '       dd\.hh\:mm\:ss --> 00.23:17:47
        '       dd\.hh\:mm\:ss --> 365.21:19:45
        '       
        '       ddd\.hh\:mm\:ss --> 000.23:17:47
        '       ddd\.hh\:mm\:ss --> 365.21:19:45
        '       
        '       dddd\.hh\:mm\:ss --> 0000.23:17:47
        '       dddd\.hh\:mm\:ss --> 0365.21:19:45
        '       
        '       ddddd\.hh\:mm\:ss --> 00000.23:17:47
        '       ddddd\.hh\:mm\:ss --> 00365.21:19:45
        '       
        '       dddddd\.hh\:mm\:ss --> 000000.23:17:47
        '       dddddd\.hh\:mm\:ss --> 000365.21:19:45
        '       
        '       ddddddd\.hh\:mm\:ss --> 0000000.23:17:47
        '       ddddddd\.hh\:mm\:ss --> 0000365.21:19:45
        '       
        '       dddddddd\.hh\:mm\:ss --> 00000000.23:17:47
        '       dddddddd\.hh\:mm\:ss --> 00000365.21:19:45      
        ' </Snippet5>
    End Sub

    Private Sub hSpecifier()
        ' <Snippet6>
        Dim ts As New TimeSpan(3, 42, 0)
        Console.WriteLine("{0:%h} hours {0:%m} minutes", ts)
        ' The example displays the following output:
        '       3 hours 42 minutes
        ' </Snippet6>
        Console.WriteLine()

        ' <Snippet7>
        Dim ts1 As New TimeSpan(14, 3, 17)
        Console.WriteLine(ts1.ToString("d\.h\:mm\:ss"))

        Dim ts2 As New TimeSpan(3, 4, 3, 17)
        Console.WriteLine(ts2.ToString("d\.h\:mm\:ss"))
        ' The example displays the following output:
        '       0.14:03:17
        '       3.4:03:17
        ' </Snippet7>
    End Sub

    Public Sub ParseH()
        ' <Snippet8>
        Dim value As String = "8"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "%h", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       08:00:00                              
        ' </Snippet8>
    End Sub

    Public Sub ParseHH()
        ' <Snippet9>
        Dim value As String = "08"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "hh", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       08:00:00                              
        ' </Snippet9>
    End Sub

    Private Sub hhSpecifier()
        ' <Snippet10>
        Dim ts1 As New TimeSpan(14, 3, 17)
        Console.WriteLine(ts1.ToString("d\.hh\:mm\:ss"))

        Dim ts2 As New TimeSpan(3, 4, 3, 17)
        Console.WriteLine(ts2.ToString("d\.hh\:mm\:ss"))
        ' The example displays the following output:
        '       0.14:03:17
        '       3.04:03:17
        ' </Snippet10>
    End Sub

    Public Sub ParseM()
        ' <Snippet11>
        Dim value As String = "3"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "%m", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       00:03:00                              
        ' </Snippet11>
    End Sub

    Private Sub mSpecifier()
        ' <Snippet12>
        Dim ts1 As New TimeSpan(0, 6, 32)
        Console.WriteLine("{0:m\:ss} minutes", ts1)

        Dim ts2 As New TimeSpan(0, 18, 44)
        Console.WriteLine("Elapsed time: {0:m\:ss}", ts2)
        ' The example displays the following output:
        '       6:32 minutes
        '       Elapsed time: 18:44
        ' </Snippet12>
    End Sub

    Public Sub ParseMM()
        ' <Snippet13>
        Dim value As String = "05"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "mm", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       00:05:00           
        ' </Snippet13>
    End Sub

    Private Sub mmSpecifier()
        ' <Snippet14>
        Dim departTime As New TimeSpan(11, 12, 00)
        Dim arriveTime As New TimeSpan(16, 28, 00)
        Console.WriteLine("Travel time: {0:hh\:mm}",
                          arriveTime - departTime)
        ' The example displays the following output:
        '       Travel time: 05:16      
        ' </Snippet14>
    End Sub

    Private Sub sSpecifier()
        ' <Snippet15>
        Dim ts As TimeSpan = TimeSpan.FromSeconds(12.465)
        Console.WriteLine(ts.ToString("%s"))
        ' The example displays the following output:
        '       12
        ' </Snippet15>
        Console.WriteLine()

        ' <Snippet16>
        Dim startTime As New TimeSpan(0, 12, 30, 15, 0)
        Dim endTime As New TimeSpan(0, 12, 30, 21, 3)
        Console.WriteLine("Elapsed Time: {0:s\:fff} seconds",
                          endTime - startTime)
        ' The example displays the following output:
        '       Elapsed Time: 6:003 seconds      
        ' </Snippet16>
    End Sub

    Public Sub ParseS()
        ' <Snippet17>
        Dim value As String = "9"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "%s", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       00:00:09
        ' </Snippet17>
    End Sub

    Public Sub ParseSS()
        ' <Snippet18>
        Dim values() As String = {"49", "9", "06"}
        Dim interval As TimeSpan
        For Each value As String In values
            If TimeSpan.TryParseExact(value, "ss", Nothing, interval) Then
                Console.WriteLine(interval.ToString("c"))
            Else
                Console.WriteLine("Unable to convert '{0}' to a time interval",
                                  value)
            End If
        Next
        ' The example displays the following output:
        '       00:00:49
        '       Unable to convert '9' to a time interval
        '       00:00:06
        ' </Snippet18>
    End Sub

    Private Sub ssSpecifier()
        ' <Snippet19>
        Dim interval1 As TimeSpan = TimeSpan.FromSeconds(12.60)
        Console.WriteLine(interval1.ToString("ss\.fff"))
        Dim interval2 As TimeSpan = TimeSpan.FromSeconds(6.485)
        Console.WriteLine(interval2.ToString("ss\.fff"))
        ' The example displays the following output:
        '       12.600
        '       06.485
        ' </Snippet19>
    End Sub
End Module

Ordinarily, in a parsing operation, an input string that includes only a single number is interpreted as the number of days. You can use the "%s" custom format specifier instead to interpret the numeric string as the number of seconds. The following example provides an illustration.

sample · csharpsamples/snippets/csharp/VS_Snippets_CLR/conceptual.timespan.custom/cs/customexamples1.cs17

csharp
using System;

public class Example
{
   public static void Main()
   {
      dSpecifier();
      Console.WriteLine("-------");
      ddSpecifier();
      Console.WriteLine("-------");
      hSpecifier();
      Console.WriteLine("-------");
      ParseH();
      Console.WriteLine("-------");
      ParseHH();
      Console.WriteLine("-------");
      hhSpecifier();
      Console.WriteLine("-------");
      ParseM();
      Console.WriteLine("-------");
      mSpecifier();
      Console.WriteLine("-------");
      ParseMM();
      Console.WriteLine("-------");
      mmSpecifier();
      Console.WriteLine("-------");
      sSpecifier();
      Console.WriteLine("-------");
      ParseS();
      Console.WriteLine("-------");
      ParseSS();
      Console.WriteLine("-------");
      ssSpecifier();
      Console.WriteLine("-------");
   }

   private static void dSpecifier()
   {
      // <Snippet3>
      TimeSpan ts1 = new TimeSpan(16, 4, 3, 17, 250);
      Console.WriteLine(ts1.ToString("%d"));
      // Displays 16
      // </Snippet3>
      Console.WriteLine();

      // <Snippet4>
      TimeSpan ts2 = new TimeSpan(4, 3, 17);
      Console.WriteLine(ts2.ToString(@"d\.hh\:mm\:ss"));

      TimeSpan ts3 = new TimeSpan(3, 4, 3, 17);
      Console.WriteLine(ts3.ToString(@"d\.hh\:mm\:ss"));
      // The example displays the following output:
      //       0.04:03:17
      //       3.04:03:17
      // </Snippet4>
   }

   private static void ddSpecifier()
   {
      // <Snippet5>
      TimeSpan ts1 = new TimeSpan(0, 23, 17, 47);
      TimeSpan ts2 = new TimeSpan(365, 21, 19, 45);

      for (int ctr = 2; ctr <= 8; ctr++)
      {
         string fmt = new String('d', ctr) + @"\.hh\:mm\:ss";
         Console.WriteLine($"{fmt} --> {ts1.ToString(fmt)}");
         Console.WriteLine($"{fmt} --> {ts2.ToString(fmt)}");
         Console.WriteLine();
      }
      // The example displays the following output:
      //       dd\.hh\:mm\:ss --> 00.23:17:47
      //       dd\.hh\:mm\:ss --> 365.21:19:45
      //
      //       ddd\.hh\:mm\:ss --> 000.23:17:47
      //       ddd\.hh\:mm\:ss --> 365.21:19:45
      //
      //       dddd\.hh\:mm\:ss --> 0000.23:17:47
      //       dddd\.hh\:mm\:ss --> 0365.21:19:45
      //
      //       ddddd\.hh\:mm\:ss --> 00000.23:17:47
      //       ddddd\.hh\:mm\:ss --> 00365.21:19:45
      //
      //       dddddd\.hh\:mm\:ss --> 000000.23:17:47
      //       dddddd\.hh\:mm\:ss --> 000365.21:19:45
      //
      //       ddddddd\.hh\:mm\:ss --> 0000000.23:17:47
      //       ddddddd\.hh\:mm\:ss --> 0000365.21:19:45
      //
      //       dddddddd\.hh\:mm\:ss --> 00000000.23:17:47
      //       dddddddd\.hh\:mm\:ss --> 00000365.21:19:45
      // </Snippet5>
   }

   private static void hSpecifier()
   {
      // <Snippet6>
      TimeSpan ts = new TimeSpan(3, 42, 0);
      Console.WriteLine($"{ts:%h} hours {ts:%m} minutes");
      // The example displays the following output:
      //       3 hours 42 minutes
      // </Snippet6>

      // <Snippet7>
      TimeSpan ts1 = new TimeSpan(14, 3, 17);
      Console.WriteLine(ts1.ToString(@"d\.h\:mm\:ss"));

      TimeSpan ts2 = new TimeSpan(3, 4, 3, 17);
      Console.WriteLine(ts2.ToString(@"d\.h\:mm\:ss"));
      // The example displays the following output:
      //       0.14:03:17
      //       3.4:03:17
      // </Snippet7>
   }

   private static void ParseH()
   {
      // <Snippet8>
      string value = "8";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "%h", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       08:00:00
      // </Snippet8>
   }

   private static void ParseHH()
   {
      // <Snippet9>
      string value = "08";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "hh", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       08:00:00
      // </Snippet9>
   }

   private static void hhSpecifier()
   {
      // <Snippet10>
      TimeSpan ts1 = new TimeSpan(14, 3, 17);
      Console.WriteLine(ts1.ToString(@"d\.hh\:mm\:ss"));

      TimeSpan ts2 = new TimeSpan(3, 4, 3, 17);
      Console.WriteLine(ts2.ToString(@"d\.hh\:mm\:ss"));
      // The example displays the following output:
      //       0.14:03:17
      //       3.04:03:17
      // </Snippet10>
   }

   private static void ParseM()
   {
      // <Snippet11>
      string value = "3";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "%m", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       00:03:00
      // </Snippet11>
   }

   private static void mSpecifier()
   {
      // <Snippet12>
      TimeSpan ts1 = new TimeSpan(0, 6, 32);
      Console.WriteLine($"{ts1:m\\:ss} minutes");

      TimeSpan ts2 = new TimeSpan(3, 4, 3, 17);
      Console.WriteLine($"Elapsed time: {ts2:m\\:ss}");
      // The example displays the following output:
      //       6:32 minutes
      //       Elapsed time: 18:44
      // </Snippet12>
   }

   private static void ParseMM()
   {
      // <Snippet13>
      string value = "07";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "mm", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       00:07:00
      // </Snippet13>
   }

   private static void mmSpecifier()
   {
      // <Snippet14>
      TimeSpan departTime = new TimeSpan(11, 12, 00);
      TimeSpan arriveTime = new TimeSpan(16, 28, 00);
      Console.WriteLine($"Travel time: {arriveTime - departTime:hh\\:mm}");
      // The example displays the following output:
      //       Travel time: 05:16
      // </Snippet14>
   }

   private static void sSpecifier()
   {
      // <Snippet15>
      TimeSpan ts = TimeSpan.FromSeconds(12.465);
      Console.WriteLine(ts.ToString("%s"));
      // The example displays the following output:
      //       12
      // </Snippet15>
      Console.WriteLine();

      // <Snippet16>
      TimeSpan startTime = new TimeSpan(0, 12, 30, 15, 0);
      TimeSpan endTime = new TimeSpan(0, 12, 30, 21, 3);
      Console.WriteLine(@"Elapsed Time: {0:s\:fff} seconds",
                        endTime - startTime);
      // The example displays the following output:
      //       Elapsed Time: 6:003 seconds
      // </Snippet16>
   }

   private static void ParseS()
   {
      // <Snippet17>
      string value = "9";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "%s", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       00:00:09
      // </Snippet17>
   }

   private static void ParseSS()
   {
      // <Snippet18>
      string[] values = { "49", "9", "06" };
      TimeSpan interval;
      foreach (string value in values)
      {
         if (TimeSpan.TryParseExact(value, "ss", null, out interval))
            Console.WriteLine(interval.ToString("c"));
         else
            Console.WriteLine($"Unable to convert '{value}' to a time interval");
      }
      // The example displays the following output:
      //       00:00:49
      //       Unable to convert '9' to a time interval
      //       00:00:06
      // </Snippet18>
   }

   private static void ssSpecifier()
   {
      // <Snippet19>
      TimeSpan interval1 = TimeSpan.FromSeconds(12.60);
      Console.WriteLine(interval1.ToString(@"ss\.fff"));

      TimeSpan interval2 = TimeSpan.FromSeconds(6.485);
      Console.WriteLine(interval2.ToString(@"ss\.fff"));
      // The example displays the following output:
      //       12.600
      //       06.485
      // </Snippet19>
   }
}

sample · vbsamples/snippets/visualbasic/VS_Snippets_CLR/conceptual.timespan.custom/vb/customexamples1.vb17

vb
' Visual Basic .NET Document
Option Strict On

Module Example
    Public Sub Main()
        dSpecifier()
        Console.WriteLine("-------")
        ddSpecifier()
        Console.WriteLine("-------")
        hSpecifier()
        Console.WriteLine("-------")
        ParseH()
        Console.WriteLine("-------")
        ParseHH()
        Console.WriteLine("-------")
        hhSpecifier()
        Console.WriteLine("-------")
        ParseM()
        Console.WriteLine("-------")
        mSpecifier()
        Console.WriteLine("-------")
        ParseMM()
        Console.WriteLine("-------")
        mmSpecifier()
        Console.WriteLine("-------")
        sSpecifier()
        Console.WriteLine("-------")
        ParseS()
        Console.WriteLine("-------")
        ParseSS()
        Console.WriteLine("-------")
        ssSpecifier()
        Console.WriteLine("-------")
    End Sub

    Private Sub dSpecifier()
        ' <Snippet3>
        Dim ts As New TimeSpan(16, 4, 3, 17, 250)
        Console.WriteLine(ts.ToString("%d"))
        ' Displays 16   
        ' </Snippet3>
        Console.WriteLine()

        ' <Snippet4>
        Dim ts2 As New TimeSpan(4, 3, 17)
        Console.WriteLine(ts2.ToString("d\.hh\:mm\:ss"))

        Dim ts3 As New TimeSpan(3, 4, 3, 17)
        Console.WriteLine(ts3.ToString("d\.hh\:mm\:ss"))
        ' The example displays the following output:
        '       0.04:03:17
        '       3.04:03:17      
        ' </Snippet4>
    End Sub

    Private Sub ddSpecifier()
        ' <Snippet5>
        Dim ts1 As New TimeSpan(0, 23, 17, 47)
        Dim ts2 As New TimeSpan(365, 21, 19, 45)

        For ctr As Integer = 2 To 8
            Dim fmt As String = New String("d"c, ctr) + "\.hh\:mm\:ss"
            Console.WriteLine("{0} --> {1:" + fmt + "}", fmt, ts1)
            Console.WriteLine("{0} --> {1:" + fmt + "}", fmt, ts2)
            Console.WriteLine()
        Next
        ' The example displays the following output:
        '       dd\.hh\:mm\:ss --> 00.23:17:47
        '       dd\.hh\:mm\:ss --> 365.21:19:45
        '       
        '       ddd\.hh\:mm\:ss --> 000.23:17:47
        '       ddd\.hh\:mm\:ss --> 365.21:19:45
        '       
        '       dddd\.hh\:mm\:ss --> 0000.23:17:47
        '       dddd\.hh\:mm\:ss --> 0365.21:19:45
        '       
        '       ddddd\.hh\:mm\:ss --> 00000.23:17:47
        '       ddddd\.hh\:mm\:ss --> 00365.21:19:45
        '       
        '       dddddd\.hh\:mm\:ss --> 000000.23:17:47
        '       dddddd\.hh\:mm\:ss --> 000365.21:19:45
        '       
        '       ddddddd\.hh\:mm\:ss --> 0000000.23:17:47
        '       ddddddd\.hh\:mm\:ss --> 0000365.21:19:45
        '       
        '       dddddddd\.hh\:mm\:ss --> 00000000.23:17:47
        '       dddddddd\.hh\:mm\:ss --> 00000365.21:19:45      
        ' </Snippet5>
    End Sub

    Private Sub hSpecifier()
        ' <Snippet6>
        Dim ts As New TimeSpan(3, 42, 0)
        Console.WriteLine("{0:%h} hours {0:%m} minutes", ts)
        ' The example displays the following output:
        '       3 hours 42 minutes
        ' </Snippet6>
        Console.WriteLine()

        ' <Snippet7>
        Dim ts1 As New TimeSpan(14, 3, 17)
        Console.WriteLine(ts1.ToString("d\.h\:mm\:ss"))

        Dim ts2 As New TimeSpan(3, 4, 3, 17)
        Console.WriteLine(ts2.ToString("d\.h\:mm\:ss"))
        ' The example displays the following output:
        '       0.14:03:17
        '       3.4:03:17
        ' </Snippet7>
    End Sub

    Public Sub ParseH()
        ' <Snippet8>
        Dim value As String = "8"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "%h", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       08:00:00                              
        ' </Snippet8>
    End Sub

    Public Sub ParseHH()
        ' <Snippet9>
        Dim value As String = "08"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "hh", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       08:00:00                              
        ' </Snippet9>
    End Sub

    Private Sub hhSpecifier()
        ' <Snippet10>
        Dim ts1 As New TimeSpan(14, 3, 17)
        Console.WriteLine(ts1.ToString("d\.hh\:mm\:ss"))

        Dim ts2 As New TimeSpan(3, 4, 3, 17)
        Console.WriteLine(ts2.ToString("d\.hh\:mm\:ss"))
        ' The example displays the following output:
        '       0.14:03:17
        '       3.04:03:17
        ' </Snippet10>
    End Sub

    Public Sub ParseM()
        ' <Snippet11>
        Dim value As String = "3"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "%m", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       00:03:00                              
        ' </Snippet11>
    End Sub

    Private Sub mSpecifier()
        ' <Snippet12>
        Dim ts1 As New TimeSpan(0, 6, 32)
        Console.WriteLine("{0:m\:ss} minutes", ts1)

        Dim ts2 As New TimeSpan(0, 18, 44)
        Console.WriteLine("Elapsed time: {0:m\:ss}", ts2)
        ' The example displays the following output:
        '       6:32 minutes
        '       Elapsed time: 18:44
        ' </Snippet12>
    End Sub

    Public Sub ParseMM()
        ' <Snippet13>
        Dim value As String = "05"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "mm", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       00:05:00           
        ' </Snippet13>
    End Sub

    Private Sub mmSpecifier()
        ' <Snippet14>
        Dim departTime As New TimeSpan(11, 12, 00)
        Dim arriveTime As New TimeSpan(16, 28, 00)
        Console.WriteLine("Travel time: {0:hh\:mm}",
                          arriveTime - departTime)
        ' The example displays the following output:
        '       Travel time: 05:16      
        ' </Snippet14>
    End Sub

    Private Sub sSpecifier()
        ' <Snippet15>
        Dim ts As TimeSpan = TimeSpan.FromSeconds(12.465)
        Console.WriteLine(ts.ToString("%s"))
        ' The example displays the following output:
        '       12
        ' </Snippet15>
        Console.WriteLine()

        ' <Snippet16>
        Dim startTime As New TimeSpan(0, 12, 30, 15, 0)
        Dim endTime As New TimeSpan(0, 12, 30, 21, 3)
        Console.WriteLine("Elapsed Time: {0:s\:fff} seconds",
                          endTime - startTime)
        ' The example displays the following output:
        '       Elapsed Time: 6:003 seconds      
        ' </Snippet16>
    End Sub

    Public Sub ParseS()
        ' <Snippet17>
        Dim value As String = "9"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "%s", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       00:00:09
        ' </Snippet17>
    End Sub

    Public Sub ParseSS()
        ' <Snippet18>
        Dim values() As String = {"49", "9", "06"}
        Dim interval As TimeSpan
        For Each value As String In values
            If TimeSpan.TryParseExact(value, "ss", Nothing, interval) Then
                Console.WriteLine(interval.ToString("c"))
            Else
                Console.WriteLine("Unable to convert '{0}' to a time interval",
                                  value)
            End If
        Next
        ' The example displays the following output:
        '       00:00:49
        '       Unable to convert '9' to a time interval
        '       00:00:06
        ' </Snippet18>
    End Sub

    Private Sub ssSpecifier()
        ' <Snippet19>
        Dim interval1 As TimeSpan = TimeSpan.FromSeconds(12.60)
        Console.WriteLine(interval1.ToString("ss\.fff"))
        Dim interval2 As TimeSpan = TimeSpan.FromSeconds(6.485)
        Console.WriteLine(interval2.ToString("ss\.fff"))
        ' The example displays the following output:
        '       12.600
        '       06.485
        ' </Snippet19>
    End Sub
End Module

The following example illustrates the use of the "s" custom format specifier.

sample · csharpsamples/snippets/csharp/VS_Snippets_CLR/conceptual.timespan.custom/cs/customexamples1.cs16

csharp
using System;

public class Example
{
   public static void Main()
   {
      dSpecifier();
      Console.WriteLine("-------");
      ddSpecifier();
      Console.WriteLine("-------");
      hSpecifier();
      Console.WriteLine("-------");
      ParseH();
      Console.WriteLine("-------");
      ParseHH();
      Console.WriteLine("-------");
      hhSpecifier();
      Console.WriteLine("-------");
      ParseM();
      Console.WriteLine("-------");
      mSpecifier();
      Console.WriteLine("-------");
      ParseMM();
      Console.WriteLine("-------");
      mmSpecifier();
      Console.WriteLine("-------");
      sSpecifier();
      Console.WriteLine("-------");
      ParseS();
      Console.WriteLine("-------");
      ParseSS();
      Console.WriteLine("-------");
      ssSpecifier();
      Console.WriteLine("-------");
   }

   private static void dSpecifier()
   {
      // <Snippet3>
      TimeSpan ts1 = new TimeSpan(16, 4, 3, 17, 250);
      Console.WriteLine(ts1.ToString("%d"));
      // Displays 16
      // </Snippet3>
      Console.WriteLine();

      // <Snippet4>
      TimeSpan ts2 = new TimeSpan(4, 3, 17);
      Console.WriteLine(ts2.ToString(@"d\.hh\:mm\:ss"));

      TimeSpan ts3 = new TimeSpan(3, 4, 3, 17);
      Console.WriteLine(ts3.ToString(@"d\.hh\:mm\:ss"));
      // The example displays the following output:
      //       0.04:03:17
      //       3.04:03:17
      // </Snippet4>
   }

   private static void ddSpecifier()
   {
      // <Snippet5>
      TimeSpan ts1 = new TimeSpan(0, 23, 17, 47);
      TimeSpan ts2 = new TimeSpan(365, 21, 19, 45);

      for (int ctr = 2; ctr <= 8; ctr++)
      {
         string fmt = new String('d', ctr) + @"\.hh\:mm\:ss";
         Console.WriteLine($"{fmt} --> {ts1.ToString(fmt)}");
         Console.WriteLine($"{fmt} --> {ts2.ToString(fmt)}");
         Console.WriteLine();
      }
      // The example displays the following output:
      //       dd\.hh\:mm\:ss --> 00.23:17:47
      //       dd\.hh\:mm\:ss --> 365.21:19:45
      //
      //       ddd\.hh\:mm\:ss --> 000.23:17:47
      //       ddd\.hh\:mm\:ss --> 365.21:19:45
      //
      //       dddd\.hh\:mm\:ss --> 0000.23:17:47
      //       dddd\.hh\:mm\:ss --> 0365.21:19:45
      //
      //       ddddd\.hh\:mm\:ss --> 00000.23:17:47
      //       ddddd\.hh\:mm\:ss --> 00365.21:19:45
      //
      //       dddddd\.hh\:mm\:ss --> 000000.23:17:47
      //       dddddd\.hh\:mm\:ss --> 000365.21:19:45
      //
      //       ddddddd\.hh\:mm\:ss --> 0000000.23:17:47
      //       ddddddd\.hh\:mm\:ss --> 0000365.21:19:45
      //
      //       dddddddd\.hh\:mm\:ss --> 00000000.23:17:47
      //       dddddddd\.hh\:mm\:ss --> 00000365.21:19:45
      // </Snippet5>
   }

   private static void hSpecifier()
   {
      // <Snippet6>
      TimeSpan ts = new TimeSpan(3, 42, 0);
      Console.WriteLine($"{ts:%h} hours {ts:%m} minutes");
      // The example displays the following output:
      //       3 hours 42 minutes
      // </Snippet6>

      // <Snippet7>
      TimeSpan ts1 = new TimeSpan(14, 3, 17);
      Console.WriteLine(ts1.ToString(@"d\.h\:mm\:ss"));

      TimeSpan ts2 = new TimeSpan(3, 4, 3, 17);
      Console.WriteLine(ts2.ToString(@"d\.h\:mm\:ss"));
      // The example displays the following output:
      //       0.14:03:17
      //       3.4:03:17
      // </Snippet7>
   }

   private static void ParseH()
   {
      // <Snippet8>
      string value = "8";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "%h", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       08:00:00
      // </Snippet8>
   }

   private static void ParseHH()
   {
      // <Snippet9>
      string value = "08";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "hh", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       08:00:00
      // </Snippet9>
   }

   private static void hhSpecifier()
   {
      // <Snippet10>
      TimeSpan ts1 = new TimeSpan(14, 3, 17);
      Console.WriteLine(ts1.ToString(@"d\.hh\:mm\:ss"));

      TimeSpan ts2 = new TimeSpan(3, 4, 3, 17);
      Console.WriteLine(ts2.ToString(@"d\.hh\:mm\:ss"));
      // The example displays the following output:
      //       0.14:03:17
      //       3.04:03:17
      // </Snippet10>
   }

   private static void ParseM()
   {
      // <Snippet11>
      string value = "3";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "%m", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       00:03:00
      // </Snippet11>
   }

   private static void mSpecifier()
   {
      // <Snippet12>
      TimeSpan ts1 = new TimeSpan(0, 6, 32);
      Console.WriteLine($"{ts1:m\\:ss} minutes");

      TimeSpan ts2 = new TimeSpan(3, 4, 3, 17);
      Console.WriteLine($"Elapsed time: {ts2:m\\:ss}");
      // The example displays the following output:
      //       6:32 minutes
      //       Elapsed time: 18:44
      // </Snippet12>
   }

   private static void ParseMM()
   {
      // <Snippet13>
      string value = "07";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "mm", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       00:07:00
      // </Snippet13>
   }

   private static void mmSpecifier()
   {
      // <Snippet14>
      TimeSpan departTime = new TimeSpan(11, 12, 00);
      TimeSpan arriveTime = new TimeSpan(16, 28, 00);
      Console.WriteLine($"Travel time: {arriveTime - departTime:hh\\:mm}");
      // The example displays the following output:
      //       Travel time: 05:16
      // </Snippet14>
   }

   private static void sSpecifier()
   {
      // <Snippet15>
      TimeSpan ts = TimeSpan.FromSeconds(12.465);
      Console.WriteLine(ts.ToString("%s"));
      // The example displays the following output:
      //       12
      // </Snippet15>
      Console.WriteLine();

      // <Snippet16>
      TimeSpan startTime = new TimeSpan(0, 12, 30, 15, 0);
      TimeSpan endTime = new TimeSpan(0, 12, 30, 21, 3);
      Console.WriteLine(@"Elapsed Time: {0:s\:fff} seconds",
                        endTime - startTime);
      // The example displays the following output:
      //       Elapsed Time: 6:003 seconds
      // </Snippet16>
   }

   private static void ParseS()
   {
      // <Snippet17>
      string value = "9";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "%s", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       00:00:09
      // </Snippet17>
   }

   private static void ParseSS()
   {
      // <Snippet18>
      string[] values = { "49", "9", "06" };
      TimeSpan interval;
      foreach (string value in values)
      {
         if (TimeSpan.TryParseExact(value, "ss", null, out interval))
            Console.WriteLine(interval.ToString("c"));
         else
            Console.WriteLine($"Unable to convert '{value}' to a time interval");
      }
      // The example displays the following output:
      //       00:00:49
      //       Unable to convert '9' to a time interval
      //       00:00:06
      // </Snippet18>
   }

   private static void ssSpecifier()
   {
      // <Snippet19>
      TimeSpan interval1 = TimeSpan.FromSeconds(12.60);
      Console.WriteLine(interval1.ToString(@"ss\.fff"));

      TimeSpan interval2 = TimeSpan.FromSeconds(6.485);
      Console.WriteLine(interval2.ToString(@"ss\.fff"));
      // The example displays the following output:
      //       12.600
      //       06.485
      // </Snippet19>
   }
}

sample · vbsamples/snippets/visualbasic/VS_Snippets_CLR/conceptual.timespan.custom/vb/customexamples1.vb16

vb
' Visual Basic .NET Document
Option Strict On

Module Example
    Public Sub Main()
        dSpecifier()
        Console.WriteLine("-------")
        ddSpecifier()
        Console.WriteLine("-------")
        hSpecifier()
        Console.WriteLine("-------")
        ParseH()
        Console.WriteLine("-------")
        ParseHH()
        Console.WriteLine("-------")
        hhSpecifier()
        Console.WriteLine("-------")
        ParseM()
        Console.WriteLine("-------")
        mSpecifier()
        Console.WriteLine("-------")
        ParseMM()
        Console.WriteLine("-------")
        mmSpecifier()
        Console.WriteLine("-------")
        sSpecifier()
        Console.WriteLine("-------")
        ParseS()
        Console.WriteLine("-------")
        ParseSS()
        Console.WriteLine("-------")
        ssSpecifier()
        Console.WriteLine("-------")
    End Sub

    Private Sub dSpecifier()
        ' <Snippet3>
        Dim ts As New TimeSpan(16, 4, 3, 17, 250)
        Console.WriteLine(ts.ToString("%d"))
        ' Displays 16   
        ' </Snippet3>
        Console.WriteLine()

        ' <Snippet4>
        Dim ts2 As New TimeSpan(4, 3, 17)
        Console.WriteLine(ts2.ToString("d\.hh\:mm\:ss"))

        Dim ts3 As New TimeSpan(3, 4, 3, 17)
        Console.WriteLine(ts3.ToString("d\.hh\:mm\:ss"))
        ' The example displays the following output:
        '       0.04:03:17
        '       3.04:03:17      
        ' </Snippet4>
    End Sub

    Private Sub ddSpecifier()
        ' <Snippet5>
        Dim ts1 As New TimeSpan(0, 23, 17, 47)
        Dim ts2 As New TimeSpan(365, 21, 19, 45)

        For ctr As Integer = 2 To 8
            Dim fmt As String = New String("d"c, ctr) + "\.hh\:mm\:ss"
            Console.WriteLine("{0} --> {1:" + fmt + "}", fmt, ts1)
            Console.WriteLine("{0} --> {1:" + fmt + "}", fmt, ts2)
            Console.WriteLine()
        Next
        ' The example displays the following output:
        '       dd\.hh\:mm\:ss --> 00.23:17:47
        '       dd\.hh\:mm\:ss --> 365.21:19:45
        '       
        '       ddd\.hh\:mm\:ss --> 000.23:17:47
        '       ddd\.hh\:mm\:ss --> 365.21:19:45
        '       
        '       dddd\.hh\:mm\:ss --> 0000.23:17:47
        '       dddd\.hh\:mm\:ss --> 0365.21:19:45
        '       
        '       ddddd\.hh\:mm\:ss --> 00000.23:17:47
        '       ddddd\.hh\:mm\:ss --> 00365.21:19:45
        '       
        '       dddddd\.hh\:mm\:ss --> 000000.23:17:47
        '       dddddd\.hh\:mm\:ss --> 000365.21:19:45
        '       
        '       ddddddd\.hh\:mm\:ss --> 0000000.23:17:47
        '       ddddddd\.hh\:mm\:ss --> 0000365.21:19:45
        '       
        '       dddddddd\.hh\:mm\:ss --> 00000000.23:17:47
        '       dddddddd\.hh\:mm\:ss --> 00000365.21:19:45      
        ' </Snippet5>
    End Sub

    Private Sub hSpecifier()
        ' <Snippet6>
        Dim ts As New TimeSpan(3, 42, 0)
        Console.WriteLine("{0:%h} hours {0:%m} minutes", ts)
        ' The example displays the following output:
        '       3 hours 42 minutes
        ' </Snippet6>
        Console.WriteLine()

        ' <Snippet7>
        Dim ts1 As New TimeSpan(14, 3, 17)
        Console.WriteLine(ts1.ToString("d\.h\:mm\:ss"))

        Dim ts2 As New TimeSpan(3, 4, 3, 17)
        Console.WriteLine(ts2.ToString("d\.h\:mm\:ss"))
        ' The example displays the following output:
        '       0.14:03:17
        '       3.4:03:17
        ' </Snippet7>
    End Sub

    Public Sub ParseH()
        ' <Snippet8>
        Dim value As String = "8"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "%h", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       08:00:00                              
        ' </Snippet8>
    End Sub

    Public Sub ParseHH()
        ' <Snippet9>
        Dim value As String = "08"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "hh", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       08:00:00                              
        ' </Snippet9>
    End Sub

    Private Sub hhSpecifier()
        ' <Snippet10>
        Dim ts1 As New TimeSpan(14, 3, 17)
        Console.WriteLine(ts1.ToString("d\.hh\:mm\:ss"))

        Dim ts2 As New TimeSpan(3, 4, 3, 17)
        Console.WriteLine(ts2.ToString("d\.hh\:mm\:ss"))
        ' The example displays the following output:
        '       0.14:03:17
        '       3.04:03:17
        ' </Snippet10>
    End Sub

    Public Sub ParseM()
        ' <Snippet11>
        Dim value As String = "3"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "%m", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       00:03:00                              
        ' </Snippet11>
    End Sub

    Private Sub mSpecifier()
        ' <Snippet12>
        Dim ts1 As New TimeSpan(0, 6, 32)
        Console.WriteLine("{0:m\:ss} minutes", ts1)

        Dim ts2 As New TimeSpan(0, 18, 44)
        Console.WriteLine("Elapsed time: {0:m\:ss}", ts2)
        ' The example displays the following output:
        '       6:32 minutes
        '       Elapsed time: 18:44
        ' </Snippet12>
    End Sub

    Public Sub ParseMM()
        ' <Snippet13>
        Dim value As String = "05"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "mm", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       00:05:00           
        ' </Snippet13>
    End Sub

    Private Sub mmSpecifier()
        ' <Snippet14>
        Dim departTime As New TimeSpan(11, 12, 00)
        Dim arriveTime As New TimeSpan(16, 28, 00)
        Console.WriteLine("Travel time: {0:hh\:mm}",
                          arriveTime - departTime)
        ' The example displays the following output:
        '       Travel time: 05:16      
        ' </Snippet14>
    End Sub

    Private Sub sSpecifier()
        ' <Snippet15>
        Dim ts As TimeSpan = TimeSpan.FromSeconds(12.465)
        Console.WriteLine(ts.ToString("%s"))
        ' The example displays the following output:
        '       12
        ' </Snippet15>
        Console.WriteLine()

        ' <Snippet16>
        Dim startTime As New TimeSpan(0, 12, 30, 15, 0)
        Dim endTime As New TimeSpan(0, 12, 30, 21, 3)
        Console.WriteLine("Elapsed Time: {0:s\:fff} seconds",
                          endTime - startTime)
        ' The example displays the following output:
        '       Elapsed Time: 6:003 seconds      
        ' </Snippet16>
    End Sub

    Public Sub ParseS()
        ' <Snippet17>
        Dim value As String = "9"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "%s", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       00:00:09
        ' </Snippet17>
    End Sub

    Public Sub ParseSS()
        ' <Snippet18>
        Dim values() As String = {"49", "9", "06"}
        Dim interval As TimeSpan
        For Each value As String In values
            If TimeSpan.TryParseExact(value, "ss", Nothing, interval) Then
                Console.WriteLine(interval.ToString("c"))
            Else
                Console.WriteLine("Unable to convert '{0}' to a time interval",
                                  value)
            End If
        Next
        ' The example displays the following output:
        '       00:00:49
        '       Unable to convert '9' to a time interval
        '       00:00:06
        ' </Snippet18>
    End Sub

    Private Sub ssSpecifier()
        ' <Snippet19>
        Dim interval1 As TimeSpan = TimeSpan.FromSeconds(12.60)
        Console.WriteLine(interval1.ToString("ss\.fff"))
        Dim interval2 As TimeSpan = TimeSpan.FromSeconds(6.485)
        Console.WriteLine(interval2.ToString("ss\.fff"))
        ' The example displays the following output:
        '       12.600
        '       06.485
        ' </Snippet19>
    End Sub
End Module

Back to table

The "ss" custom format specifier

The "ss" custom format specifier outputs the value of the System.TimeSpan.Seconds property, which represents the number of whole seconds in the time interval that isn't included as part of its hours, days, or minutes component. For values from 0 through 9, the output string includes a leading zero.

Ordinarily, in a parsing operation, an input string that includes only a single number is interpreted as the number of days. You can use the "ss" custom format specifier instead to interpret the numeric string as the number of seconds. The following example provides an illustration.

sample · csharpsamples/snippets/csharp/VS_Snippets_CLR/conceptual.timespan.custom/cs/customexamples1.cs18

csharp
using System;

public class Example
{
   public static void Main()
   {
      dSpecifier();
      Console.WriteLine("-------");
      ddSpecifier();
      Console.WriteLine("-------");
      hSpecifier();
      Console.WriteLine("-------");
      ParseH();
      Console.WriteLine("-------");
      ParseHH();
      Console.WriteLine("-------");
      hhSpecifier();
      Console.WriteLine("-------");
      ParseM();
      Console.WriteLine("-------");
      mSpecifier();
      Console.WriteLine("-------");
      ParseMM();
      Console.WriteLine("-------");
      mmSpecifier();
      Console.WriteLine("-------");
      sSpecifier();
      Console.WriteLine("-------");
      ParseS();
      Console.WriteLine("-------");
      ParseSS();
      Console.WriteLine("-------");
      ssSpecifier();
      Console.WriteLine("-------");
   }

   private static void dSpecifier()
   {
      // <Snippet3>
      TimeSpan ts1 = new TimeSpan(16, 4, 3, 17, 250);
      Console.WriteLine(ts1.ToString("%d"));
      // Displays 16
      // </Snippet3>
      Console.WriteLine();

      // <Snippet4>
      TimeSpan ts2 = new TimeSpan(4, 3, 17);
      Console.WriteLine(ts2.ToString(@"d\.hh\:mm\:ss"));

      TimeSpan ts3 = new TimeSpan(3, 4, 3, 17);
      Console.WriteLine(ts3.ToString(@"d\.hh\:mm\:ss"));
      // The example displays the following output:
      //       0.04:03:17
      //       3.04:03:17
      // </Snippet4>
   }

   private static void ddSpecifier()
   {
      // <Snippet5>
      TimeSpan ts1 = new TimeSpan(0, 23, 17, 47);
      TimeSpan ts2 = new TimeSpan(365, 21, 19, 45);

      for (int ctr = 2; ctr <= 8; ctr++)
      {
         string fmt = new String('d', ctr) + @"\.hh\:mm\:ss";
         Console.WriteLine($"{fmt} --> {ts1.ToString(fmt)}");
         Console.WriteLine($"{fmt} --> {ts2.ToString(fmt)}");
         Console.WriteLine();
      }
      // The example displays the following output:
      //       dd\.hh\:mm\:ss --> 00.23:17:47
      //       dd\.hh\:mm\:ss --> 365.21:19:45
      //
      //       ddd\.hh\:mm\:ss --> 000.23:17:47
      //       ddd\.hh\:mm\:ss --> 365.21:19:45
      //
      //       dddd\.hh\:mm\:ss --> 0000.23:17:47
      //       dddd\.hh\:mm\:ss --> 0365.21:19:45
      //
      //       ddddd\.hh\:mm\:ss --> 00000.23:17:47
      //       ddddd\.hh\:mm\:ss --> 00365.21:19:45
      //
      //       dddddd\.hh\:mm\:ss --> 000000.23:17:47
      //       dddddd\.hh\:mm\:ss --> 000365.21:19:45
      //
      //       ddddddd\.hh\:mm\:ss --> 0000000.23:17:47
      //       ddddddd\.hh\:mm\:ss --> 0000365.21:19:45
      //
      //       dddddddd\.hh\:mm\:ss --> 00000000.23:17:47
      //       dddddddd\.hh\:mm\:ss --> 00000365.21:19:45
      // </Snippet5>
   }

   private static void hSpecifier()
   {
      // <Snippet6>
      TimeSpan ts = new TimeSpan(3, 42, 0);
      Console.WriteLine($"{ts:%h} hours {ts:%m} minutes");
      // The example displays the following output:
      //       3 hours 42 minutes
      // </Snippet6>

      // <Snippet7>
      TimeSpan ts1 = new TimeSpan(14, 3, 17);
      Console.WriteLine(ts1.ToString(@"d\.h\:mm\:ss"));

      TimeSpan ts2 = new TimeSpan(3, 4, 3, 17);
      Console.WriteLine(ts2.ToString(@"d\.h\:mm\:ss"));
      // The example displays the following output:
      //       0.14:03:17
      //       3.4:03:17
      // </Snippet7>
   }

   private static void ParseH()
   {
      // <Snippet8>
      string value = "8";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "%h", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       08:00:00
      // </Snippet8>
   }

   private static void ParseHH()
   {
      // <Snippet9>
      string value = "08";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "hh", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       08:00:00
      // </Snippet9>
   }

   private static void hhSpecifier()
   {
      // <Snippet10>
      TimeSpan ts1 = new TimeSpan(14, 3, 17);
      Console.WriteLine(ts1.ToString(@"d\.hh\:mm\:ss"));

      TimeSpan ts2 = new TimeSpan(3, 4, 3, 17);
      Console.WriteLine(ts2.ToString(@"d\.hh\:mm\:ss"));
      // The example displays the following output:
      //       0.14:03:17
      //       3.04:03:17
      // </Snippet10>
   }

   private static void ParseM()
   {
      // <Snippet11>
      string value = "3";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "%m", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       00:03:00
      // </Snippet11>
   }

   private static void mSpecifier()
   {
      // <Snippet12>
      TimeSpan ts1 = new TimeSpan(0, 6, 32);
      Console.WriteLine($"{ts1:m\\:ss} minutes");

      TimeSpan ts2 = new TimeSpan(3, 4, 3, 17);
      Console.WriteLine($"Elapsed time: {ts2:m\\:ss}");
      // The example displays the following output:
      //       6:32 minutes
      //       Elapsed time: 18:44
      // </Snippet12>
   }

   private static void ParseMM()
   {
      // <Snippet13>
      string value = "07";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "mm", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       00:07:00
      // </Snippet13>
   }

   private static void mmSpecifier()
   {
      // <Snippet14>
      TimeSpan departTime = new TimeSpan(11, 12, 00);
      TimeSpan arriveTime = new TimeSpan(16, 28, 00);
      Console.WriteLine($"Travel time: {arriveTime - departTime:hh\\:mm}");
      // The example displays the following output:
      //       Travel time: 05:16
      // </Snippet14>
   }

   private static void sSpecifier()
   {
      // <Snippet15>
      TimeSpan ts = TimeSpan.FromSeconds(12.465);
      Console.WriteLine(ts.ToString("%s"));
      // The example displays the following output:
      //       12
      // </Snippet15>
      Console.WriteLine();

      // <Snippet16>
      TimeSpan startTime = new TimeSpan(0, 12, 30, 15, 0);
      TimeSpan endTime = new TimeSpan(0, 12, 30, 21, 3);
      Console.WriteLine(@"Elapsed Time: {0:s\:fff} seconds",
                        endTime - startTime);
      // The example displays the following output:
      //       Elapsed Time: 6:003 seconds
      // </Snippet16>
   }

   private static void ParseS()
   {
      // <Snippet17>
      string value = "9";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "%s", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       00:00:09
      // </Snippet17>
   }

   private static void ParseSS()
   {
      // <Snippet18>
      string[] values = { "49", "9", "06" };
      TimeSpan interval;
      foreach (string value in values)
      {
         if (TimeSpan.TryParseExact(value, "ss", null, out interval))
            Console.WriteLine(interval.ToString("c"));
         else
            Console.WriteLine($"Unable to convert '{value}' to a time interval");
      }
      // The example displays the following output:
      //       00:00:49
      //       Unable to convert '9' to a time interval
      //       00:00:06
      // </Snippet18>
   }

   private static void ssSpecifier()
   {
      // <Snippet19>
      TimeSpan interval1 = TimeSpan.FromSeconds(12.60);
      Console.WriteLine(interval1.ToString(@"ss\.fff"));

      TimeSpan interval2 = TimeSpan.FromSeconds(6.485);
      Console.WriteLine(interval2.ToString(@"ss\.fff"));
      // The example displays the following output:
      //       12.600
      //       06.485
      // </Snippet19>
   }
}

sample · vbsamples/snippets/visualbasic/VS_Snippets_CLR/conceptual.timespan.custom/vb/customexamples1.vb18

vb
' Visual Basic .NET Document
Option Strict On

Module Example
    Public Sub Main()
        dSpecifier()
        Console.WriteLine("-------")
        ddSpecifier()
        Console.WriteLine("-------")
        hSpecifier()
        Console.WriteLine("-------")
        ParseH()
        Console.WriteLine("-------")
        ParseHH()
        Console.WriteLine("-------")
        hhSpecifier()
        Console.WriteLine("-------")
        ParseM()
        Console.WriteLine("-------")
        mSpecifier()
        Console.WriteLine("-------")
        ParseMM()
        Console.WriteLine("-------")
        mmSpecifier()
        Console.WriteLine("-------")
        sSpecifier()
        Console.WriteLine("-------")
        ParseS()
        Console.WriteLine("-------")
        ParseSS()
        Console.WriteLine("-------")
        ssSpecifier()
        Console.WriteLine("-------")
    End Sub

    Private Sub dSpecifier()
        ' <Snippet3>
        Dim ts As New TimeSpan(16, 4, 3, 17, 250)
        Console.WriteLine(ts.ToString("%d"))
        ' Displays 16   
        ' </Snippet3>
        Console.WriteLine()

        ' <Snippet4>
        Dim ts2 As New TimeSpan(4, 3, 17)
        Console.WriteLine(ts2.ToString("d\.hh\:mm\:ss"))

        Dim ts3 As New TimeSpan(3, 4, 3, 17)
        Console.WriteLine(ts3.ToString("d\.hh\:mm\:ss"))
        ' The example displays the following output:
        '       0.04:03:17
        '       3.04:03:17      
        ' </Snippet4>
    End Sub

    Private Sub ddSpecifier()
        ' <Snippet5>
        Dim ts1 As New TimeSpan(0, 23, 17, 47)
        Dim ts2 As New TimeSpan(365, 21, 19, 45)

        For ctr As Integer = 2 To 8
            Dim fmt As String = New String("d"c, ctr) + "\.hh\:mm\:ss"
            Console.WriteLine("{0} --> {1:" + fmt + "}", fmt, ts1)
            Console.WriteLine("{0} --> {1:" + fmt + "}", fmt, ts2)
            Console.WriteLine()
        Next
        ' The example displays the following output:
        '       dd\.hh\:mm\:ss --> 00.23:17:47
        '       dd\.hh\:mm\:ss --> 365.21:19:45
        '       
        '       ddd\.hh\:mm\:ss --> 000.23:17:47
        '       ddd\.hh\:mm\:ss --> 365.21:19:45
        '       
        '       dddd\.hh\:mm\:ss --> 0000.23:17:47
        '       dddd\.hh\:mm\:ss --> 0365.21:19:45
        '       
        '       ddddd\.hh\:mm\:ss --> 00000.23:17:47
        '       ddddd\.hh\:mm\:ss --> 00365.21:19:45
        '       
        '       dddddd\.hh\:mm\:ss --> 000000.23:17:47
        '       dddddd\.hh\:mm\:ss --> 000365.21:19:45
        '       
        '       ddddddd\.hh\:mm\:ss --> 0000000.23:17:47
        '       ddddddd\.hh\:mm\:ss --> 0000365.21:19:45
        '       
        '       dddddddd\.hh\:mm\:ss --> 00000000.23:17:47
        '       dddddddd\.hh\:mm\:ss --> 00000365.21:19:45      
        ' </Snippet5>
    End Sub

    Private Sub hSpecifier()
        ' <Snippet6>
        Dim ts As New TimeSpan(3, 42, 0)
        Console.WriteLine("{0:%h} hours {0:%m} minutes", ts)
        ' The example displays the following output:
        '       3 hours 42 minutes
        ' </Snippet6>
        Console.WriteLine()

        ' <Snippet7>
        Dim ts1 As New TimeSpan(14, 3, 17)
        Console.WriteLine(ts1.ToString("d\.h\:mm\:ss"))

        Dim ts2 As New TimeSpan(3, 4, 3, 17)
        Console.WriteLine(ts2.ToString("d\.h\:mm\:ss"))
        ' The example displays the following output:
        '       0.14:03:17
        '       3.4:03:17
        ' </Snippet7>
    End Sub

    Public Sub ParseH()
        ' <Snippet8>
        Dim value As String = "8"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "%h", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       08:00:00                              
        ' </Snippet8>
    End Sub

    Public Sub ParseHH()
        ' <Snippet9>
        Dim value As String = "08"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "hh", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       08:00:00                              
        ' </Snippet9>
    End Sub

    Private Sub hhSpecifier()
        ' <Snippet10>
        Dim ts1 As New TimeSpan(14, 3, 17)
        Console.WriteLine(ts1.ToString("d\.hh\:mm\:ss"))

        Dim ts2 As New TimeSpan(3, 4, 3, 17)
        Console.WriteLine(ts2.ToString("d\.hh\:mm\:ss"))
        ' The example displays the following output:
        '       0.14:03:17
        '       3.04:03:17
        ' </Snippet10>
    End Sub

    Public Sub ParseM()
        ' <Snippet11>
        Dim value As String = "3"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "%m", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       00:03:00                              
        ' </Snippet11>
    End Sub

    Private Sub mSpecifier()
        ' <Snippet12>
        Dim ts1 As New TimeSpan(0, 6, 32)
        Console.WriteLine("{0:m\:ss} minutes", ts1)

        Dim ts2 As New TimeSpan(0, 18, 44)
        Console.WriteLine("Elapsed time: {0:m\:ss}", ts2)
        ' The example displays the following output:
        '       6:32 minutes
        '       Elapsed time: 18:44
        ' </Snippet12>
    End Sub

    Public Sub ParseMM()
        ' <Snippet13>
        Dim value As String = "05"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "mm", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       00:05:00           
        ' </Snippet13>
    End Sub

    Private Sub mmSpecifier()
        ' <Snippet14>
        Dim departTime As New TimeSpan(11, 12, 00)
        Dim arriveTime As New TimeSpan(16, 28, 00)
        Console.WriteLine("Travel time: {0:hh\:mm}",
                          arriveTime - departTime)
        ' The example displays the following output:
        '       Travel time: 05:16      
        ' </Snippet14>
    End Sub

    Private Sub sSpecifier()
        ' <Snippet15>
        Dim ts As TimeSpan = TimeSpan.FromSeconds(12.465)
        Console.WriteLine(ts.ToString("%s"))
        ' The example displays the following output:
        '       12
        ' </Snippet15>
        Console.WriteLine()

        ' <Snippet16>
        Dim startTime As New TimeSpan(0, 12, 30, 15, 0)
        Dim endTime As New TimeSpan(0, 12, 30, 21, 3)
        Console.WriteLine("Elapsed Time: {0:s\:fff} seconds",
                          endTime - startTime)
        ' The example displays the following output:
        '       Elapsed Time: 6:003 seconds      
        ' </Snippet16>
    End Sub

    Public Sub ParseS()
        ' <Snippet17>
        Dim value As String = "9"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "%s", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       00:00:09
        ' </Snippet17>
    End Sub

    Public Sub ParseSS()
        ' <Snippet18>
        Dim values() As String = {"49", "9", "06"}
        Dim interval As TimeSpan
        For Each value As String In values
            If TimeSpan.TryParseExact(value, "ss", Nothing, interval) Then
                Console.WriteLine(interval.ToString("c"))
            Else
                Console.WriteLine("Unable to convert '{0}' to a time interval",
                                  value)
            End If
        Next
        ' The example displays the following output:
        '       00:00:49
        '       Unable to convert '9' to a time interval
        '       00:00:06
        ' </Snippet18>
    End Sub

    Private Sub ssSpecifier()
        ' <Snippet19>
        Dim interval1 As TimeSpan = TimeSpan.FromSeconds(12.60)
        Console.WriteLine(interval1.ToString("ss\.fff"))
        Dim interval2 As TimeSpan = TimeSpan.FromSeconds(6.485)
        Console.WriteLine(interval2.ToString("ss\.fff"))
        ' The example displays the following output:
        '       12.600
        '       06.485
        ' </Snippet19>
    End Sub
End Module

The following example illustrates the use of the "ss" custom format specifier.

sample · csharpsamples/snippets/csharp/VS_Snippets_CLR/conceptual.timespan.custom/cs/customexamples1.cs19

csharp
using System;

public class Example
{
   public static void Main()
   {
      dSpecifier();
      Console.WriteLine("-------");
      ddSpecifier();
      Console.WriteLine("-------");
      hSpecifier();
      Console.WriteLine("-------");
      ParseH();
      Console.WriteLine("-------");
      ParseHH();
      Console.WriteLine("-------");
      hhSpecifier();
      Console.WriteLine("-------");
      ParseM();
      Console.WriteLine("-------");
      mSpecifier();
      Console.WriteLine("-------");
      ParseMM();
      Console.WriteLine("-------");
      mmSpecifier();
      Console.WriteLine("-------");
      sSpecifier();
      Console.WriteLine("-------");
      ParseS();
      Console.WriteLine("-------");
      ParseSS();
      Console.WriteLine("-------");
      ssSpecifier();
      Console.WriteLine("-------");
   }

   private static void dSpecifier()
   {
      // <Snippet3>
      TimeSpan ts1 = new TimeSpan(16, 4, 3, 17, 250);
      Console.WriteLine(ts1.ToString("%d"));
      // Displays 16
      // </Snippet3>
      Console.WriteLine();

      // <Snippet4>
      TimeSpan ts2 = new TimeSpan(4, 3, 17);
      Console.WriteLine(ts2.ToString(@"d\.hh\:mm\:ss"));

      TimeSpan ts3 = new TimeSpan(3, 4, 3, 17);
      Console.WriteLine(ts3.ToString(@"d\.hh\:mm\:ss"));
      // The example displays the following output:
      //       0.04:03:17
      //       3.04:03:17
      // </Snippet4>
   }

   private static void ddSpecifier()
   {
      // <Snippet5>
      TimeSpan ts1 = new TimeSpan(0, 23, 17, 47);
      TimeSpan ts2 = new TimeSpan(365, 21, 19, 45);

      for (int ctr = 2; ctr <= 8; ctr++)
      {
         string fmt = new String('d', ctr) + @"\.hh\:mm\:ss";
         Console.WriteLine($"{fmt} --> {ts1.ToString(fmt)}");
         Console.WriteLine($"{fmt} --> {ts2.ToString(fmt)}");
         Console.WriteLine();
      }
      // The example displays the following output:
      //       dd\.hh\:mm\:ss --> 00.23:17:47
      //       dd\.hh\:mm\:ss --> 365.21:19:45
      //
      //       ddd\.hh\:mm\:ss --> 000.23:17:47
      //       ddd\.hh\:mm\:ss --> 365.21:19:45
      //
      //       dddd\.hh\:mm\:ss --> 0000.23:17:47
      //       dddd\.hh\:mm\:ss --> 0365.21:19:45
      //
      //       ddddd\.hh\:mm\:ss --> 00000.23:17:47
      //       ddddd\.hh\:mm\:ss --> 00365.21:19:45
      //
      //       dddddd\.hh\:mm\:ss --> 000000.23:17:47
      //       dddddd\.hh\:mm\:ss --> 000365.21:19:45
      //
      //       ddddddd\.hh\:mm\:ss --> 0000000.23:17:47
      //       ddddddd\.hh\:mm\:ss --> 0000365.21:19:45
      //
      //       dddddddd\.hh\:mm\:ss --> 00000000.23:17:47
      //       dddddddd\.hh\:mm\:ss --> 00000365.21:19:45
      // </Snippet5>
   }

   private static void hSpecifier()
   {
      // <Snippet6>
      TimeSpan ts = new TimeSpan(3, 42, 0);
      Console.WriteLine($"{ts:%h} hours {ts:%m} minutes");
      // The example displays the following output:
      //       3 hours 42 minutes
      // </Snippet6>

      // <Snippet7>
      TimeSpan ts1 = new TimeSpan(14, 3, 17);
      Console.WriteLine(ts1.ToString(@"d\.h\:mm\:ss"));

      TimeSpan ts2 = new TimeSpan(3, 4, 3, 17);
      Console.WriteLine(ts2.ToString(@"d\.h\:mm\:ss"));
      // The example displays the following output:
      //       0.14:03:17
      //       3.4:03:17
      // </Snippet7>
   }

   private static void ParseH()
   {
      // <Snippet8>
      string value = "8";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "%h", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       08:00:00
      // </Snippet8>
   }

   private static void ParseHH()
   {
      // <Snippet9>
      string value = "08";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "hh", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       08:00:00
      // </Snippet9>
   }

   private static void hhSpecifier()
   {
      // <Snippet10>
      TimeSpan ts1 = new TimeSpan(14, 3, 17);
      Console.WriteLine(ts1.ToString(@"d\.hh\:mm\:ss"));

      TimeSpan ts2 = new TimeSpan(3, 4, 3, 17);
      Console.WriteLine(ts2.ToString(@"d\.hh\:mm\:ss"));
      // The example displays the following output:
      //       0.14:03:17
      //       3.04:03:17
      // </Snippet10>
   }

   private static void ParseM()
   {
      // <Snippet11>
      string value = "3";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "%m", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       00:03:00
      // </Snippet11>
   }

   private static void mSpecifier()
   {
      // <Snippet12>
      TimeSpan ts1 = new TimeSpan(0, 6, 32);
      Console.WriteLine($"{ts1:m\\:ss} minutes");

      TimeSpan ts2 = new TimeSpan(3, 4, 3, 17);
      Console.WriteLine($"Elapsed time: {ts2:m\\:ss}");
      // The example displays the following output:
      //       6:32 minutes
      //       Elapsed time: 18:44
      // </Snippet12>
   }

   private static void ParseMM()
   {
      // <Snippet13>
      string value = "07";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "mm", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       00:07:00
      // </Snippet13>
   }

   private static void mmSpecifier()
   {
      // <Snippet14>
      TimeSpan departTime = new TimeSpan(11, 12, 00);
      TimeSpan arriveTime = new TimeSpan(16, 28, 00);
      Console.WriteLine($"Travel time: {arriveTime - departTime:hh\\:mm}");
      // The example displays the following output:
      //       Travel time: 05:16
      // </Snippet14>
   }

   private static void sSpecifier()
   {
      // <Snippet15>
      TimeSpan ts = TimeSpan.FromSeconds(12.465);
      Console.WriteLine(ts.ToString("%s"));
      // The example displays the following output:
      //       12
      // </Snippet15>
      Console.WriteLine();

      // <Snippet16>
      TimeSpan startTime = new TimeSpan(0, 12, 30, 15, 0);
      TimeSpan endTime = new TimeSpan(0, 12, 30, 21, 3);
      Console.WriteLine(@"Elapsed Time: {0:s\:fff} seconds",
                        endTime - startTime);
      // The example displays the following output:
      //       Elapsed Time: 6:003 seconds
      // </Snippet16>
   }

   private static void ParseS()
   {
      // <Snippet17>
      string value = "9";
      TimeSpan interval;
      if (TimeSpan.TryParseExact(value, "%s", null, out interval))
         Console.WriteLine(interval.ToString("c"));
      else
         Console.WriteLine($"Unable to convert '{value}' to a time interval");
      // The example displays the following output:
      //       00:00:09
      // </Snippet17>
   }

   private static void ParseSS()
   {
      // <Snippet18>
      string[] values = { "49", "9", "06" };
      TimeSpan interval;
      foreach (string value in values)
      {
         if (TimeSpan.TryParseExact(value, "ss", null, out interval))
            Console.WriteLine(interval.ToString("c"));
         else
            Console.WriteLine($"Unable to convert '{value}' to a time interval");
      }
      // The example displays the following output:
      //       00:00:49
      //       Unable to convert '9' to a time interval
      //       00:00:06
      // </Snippet18>
   }

   private static void ssSpecifier()
   {
      // <Snippet19>
      TimeSpan interval1 = TimeSpan.FromSeconds(12.60);
      Console.WriteLine(interval1.ToString(@"ss\.fff"));

      TimeSpan interval2 = TimeSpan.FromSeconds(6.485);
      Console.WriteLine(interval2.ToString(@"ss\.fff"));
      // The example displays the following output:
      //       12.600
      //       06.485
      // </Snippet19>
   }
}

sample · vbsamples/snippets/visualbasic/VS_Snippets_CLR/conceptual.timespan.custom/vb/customexamples1.vb19

vb
' Visual Basic .NET Document
Option Strict On

Module Example
    Public Sub Main()
        dSpecifier()
        Console.WriteLine("-------")
        ddSpecifier()
        Console.WriteLine("-------")
        hSpecifier()
        Console.WriteLine("-------")
        ParseH()
        Console.WriteLine("-------")
        ParseHH()
        Console.WriteLine("-------")
        hhSpecifier()
        Console.WriteLine("-------")
        ParseM()
        Console.WriteLine("-------")
        mSpecifier()
        Console.WriteLine("-------")
        ParseMM()
        Console.WriteLine("-------")
        mmSpecifier()
        Console.WriteLine("-------")
        sSpecifier()
        Console.WriteLine("-------")
        ParseS()
        Console.WriteLine("-------")
        ParseSS()
        Console.WriteLine("-------")
        ssSpecifier()
        Console.WriteLine("-------")
    End Sub

    Private Sub dSpecifier()
        ' <Snippet3>
        Dim ts As New TimeSpan(16, 4, 3, 17, 250)
        Console.WriteLine(ts.ToString("%d"))
        ' Displays 16   
        ' </Snippet3>
        Console.WriteLine()

        ' <Snippet4>
        Dim ts2 As New TimeSpan(4, 3, 17)
        Console.WriteLine(ts2.ToString("d\.hh\:mm\:ss"))

        Dim ts3 As New TimeSpan(3, 4, 3, 17)
        Console.WriteLine(ts3.ToString("d\.hh\:mm\:ss"))
        ' The example displays the following output:
        '       0.04:03:17
        '       3.04:03:17      
        ' </Snippet4>
    End Sub

    Private Sub ddSpecifier()
        ' <Snippet5>
        Dim ts1 As New TimeSpan(0, 23, 17, 47)
        Dim ts2 As New TimeSpan(365, 21, 19, 45)

        For ctr As Integer = 2 To 8
            Dim fmt As String = New String("d"c, ctr) + "\.hh\:mm\:ss"
            Console.WriteLine("{0} --> {1:" + fmt + "}", fmt, ts1)
            Console.WriteLine("{0} --> {1:" + fmt + "}", fmt, ts2)
            Console.WriteLine()
        Next
        ' The example displays the following output:
        '       dd\.hh\:mm\:ss --> 00.23:17:47
        '       dd\.hh\:mm\:ss --> 365.21:19:45
        '       
        '       ddd\.hh\:mm\:ss --> 000.23:17:47
        '       ddd\.hh\:mm\:ss --> 365.21:19:45
        '       
        '       dddd\.hh\:mm\:ss --> 0000.23:17:47
        '       dddd\.hh\:mm\:ss --> 0365.21:19:45
        '       
        '       ddddd\.hh\:mm\:ss --> 00000.23:17:47
        '       ddddd\.hh\:mm\:ss --> 00365.21:19:45
        '       
        '       dddddd\.hh\:mm\:ss --> 000000.23:17:47
        '       dddddd\.hh\:mm\:ss --> 000365.21:19:45
        '       
        '       ddddddd\.hh\:mm\:ss --> 0000000.23:17:47
        '       ddddddd\.hh\:mm\:ss --> 0000365.21:19:45
        '       
        '       dddddddd\.hh\:mm\:ss --> 00000000.23:17:47
        '       dddddddd\.hh\:mm\:ss --> 00000365.21:19:45      
        ' </Snippet5>
    End Sub

    Private Sub hSpecifier()
        ' <Snippet6>
        Dim ts As New TimeSpan(3, 42, 0)
        Console.WriteLine("{0:%h} hours {0:%m} minutes", ts)
        ' The example displays the following output:
        '       3 hours 42 minutes
        ' </Snippet6>
        Console.WriteLine()

        ' <Snippet7>
        Dim ts1 As New TimeSpan(14, 3, 17)
        Console.WriteLine(ts1.ToString("d\.h\:mm\:ss"))

        Dim ts2 As New TimeSpan(3, 4, 3, 17)
        Console.WriteLine(ts2.ToString("d\.h\:mm\:ss"))
        ' The example displays the following output:
        '       0.14:03:17
        '       3.4:03:17
        ' </Snippet7>
    End Sub

    Public Sub ParseH()
        ' <Snippet8>
        Dim value As String = "8"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "%h", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       08:00:00                              
        ' </Snippet8>
    End Sub

    Public Sub ParseHH()
        ' <Snippet9>
        Dim value As String = "08"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "hh", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       08:00:00                              
        ' </Snippet9>
    End Sub

    Private Sub hhSpecifier()
        ' <Snippet10>
        Dim ts1 As New TimeSpan(14, 3, 17)
        Console.WriteLine(ts1.ToString("d\.hh\:mm\:ss"))

        Dim ts2 As New TimeSpan(3, 4, 3, 17)
        Console.WriteLine(ts2.ToString("d\.hh\:mm\:ss"))
        ' The example displays the following output:
        '       0.14:03:17
        '       3.04:03:17
        ' </Snippet10>
    End Sub

    Public Sub ParseM()
        ' <Snippet11>
        Dim value As String = "3"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "%m", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       00:03:00                              
        ' </Snippet11>
    End Sub

    Private Sub mSpecifier()
        ' <Snippet12>
        Dim ts1 As New TimeSpan(0, 6, 32)
        Console.WriteLine("{0:m\:ss} minutes", ts1)

        Dim ts2 As New TimeSpan(0, 18, 44)
        Console.WriteLine("Elapsed time: {0:m\:ss}", ts2)
        ' The example displays the following output:
        '       6:32 minutes
        '       Elapsed time: 18:44
        ' </Snippet12>
    End Sub

    Public Sub ParseMM()
        ' <Snippet13>
        Dim value As String = "05"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "mm", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       00:05:00           
        ' </Snippet13>
    End Sub

    Private Sub mmSpecifier()
        ' <Snippet14>
        Dim departTime As New TimeSpan(11, 12, 00)
        Dim arriveTime As New TimeSpan(16, 28, 00)
        Console.WriteLine("Travel time: {0:hh\:mm}",
                          arriveTime - departTime)
        ' The example displays the following output:
        '       Travel time: 05:16      
        ' </Snippet14>
    End Sub

    Private Sub sSpecifier()
        ' <Snippet15>
        Dim ts As TimeSpan = TimeSpan.FromSeconds(12.465)
        Console.WriteLine(ts.ToString("%s"))
        ' The example displays the following output:
        '       12
        ' </Snippet15>
        Console.WriteLine()

        ' <Snippet16>
        Dim startTime As New TimeSpan(0, 12, 30, 15, 0)
        Dim endTime As New TimeSpan(0, 12, 30, 21, 3)
        Console.WriteLine("Elapsed Time: {0:s\:fff} seconds",
                          endTime - startTime)
        ' The example displays the following output:
        '       Elapsed Time: 6:003 seconds      
        ' </Snippet16>
    End Sub

    Public Sub ParseS()
        ' <Snippet17>
        Dim value As String = "9"
        Dim interval As TimeSpan
        If TimeSpan.TryParseExact(value, "%s", Nothing, interval) Then
            Console.WriteLine(interval.ToString("c"))
        Else
            Console.WriteLine("Unable to convert '{0}' to a time interval",
                              value)
        End If
        ' The example displays the following output:
        '       00:00:09
        ' </Snippet17>
    End Sub

    Public Sub ParseSS()
        ' <Snippet18>
        Dim values() As String = {"49", "9", "06"}
        Dim interval As TimeSpan
        For Each value As String In values
            If TimeSpan.TryParseExact(value, "ss", Nothing, interval) Then
                Console.WriteLine(interval.ToString("c"))
            Else
                Console.WriteLine("Unable to convert '{0}' to a time interval",
                                  value)
            End If
        Next
        ' The example displays the following output:
        '       00:00:49
        '       Unable to convert '9' to a time interval
        '       00:00:06
        ' </Snippet18>
    End Sub

    Private Sub ssSpecifier()
        ' <Snippet19>
        Dim interval1 As TimeSpan = TimeSpan.FromSeconds(12.60)
        Console.WriteLine(interval1.ToString("ss\.fff"))
        Dim interval2 As TimeSpan = TimeSpan.FromSeconds(6.485)
        Console.WriteLine(interval2.ToString("ss\.fff"))
        ' The example displays the following output:
        '       12.600
        '       06.485
        ' </Snippet19>
    End Sub
End Module

Back to table

The "f" custom format specifier

The "f" custom format specifier outputs the tenths of a second in a time interval. In a formatting operation, any remaining fractional digits are truncated. In a parsing operation that calls the System.TimeSpan.ParseExact or System.TimeSpan.TryParseExact method, the input string must contain exactly one fractional digit.

If the "f" custom format specifier is used alone, specify "%f" so that it isn't misinterpreted as a standard format string.

The following example uses the "f" custom format specifier to display the tenths of a second in a System.TimeSpan value. "f" is used first as the only format specifier, and then combined with the "s" specifier in a custom format string.

sample · csharpsamples/snippets/csharp/VS_Snippets_CLR/conceptual.timespan.custom/cs/fspecifiers1.cs20

csharp
using System;

public class Example
{
   public static void Main()
   {
      // <Snippet20>
      TimeSpan ts = new TimeSpan(1003498765432);
      string fmt;
      Console.WriteLine(ts.ToString("c"));
      Console.WriteLine();

      for (int ctr = 1; ctr <= 7; ctr++) {
         fmt = new string('f', ctr);
         if (fmt.Length == 1) fmt = "%" + fmt;
         Console.WriteLine($"{fmt,10}: {ts.ToString(fmt)}");
      }
      Console.WriteLine();

      for (int ctr = 1; ctr <= 7; ctr++) {
         fmt = $"s\\.{new string('f', ctr)}";
         Console.WriteLine($"{fmt,10}: {ts.ToString(fmt)}");
      }
      // The example displays the following output:
      //               %f: 8
      //               ff: 87
      //              fff: 876
      //             ffff: 8765
      //            fffff: 87654
      //           ffffff: 876543
      //          fffffff: 8765432
      //
      //              s\.f: 29.8
      //             s\.ff: 29.87
      //            s\.fff: 29.876
      //           s\.ffff: 29.8765
      //          s\.fffff: 29.87654
      //         s\.ffffff: 29.876543
      //        s\.fffffff: 29.8765432
      // </Snippet20>
   }
}

sample · vbsamples/snippets/visualbasic/VS_Snippets_CLR/conceptual.timespan.custom/vb/fspecifiers1.vb20

vb
' Visual Basic .NET Document
Option Strict On

Module Example
    Public Sub Main()
        ShowSingleSpecifier()
    End Sub

    Private Sub ShowSingleSpecifier()
        ' <Snippet20>
        Dim ts As New TimeSpan(1003498765432)
        Dim fmt As String
        Console.WriteLine(ts.ToString("c"))
        Console.WriteLine()

        For ctr = 1 To 7
            fmt = New String("f"c, ctr)
            If fmt.Length = 1 Then fmt = "%" + fmt
            Console.WriteLine("{0,10}: {1:" + fmt + "}", fmt, ts)
        Next
        Console.WriteLine()

        For ctr = 1 To 7
            fmt = New String("f"c, ctr)
            Console.WriteLine("{0,10}: {1:s\." + fmt + "}", "s\." + fmt, ts)
        Next
        ' The example displays the following output:
        '            %f: 8
        '            ff: 87
        '           fff: 876
        '          ffff: 8765
        '         fffff: 87654
        '        ffffff: 876543
        '       fffffff: 8765432
        '    
        '           s\.f: 29.8
        '          s\.ff: 29.87
        '         s\.fff: 29.876
        '        s\.ffff: 29.8765
        '       s\.fffff: 29.87654
        '      s\.ffffff: 29.876543
        '     s\.fffffff: 29.8765432
        ' </Snippet20>
    End Sub
End Module

Back to table

The "ff" custom format specifier

The "ff" custom format specifier outputs the hundredths of a second in a time interval. In a formatting operation, any remaining fractional digits are truncated. In a parsing operation that calls the System.TimeSpan.ParseExact or System.TimeSpan.TryParseExact method, the input string must contain exactly two fractional digits.

The following example uses the "ff" custom format specifier to display the hundredths of a second in a System.TimeSpan value. "ff" is used first as the only format specifier, and then combined with the "s" specifier in a custom format string.

sample · csharpsamples/snippets/csharp/VS_Snippets_CLR/conceptual.timespan.custom/cs/fspecifiers1.cs20

csharp
using System;

public class Example
{
   public static void Main()
   {
      // <Snippet20>
      TimeSpan ts = new TimeSpan(1003498765432);
      string fmt;
      Console.WriteLine(ts.ToString("c"));
      Console.WriteLine();

      for (int ctr = 1; ctr <= 7; ctr++) {
         fmt = new string('f', ctr);
         if (fmt.Length == 1) fmt = "%" + fmt;
         Console.WriteLine($"{fmt,10}: {ts.ToString(fmt)}");
      }
      Console.WriteLine();

      for (int ctr = 1; ctr <= 7; ctr++) {
         fmt = $"s\\.{new string('f', ctr)}";
         Console.WriteLine($"{fmt,10}: {ts.ToString(fmt)}");
      }
      // The example displays the following output:
      //               %f: 8
      //               ff: 87
      //              fff: 876
      //             ffff: 8765
      //            fffff: 87654
      //           ffffff: 876543
      //          fffffff: 8765432
      //
      //              s\.f: 29.8
      //             s\.ff: 29.87
      //            s\.fff: 29.876
      //           s\.ffff: 29.8765
      //          s\.fffff: 29.87654
      //         s\.ffffff: 29.876543
      //        s\.fffffff: 29.8765432
      // </Snippet20>
   }
}

sample · vbsamples/snippets/visualbasic/VS_Snippets_CLR/conceptual.timespan.custom/vb/fspecifiers1.vb20

vb
' Visual Basic .NET Document
Option Strict On

Module Example
    Public Sub Main()
        ShowSingleSpecifier()
    End Sub

    Private Sub ShowSingleSpecifier()
        ' <Snippet20>
        Dim ts As New TimeSpan(1003498765432)
        Dim fmt As String
        Console.WriteLine(ts.ToString("c"))
        Console.WriteLine()

        For ctr = 1 To 7
            fmt = New String("f"c, ctr)
            If fmt.Length = 1 Then fmt = "%" + fmt
            Console.WriteLine("{0,10}: {1:" + fmt + "}", fmt, ts)
        Next
        Console.WriteLine()

        For ctr = 1 To 7
            fmt = New String("f"c, ctr)
            Console.WriteLine("{0,10}: {1:s\." + fmt + "}", "s\." + fmt, ts)
        Next
        ' The example displays the following output:
        '            %f: 8
        '            ff: 87
        '           fff: 876
        '          ffff: 8765
        '         fffff: 87654
        '        ffffff: 876543
        '       fffffff: 8765432
        '    
        '           s\.f: 29.8
        '          s\.ff: 29.87
        '         s\.fff: 29.876
        '        s\.ffff: 29.8765
        '       s\.fffff: 29.87654
        '      s\.ffffff: 29.876543
        '     s\.fffffff: 29.8765432
        ' </Snippet20>
    End Sub
End Module

Back to table

The "fff" custom format specifier

The "fff" custom format specifier (with three f characters) outputs the milliseconds in a time interval. In a formatting operation, any remaining fractional digits are truncated. In a parsing operation that calls the System.TimeSpan.ParseExact or System.TimeSpan.TryParseExact method, the input string must contain exactly three fractional digits.

The following example uses the "fff" custom format specifier to display the milliseconds in a System.TimeSpan value. "fff" is used first as the only format specifier, and then combined with the "s" specifier in a custom format string.

sample · csharpsamples/snippets/csharp/VS_Snippets_CLR/conceptual.timespan.custom/cs/fspecifiers1.cs20

csharp
using System;

public class Example
{
   public static void Main()
   {
      // <Snippet20>
      TimeSpan ts = new TimeSpan(1003498765432);
      string fmt;
      Console.WriteLine(ts.ToString("c"));
      Console.WriteLine();

      for (int ctr = 1; ctr <= 7; ctr++) {
         fmt = new string('f', ctr);
         if (fmt.Length == 1) fmt = "%" + fmt;
         Console.WriteLine($"{fmt,10}: {ts.ToString(fmt)}");
      }
      Console.WriteLine();

      for (int ctr = 1; ctr <= 7; ctr++) {
         fmt = $"s\\.{new string('f', ctr)}";
         Console.WriteLine($"{fmt,10}: {ts.ToString(fmt)}");
      }
      // The example displays the following output:
      //               %f: 8
      //               ff: 87
      //              fff: 876
      //             ffff: 8765
      //            fffff: 87654
      //           ffffff: 876543
      //          fffffff: 8765432
      //
      //              s\.f: 29.8
      //             s\.ff: 29.87
      //            s\.fff: 29.876
      //           s\.ffff: 29.8765
      //          s\.fffff: 29.87654
      //         s\.ffffff: 29.876543
      //        s\.fffffff: 29.8765432
      // </Snippet20>
   }
}

sample · vbsamples/snippets/visualbasic/VS_Snippets_CLR/conceptual.timespan.custom/vb/fspecifiers1.vb20

vb
' Visual Basic .NET Document
Option Strict On

Module Example
    Public Sub Main()
        ShowSingleSpecifier()
    End Sub

    Private Sub ShowSingleSpecifier()
        ' <Snippet20>
        Dim ts As New TimeSpan(1003498765432)
        Dim fmt As String
        Console.WriteLine(ts.ToString("c"))
        Console.WriteLine()

        For ctr = 1 To 7
            fmt = New String("f"c, ctr)
            If fmt.Length = 1 Then fmt = "%" + fmt
            Console.WriteLine("{0,10}: {1:" + fmt + "}", fmt, ts)
        Next
        Console.WriteLine()

        For ctr = 1 To 7
            fmt = New String("f"c, ctr)
            Console.WriteLine("{0,10}: {1:s\." + fmt + "}", "s\." + fmt, ts)
        Next
        ' The example displays the following output:
        '            %f: 8
        '            ff: 87
        '           fff: 876
        '          ffff: 8765
        '         fffff: 87654
        '        ffffff: 876543
        '       fffffff: 8765432
        '    
        '           s\.f: 29.8
        '          s\.ff: 29.87
        '         s\.fff: 29.876
        '        s\.ffff: 29.8765
        '       s\.fffff: 29.87654
        '      s\.ffffff: 29.876543
        '     s\.fffffff: 29.8765432
        ' </Snippet20>
    End Sub
End Module

Back to table

The "ffff" custom format specifier

The "ffff" custom format specifier (with four f characters) outputs the ten-thousandths of a second in a time interval. In a formatting operation, any remaining fractional digits are truncated. In a parsing operation that calls the System.TimeSpan.ParseExact or System.TimeSpan.TryParseExact method, the input string must contain exactly four fractional digits.

The following example uses the "ffff" custom format specifier to display the ten-thousandths of a second in a System.TimeSpan value. "ffff" is used first as the only format specifier, and then combined with the "s" specifier in a custom format string.

sample · csharpsamples/snippets/csharp/VS_Snippets_CLR/conceptual.timespan.custom/cs/fspecifiers1.cs20

csharp
using System;

public class Example
{
   public static void Main()
   {
      // <Snippet20>
      TimeSpan ts = new TimeSpan(1003498765432);
      string fmt;
      Console.WriteLine(ts.ToString("c"));
      Console.WriteLine();

      for (int ctr = 1; ctr <= 7; ctr++) {
         fmt = new string('f', ctr);
         if (fmt.Length == 1) fmt = "%" + fmt;
         Console.WriteLine($"{fmt,10}: {ts.ToString(fmt)}");
      }
      Console.WriteLine();

      for (int ctr = 1; ctr <= 7; ctr++) {
         fmt = $"s\\.{new string('f', ctr)}";
         Console.WriteLine($"{fmt,10}: {ts.ToString(fmt)}");
      }
      // The example displays the following output:
      //               %f: 8
      //               ff: 87
      //              fff: 876
      //             ffff: 8765
      //            fffff: 87654
      //           ffffff: 876543
      //          fffffff: 8765432
      //
      //              s\.f: 29.8
      //             s\.ff: 29.87
      //            s\.fff: 29.876
      //           s\.ffff: 29.8765
      //          s\.fffff: 29.87654
      //         s\.ffffff: 29.876543
      //        s\.fffffff: 29.8765432
      // </Snippet20>
   }
}

sample · vbsamples/snippets/visualbasic/VS_Snippets_CLR/conceptual.timespan.custom/vb/fspecifiers1.vb20

vb
' Visual Basic .NET Document
Option Strict On

Module Example
    Public Sub Main()
        ShowSingleSpecifier()
    End Sub

    Private Sub ShowSingleSpecifier()
        ' <Snippet20>
        Dim ts As New TimeSpan(1003498765432)
        Dim fmt As String
        Console.WriteLine(ts.ToString("c"))
        Console.WriteLine()

        For ctr = 1 To 7
            fmt = New String("f"c, ctr)
            If fmt.Length = 1 Then fmt = "%" + fmt
            Console.WriteLine("{0,10}: {1:" + fmt + "}", fmt, ts)
        Next
        Console.WriteLine()

        For ctr = 1 To 7
            fmt = New String("f"c, ctr)
            Console.WriteLine("{0,10}: {1:s\." + fmt + "}", "s\." + fmt, ts)
        Next
        ' The example displays the following output:
        '            %f: 8
        '            ff: 87
        '           fff: 876
        '          ffff: 8765
        '         fffff: 87654
        '        ffffff: 876543
        '       fffffff: 8765432
        '    
        '           s\.f: 29.8
        '          s\.ff: 29.87
        '         s\.fff: 29.876
        '        s\.ffff: 29.8765
        '       s\.fffff: 29.87654
        '      s\.ffffff: 29.876543
        '     s\.fffffff: 29.8765432
        ' </Snippet20>
    End Sub
End Module

Back to table

The "fffff" custom format specifier

The "fffff" custom format specifier (with five f characters) outputs the hundred-thousandths of a second in a time interval. In a formatting operation, any remaining fractional digits are truncated. In a parsing operation that calls the System.TimeSpan.ParseExact or System.TimeSpan.TryParseExact method, the input string must contain exactly five fractional digits.

The following example uses the "fffff" custom format specifier to display the hundred-thousandths of a second in a System.TimeSpan value. "fffff" is used first as the only format specifier, and then combined with the "s" specifier in a custom format string.

sample · csharpsamples/snippets/csharp/VS_Snippets_CLR/conceptual.timespan.custom/cs/fspecifiers1.cs20

csharp
using System;

public class Example
{
   public static void Main()
   {
      // <Snippet20>
      TimeSpan ts = new TimeSpan(1003498765432);
      string fmt;
      Console.WriteLine(ts.ToString("c"));
      Console.WriteLine();

      for (int ctr = 1; ctr <= 7; ctr++) {
         fmt = new string('f', ctr);
         if (fmt.Length == 1) fmt = "%" + fmt;
         Console.WriteLine($"{fmt,10}: {ts.ToString(fmt)}");
      }
      Console.WriteLine();

      for (int ctr = 1; ctr <= 7; ctr++) {
         fmt = $"s\\.{new string('f', ctr)}";
         Console.WriteLine($"{fmt,10}: {ts.ToString(fmt)}");
      }
      // The example displays the following output:
      //               %f: 8
      //               ff: 87
      //              fff: 876
      //             ffff: 8765
      //            fffff: 87654
      //           ffffff: 876543
      //          fffffff: 8765432
      //
      //              s\.f: 29.8
      //             s\.ff: 29.87
      //            s\.fff: 29.876
      //           s\.ffff: 29.8765
      //          s\.fffff: 29.87654
      //         s\.ffffff: 29.876543
      //        s\.fffffff: 29.8765432
      // </Snippet20>
   }
}

sample · vbsamples/snippets/visualbasic/VS_Snippets_CLR/conceptual.timespan.custom/vb/fspecifiers1.vb20

vb
' Visual Basic .NET Document
Option Strict On

Module Example
    Public Sub Main()
        ShowSingleSpecifier()
    End Sub

    Private Sub ShowSingleSpecifier()
        ' <Snippet20>
        Dim ts As New TimeSpan(1003498765432)
        Dim fmt As String
        Console.WriteLine(ts.ToString("c"))
        Console.WriteLine()

        For ctr = 1 To 7
            fmt = New String("f"c, ctr)
            If fmt.Length = 1 Then fmt = "%" + fmt
            Console.WriteLine("{0,10}: {1:" + fmt + "}", fmt, ts)
        Next
        Console.WriteLine()

        For ctr = 1 To 7
            fmt = New String("f"c, ctr)
            Console.WriteLine("{0,10}: {1:s\." + fmt + "}", "s\." + fmt, ts)
        Next
        ' The example displays the following output:
        '            %f: 8
        '            ff: 87
        '           fff: 876
        '          ffff: 8765
        '         fffff: 87654
        '        ffffff: 876543
        '       fffffff: 8765432
        '    
        '           s\.f: 29.8
        '          s\.ff: 29.87
        '         s\.fff: 29.876
        '        s\.ffff: 29.8765
        '       s\.fffff: 29.87654
        '      s\.ffffff: 29.876543
        '     s\.fffffff: 29.8765432
        ' </Snippet20>
    End Sub
End Module

Back to table

The "ffffff" custom format specifier

The "ffffff" custom format specifier (with six f characters) outputs the millionths of a second in a time interval. In a formatting operation, any remaining fractional digits are truncated. In a parsing operation that calls the System.TimeSpan.ParseExact or System.TimeSpan.TryParseExact method, the input string must contain exactly six fractional digits.

The following example uses the "ffffff" custom format specifier to display the millionths of a second in a System.TimeSpan value. It is used first as the only format specifier, and then combined with the "s" specifier in a custom format string.

sample · csharpsamples/snippets/csharp/VS_Snippets_CLR/conceptual.timespan.custom/cs/fspecifiers1.cs20

csharp
using System;

public class Example
{
   public static void Main()
   {
      // <Snippet20>
      TimeSpan ts = new TimeSpan(1003498765432);
      string fmt;
      Console.WriteLine(ts.ToString("c"));
      Console.WriteLine();

      for (int ctr = 1; ctr <= 7; ctr++) {
         fmt = new string('f', ctr);
         if (fmt.Length == 1) fmt = "%" + fmt;
         Console.WriteLine($"{fmt,10}: {ts.ToString(fmt)}");
      }
      Console.WriteLine();

      for (int ctr = 1; ctr <= 7; ctr++) {
         fmt = $"s\\.{new string('f', ctr)}";
         Console.WriteLine($"{fmt,10}: {ts.ToString(fmt)}");
      }
      // The example displays the following output:
      //               %f: 8
      //               ff: 87
      //              fff: 876
      //             ffff: 8765
      //            fffff: 87654
      //           ffffff: 876543
      //          fffffff: 8765432
      //
      //              s\.f: 29.8
      //             s\.ff: 29.87
      //            s\.fff: 29.876
      //           s\.ffff: 29.8765
      //          s\.fffff: 29.87654
      //         s\.ffffff: 29.876543
      //        s\.fffffff: 29.8765432
      // </Snippet20>
   }
}

sample · vbsamples/snippets/visualbasic/VS_Snippets_CLR/conceptual.timespan.custom/vb/fspecifiers1.vb20

vb
' Visual Basic .NET Document
Option Strict On

Module Example
    Public Sub Main()
        ShowSingleSpecifier()
    End Sub

    Private Sub ShowSingleSpecifier()
        ' <Snippet20>
        Dim ts As New TimeSpan(1003498765432)
        Dim fmt As String
        Console.WriteLine(ts.ToString("c"))
        Console.WriteLine()

        For ctr = 1 To 7
            fmt = New String("f"c, ctr)
            If fmt.Length = 1 Then fmt = "%" + fmt
            Console.WriteLine("{0,10}: {1:" + fmt + "}", fmt, ts)
        Next
        Console.WriteLine()

        For ctr = 1 To 7
            fmt = New String("f"c, ctr)
            Console.WriteLine("{0,10}: {1:s\." + fmt + "}", "s\." + fmt, ts)
        Next
        ' The example displays the following output:
        '            %f: 8
        '            ff: 87
        '           fff: 876
        '          ffff: 8765
        '         fffff: 87654
        '        ffffff: 876543
        '       fffffff: 8765432
        '    
        '           s\.f: 29.8
        '          s\.ff: 29.87
        '         s\.fff: 29.876
        '        s\.ffff: 29.8765
        '       s\.fffff: 29.87654
        '      s\.ffffff: 29.876543
        '     s\.fffffff: 29.8765432
        ' </Snippet20>
    End Sub
End Module

Back to table

The "fffffff" custom format specifier

The "fffffff" custom format specifier (with seven f characters) outputs the ten-millionths of a second (or the fractional number of ticks) in a time interval. In a parsing operation that calls the System.TimeSpan.ParseExact or System.TimeSpan.TryParseExact method, the input string must contain exactly seven fractional digits.

The following example uses the "fffffff" custom format specifier to display the fractional number of ticks in a System.TimeSpan value. It is used first as the only format specifier, and then combined with the "s" specifier in a custom format string.

sample · csharpsamples/snippets/csharp/VS_Snippets_CLR/conceptual.timespan.custom/cs/fspecifiers1.cs20

csharp
using System;

public class Example
{
   public static void Main()
   {
      // <Snippet20>
      TimeSpan ts = new TimeSpan(1003498765432);
      string fmt;
      Console.WriteLine(ts.ToString("c"));
      Console.WriteLine();

      for (int ctr = 1; ctr <= 7; ctr++) {
         fmt = new string('f', ctr);
         if (fmt.Length == 1) fmt = "%" + fmt;
         Console.WriteLine($"{fmt,10}: {ts.ToString(fmt)}");
      }
      Console.WriteLine();

      for (int ctr = 1; ctr <= 7; ctr++) {
         fmt = $"s\\.{new string('f', ctr)}";
         Console.WriteLine($"{fmt,10}: {ts.ToString(fmt)}");
      }
      // The example displays the following output:
      //               %f: 8
      //               ff: 87
      //              fff: 876
      //             ffff: 8765
      //            fffff: 87654
      //           ffffff: 876543
      //          fffffff: 8765432
      //
      //              s\.f: 29.8
      //             s\.ff: 29.87
      //            s\.fff: 29.876
      //           s\.ffff: 29.8765
      //          s\.fffff: 29.87654
      //         s\.ffffff: 29.876543
      //        s\.fffffff: 29.8765432
      // </Snippet20>
   }
}

sample · vbsamples/snippets/visualbasic/VS_Snippets_CLR/conceptual.timespan.custom/vb/fspecifiers1.vb20

vb
' Visual Basic .NET Document
Option Strict On

Module Example
    Public Sub Main()
        ShowSingleSpecifier()
    End Sub

    Private Sub ShowSingleSpecifier()
        ' <Snippet20>
        Dim ts As New TimeSpan(1003498765432)
        Dim fmt As String
        Console.WriteLine(ts.ToString("c"))
        Console.WriteLine()

        For ctr = 1 To 7
            fmt = New String("f"c, ctr)
            If fmt.Length = 1 Then fmt = "%" + fmt
            Console.WriteLine("{0,10}: {1:" + fmt + "}", fmt, ts)
        Next
        Console.WriteLine()

        For ctr = 1 To 7
            fmt = New String("f"c, ctr)
            Console.WriteLine("{0,10}: {1:s\." + fmt + "}", "s\." + fmt, ts)
        Next
        ' The example displays the following output:
        '            %f: 8
        '            ff: 87
        '           fff: 876
        '          ffff: 8765
        '         fffff: 87654
        '        ffffff: 876543
        '       fffffff: 8765432
        '    
        '           s\.f: 29.8
        '          s\.ff: 29.87
        '         s\.fff: 29.876
        '        s\.ffff: 29.8765
        '       s\.fffff: 29.87654
        '      s\.ffffff: 29.876543
        '     s\.fffffff: 29.8765432
        ' </Snippet20>
    End Sub
End Module

Back to table

The "F" custom format specifier

The "F" custom format specifier outputs the tenths of a second in a time interval. In a formatting operation, any remaining fractional digits are truncated. If the value of the time interval's tenths of a second is zero, it isn't included in the result string. In a parsing operation that calls the System.TimeSpan.ParseExact or System.TimeSpan.TryParseExact method, the presence of the tenths of a second digit is optional.

If the "F" custom format specifier is used alone, specify "%F" so that it isn't misinterpreted as a standard format string.

The following example uses the "F" custom format specifier to display the tenths of a second in a System.TimeSpan value. It also uses this custom format specifier in a parsing operation.

sample · csharpsamples/snippets/csharp/VS_Snippets_CLR/conceptual.timespan.custom/cs/f_specifiers1.cs21

csharp
using System;

public class Example
{
   public static void Main()
   {
      FSpecifier();
      Console.WriteLine("-----");
      FFSpecifier();
      Console.WriteLine("-----");
      FFFSpecifier();
      Console.WriteLine("-----");
      FFFFSpecifier();
      Console.WriteLine("-----");
      FFFFFSpecifier();
      Console.WriteLine("-----");
      FFFFFFSpecifier();
      Console.WriteLine("-----");
      F7Specifier();
   }

   private static void FSpecifier()
   {
      // <Snippet21>
      Console.WriteLine("Formatting:");
      TimeSpan ts1 = TimeSpan.Parse("0:0:3.669");
      Console.WriteLine($"{ts1} ('%F') --> {ts1:%F}");

      TimeSpan ts2 = TimeSpan.Parse("0:0:3.091");
      Console.WriteLine($"{ts2} ('ss\\.F') --> {ts2:ss\\.F}");
      Console.WriteLine();

      Console.WriteLine("Parsing:");
      string[] inputs = { "0:0:03.", "0:0:03.1", "0:0:03.12" };
      string fmt = @"h\:m\:ss\.F";
      TimeSpan ts3;

      foreach (string input in inputs) {
         if (TimeSpan.TryParseExact(input, fmt, null, out ts3))
            Console.WriteLine($"{input} ('{fmt}') --> {ts3}");
         else
            Console.WriteLine($"Cannot parse {input} with '{fmt}'.");
      }
      // The example displays the following output:
      //       Formatting:
      //       00:00:03.6690000 ('%F') --> 6
      //       00:00:03.0910000 ('ss\.F') --> 03.
      //
      //       Parsing:
      //       0:0:03. ('h\:m\:ss\.F') --> 00:00:03
      //       0:0:03.1 ('h\:m\:ss\.F') --> 00:00:03.1000000
      //       Cannot parse 0:0:03.12 with 'h\:m\:ss\.F'.
      // </Snippet21>
   }

   private static void FFSpecifier()
   {
      // <Snippet22>
      Console.WriteLine("Formatting:");
      TimeSpan ts1 = TimeSpan.Parse("0:0:3.697");
      Console.WriteLine($"{ts1} ('FF') --> {ts1:FF}");

      TimeSpan ts2 = TimeSpan.Parse("0:0:3.809");
      Console.WriteLine($"{ts2} ('ss\\.FF') --> {ts2:ss\\.FF}");
      Console.WriteLine();

      Console.WriteLine("Parsing:");
      string[] inputs = { "0:0:03.", "0:0:03.1", "0:0:03.127" };
      string fmt = @"h\:m\:ss\.FF";
      TimeSpan ts3;

      foreach (string input in inputs) {
         if (TimeSpan.TryParseExact(input, fmt, null, out ts3))
            Console.WriteLine($"{input} ('{fmt}') --> {ts3}");
         else
            Console.WriteLine($"Cannot parse {input} with '{fmt}'.");
      }
      // The example displays the following output:
      //       Formatting:
      //       00:00:03.6970000 ('FF') --> 69
      //       00:00:03.8090000 ('ss\.FF') --> 03.8
      //
      //       Parsing:
      //       0:0:03. ('h\:m\:ss\.FF') --> 00:00:03
      //       0:0:03.1 ('h\:m\:ss\.FF') --> 00:00:03.1000000
      //       Cannot parse 0:0:03.127 with 'h\:m\:ss\.FF'.
      // </Snippet22>
   }

   private static void FFFSpecifier()
   {
      // <Snippet23>
      Console.WriteLine("Formatting:");
      TimeSpan ts1 = TimeSpan.Parse("0:0:3.6974");
      Console.WriteLine($"{ts1} ('FFF') --> {ts1:FFF}");

      TimeSpan ts2 = TimeSpan.Parse("0:0:3.8009");
      Console.WriteLine($"{ts2} ('ss\\.FFF') --> {ts2:ss\\.FFF}");
      Console.WriteLine();

      Console.WriteLine("Parsing:");
      string[] inputs = { "0:0:03.", "0:0:03.12", "0:0:03.1279" };
      string fmt = @"h\:m\:ss\.FFF";
      TimeSpan ts3;

      foreach (string input in inputs) {
         if (TimeSpan.TryParseExact(input, fmt, null, out ts3))
            Console.WriteLine($"{input} ('{fmt}') --> {ts3}");
         else
            Console.WriteLine($"Cannot parse {input} with '{fmt}'.");
      }
      // The example displays the following output:
      //       Formatting:
      //       00:00:03.6974000 ('FFF') --> 697
      //       00:00:03.8009000 ('ss\.FFF') --> 03.8
      //
      //       Parsing:
      //       0:0:03. ('h\:m\:ss\.FFF') --> 00:00:03
      //       0:0:03.12 ('h\:m\:ss\.FFF') --> 00:00:03.1200000
      //       Cannot parse 0:0:03.1279 with 'h\:m\:ss\.FFF'.
      // </Snippet23>
   }

   private static void FFFFSpecifier()
   {
      // <Snippet24>
      Console.WriteLine("Formatting:");
      TimeSpan ts1 = TimeSpan.Parse("0:0:3.69749");
      Console.WriteLine($"{ts1} ('FFFF') --> {ts1:FFFF}");

      TimeSpan ts2 = TimeSpan.Parse("0:0:3.80009");
      Console.WriteLine($"{ts2} ('ss\\.FFFF') --> {ts2:ss\\.FFFF}");
      Console.WriteLine();

      Console.WriteLine("Parsing:");
      string[] inputs = { "0:0:03.", "0:0:03.12", "0:0:03.12795" };
      string fmt = @"h\:m\:ss\.FFFF";
      TimeSpan ts3;

      foreach (string input in inputs) {
         if (TimeSpan.TryParseExact(input, fmt, null, out ts3))
            Console.WriteLine($"{input} ('{fmt}') --> {ts3}");
         else
            Console.WriteLine($"Cannot parse {input} with '{fmt}'.");
      }
      // The example displays the following output:
      //       Formatting:
      //       00:00:03.6974900 ('FFFF') --> 6974
      //       00:00:03.8000900 ('ss\.FFFF') --> 03.8
      //
      //       Parsing:
      //       0:0:03. ('h\:m\:ss\.FFFF') --> 00:00:03
      //       0:0:03.12 ('h\:m\:ss\.FFFF') --> 00:00:03.1200000
      //       Cannot parse 0:0:03.12795 with 'h\:m\:ss\.FFFF'.
      // </Snippet24>
   }

   private static void FFFFFSpecifier()
   {
      // <Snippet25>
      Console.WriteLine("Formatting:");
      TimeSpan ts1 = TimeSpan.Parse("0:0:3.697497");
      Console.WriteLine($"{ts1} ('FFFFF') --> {ts1:FFFFF}");

      TimeSpan ts2 = TimeSpan.Parse("0:0:3.800009");
      Console.WriteLine($"{ts2} ('ss\\.FFFFF') --> {ts2:ss\\.FFFFF}");
      Console.WriteLine();

      Console.WriteLine("Parsing:");
      string[] inputs = { "0:0:03.", "0:0:03.12", "0:0:03.127956" };
      string fmt = @"h\:m\:ss\.FFFFF";
      TimeSpan ts3;

      foreach (string input in inputs) {
         if (TimeSpan.TryParseExact(input, fmt, null, out ts3))
            Console.WriteLine($"{input} ('{fmt}') --> {ts3}");
         else
            Console.WriteLine($"Cannot parse {input} with '{fmt}'.");
      }
      // The example displays the following output:
      //       Formatting:
      //       00:00:03.6974970 ('FFFFF') --> 69749
      //       00:00:03.8000090 ('ss\.FFFFF') --> 03.8
      //
      //       Parsing:
      //       0:0:03. ('h\:m\:ss\.FFFF') --> 00:00:03
      //       0:0:03.12 ('h\:m\:ss\.FFFF') --> 00:00:03.1200000
      //       Cannot parse 0:0:03.127956 with 'h\:m\:ss\.FFFF'.
      // </Snippet25>
   }

   private static void FFFFFFSpecifier()
   {
      // <Snippet26>
      Console.WriteLine("Formatting:");
      TimeSpan ts1 = TimeSpan.Parse("0:0:3.6974974");
      Console.WriteLine($"{ts1} ('FFFFFF') --> {ts1:FFFFFF}");

      TimeSpan ts2 = TimeSpan.Parse("0:0:3.8000009");
      Console.WriteLine($"{ts2} ('ss\\.FFFFFF') --> {ts2:ss\\.FFFFFF}");
      Console.WriteLine();

      Console.WriteLine("Parsing:");
      string[] inputs = { "0:0:03.", "0:0:03.12", "0:0:03.1279569" };
      string fmt = @"h\:m\:ss\.FFFFFF";
      TimeSpan ts3;

      foreach (string input in inputs) {
         if (TimeSpan.TryParseExact(input, fmt, null, out ts3))
            Console.WriteLine($"{input} ('{fmt}') --> {ts3}");
         else
            Console.WriteLine($"Cannot parse {input} with '{fmt}'.");
      }
      // The example displays the following output:
      //       Formatting:
      //       00:00:03.6974974 ('FFFFFF') --> 697497
      //       00:00:03.8000009 ('ss\.FFFFFF') --> 03.8
      //
      //       Parsing:
      //       0:0:03. ('h\:m\:ss\.FFFFFF') --> 00:00:03
      //       0:0:03.12 ('h\:m\:ss\.FFFFFF') --> 00:00:03.1200000
      //       Cannot parse 0:0:03.1279569 with 'h\:m\:ss\.FFFFFF'.
      // </Snippet26>
   }

   private static void F7Specifier()
   {
      // <Snippet27>
      Console.WriteLine("Formatting:");
      TimeSpan ts1 = TimeSpan.Parse("0:0:3.6974974");
      Console.WriteLine($"{ts1} ('FFFFFFF') --> {ts1:FFFFFFF}");

      TimeSpan ts2 = TimeSpan.Parse("0:0:3.9500000");
      Console.WriteLine($"{ts2} ('ss\\.FFFFFFF') --> {ts2:ss\\.FFFFFFF}");
      Console.WriteLine();

      Console.WriteLine("Parsing:");
      string[] inputs = { "0:0:03.", "0:0:03.12", "0:0:03.1279569" };
      string fmt = @"h\:m\:ss\.FFFFFFF";
      TimeSpan ts3;

      foreach (string input in inputs) {
         if (TimeSpan.TryParseExact(input, fmt, null, out ts3))
            Console.WriteLine($"{input} ('{fmt}') --> {ts3}");
         else
            Console.WriteLine($"Cannot parse {input} with '{fmt}'.");
      }
      // The example displays the following output:
      //    Formatting:
      //    00:00:03.6974974 ('FFFFFFF') --> 6974974
      //    00:00:03.9500000 ('ss\.FFFFFFF') --> 03.95
      //
      //    Parsing:
      //    0:0:03. ('h\:m\:ss\.FFFFFFF') --> 00:00:03
      //    0:0:03.12 ('h\:m\:ss\.FFFFFFF') --> 00:00:03.1200000
      //    0:0:03.1279569 ('h\:m\:ss\.FFFFFFF') --> 00:00:03.1279569
      // </Snippet27>
   }
}

sample · vbsamples/snippets/visualbasic/VS_Snippets_CLR/conceptual.timespan.custom/vb/f_specifiers1.vb21

vb
' Visual Basic .NET Document
Option Strict On

Module Example
    Public Sub Main()
        FSpecifier()
        Console.WriteLine("-----")
        FFSpecifier()
        Console.WriteLine("-----")
        FFFSpecifier()
        Console.WriteLine("-----")
        FFFFSpecifier()
        Console.WriteLine("-----")
        FFFFFSpecifier()
        Console.WriteLine("-----")
        FFFFFFSpecifier()
        Console.WriteLine("-----")
        F7Specifier()
    End Sub

    Private Sub FSpecifier()
        ' <Snippet21>
        Console.WriteLine("Formatting:")
        Dim ts1 As TimeSpan = TimeSpan.Parse("0:0:3.669")
        Console.WriteLine("{0} ('%F') --> {0:%F}", ts1)

        Dim ts2 As TimeSpan = TimeSpan.Parse("0:0:3.091")
        Console.WriteLine("{0} ('ss\.F') --> {0:ss\.F}", ts2)
        Console.WriteLine()

        Console.WriteLine("Parsing:")
        Dim inputs() As String = {"0:0:03.", "0:0:03.1", "0:0:03.12"}
        Dim fmt As String = "h\:m\:ss\.F"
        Dim ts3 As TimeSpan

        For Each input As String In inputs
            If TimeSpan.TryParseExact(input, fmt, Nothing, ts3)
                Console.WriteLine("{0} ('{1}') --> {2}", input, fmt, ts3)
            Else
                Console.WriteLine("Cannot parse {0} with '{1}'.",
                                  input, fmt)
            End If
        Next
        ' The example displays the following output:
        '       Formatting:
        '       00:00:03.6690000 ('%F') --> 6
        '       00:00:03.0910000 ('ss\.F') --> 03.
        '       
        '       Parsing:
        '       0:0:03. ('h\:m\:ss\.F') --> 00:00:03
        '       0:0:03.1 ('h\:m\:ss\.F') --> 00:00:03.1000000
        '       Cannot parse 0:0:03.12 with 'h\:m\:ss\.F'.      
        ' </Snippet21>
    End Sub

    Private Sub FFSpecifier()
        ' <Snippet22>
        Console.WriteLine("Formatting:")
        Dim ts1 As TimeSpan = TimeSpan.Parse("0:0:3.697")
        Console.WriteLine("{0} ('FF') --> {0:FF}", ts1)

        Dim ts2 As TimeSpan = TimeSpan.Parse("0:0:3.809")
        Console.WriteLine("{0} ('ss\.FF') --> {0:ss\.FF}", ts2)
        Console.WriteLine()

        Console.WriteLine("Parsing:")
        Dim inputs() As String = {"0:0:03.", "0:0:03.1", "0:0:03.127"}
        Dim fmt As String = "h\:m\:ss\.FF"
        Dim ts3 As TimeSpan

        For Each input As String In inputs
            If TimeSpan.TryParseExact(input, fmt, Nothing, ts3)
                Console.WriteLine("{0} ('{1}') --> {2}", input, fmt, ts3)
            Else
                Console.WriteLine("Cannot parse {0} with '{1}'.",
                                  input, fmt)
            End If
        Next
        ' The example displays the following output:
        '       Formatting:
        '       00:00:03.6970000 ('FF') --> 69
        '       00:00:03.8090000 ('ss\.FF') --> 03.8
        '       
        '       Parsing:
        '       0:0:03. ('h\:m\:ss\.FF') --> 00:00:03
        '       0:0:03.1 ('h\:m\:ss\.FF') --> 00:00:03.1000000
        '       Cannot parse 0:0:03.127 with 'h\:m\:ss\.FF'.
        ' </Snippet22>
    End Sub

    Private Sub FFFSpecifier()
        ' <Snippet23>
        Console.WriteLine("Formatting:")
        Dim ts1 As TimeSpan = TimeSpan.Parse("0:0:3.6974")
        Console.WriteLine("{0} ('FFF') --> {0:FFF}", ts1)

        Dim ts2 As TimeSpan = TimeSpan.Parse("0:0:3.8009")
        Console.WriteLine("{0} ('ss\.FFF') --> {0:ss\.FFF}", ts2)
        Console.WriteLine()

        Console.WriteLine("Parsing:")
        Dim inputs() As String = {"0:0:03.", "0:0:03.12", "0:0:03.1279"}
        Dim fmt As String = "h\:m\:ss\.FFF"
        Dim ts3 As TimeSpan

        For Each input As String In inputs
            If TimeSpan.TryParseExact(input, fmt, Nothing, ts3)
                Console.WriteLine("{0} ('{1}') --> {2}", input, fmt, ts3)
            Else
                Console.WriteLine("Cannot parse {0} with '{1}'.",
                                  input, fmt)
            End If
        Next
        ' The example displays the following output:
        '       Formatting:
        '       00:00:03.6974000 ('FFF') --> 697
        '       00:00:03.8009000 ('ss\.FFF') --> 03.8
        '       
        '       Parsing:
        '       0:0:03. ('h\:m\:ss\.FFF') --> 00:00:03
        '       0:0:03.12 ('h\:m\:ss\.FFF') --> 00:00:03.1200000
        '       Cannot parse 0:0:03.1279 with 'h\:m\:ss\.FFF'.
        ' </Snippet23>
    End Sub

    Private Sub FFFFSpecifier()
        ' <Snippet24>
        Console.WriteLine("Formatting:")
        Dim ts1 As TimeSpan = TimeSpan.Parse("0:0:3.69749")
        Console.WriteLine("{0} ('FFFF') --> {0:FFFF}", ts1)

        Dim ts2 As TimeSpan = TimeSpan.Parse("0:0:3.80009")
        Console.WriteLine("{0} ('ss\.FFFF') --> {0:ss\.FFFF}", ts2)
        Console.WriteLine()

        Console.WriteLine("Parsing:")
        Dim inputs() As String = {"0:0:03.", "0:0:03.12", "0:0:03.12795"}
        Dim fmt As String = "h\:m\:ss\.FFFF"
        Dim ts3 As TimeSpan

        For Each input As String In inputs
            If TimeSpan.TryParseExact(input, fmt, Nothing, ts3)
                Console.WriteLine("{0} ('{1}') --> {2}", input, fmt, ts3)
            Else
                Console.WriteLine("Cannot parse {0} with '{1}'.",
                                  input, fmt)
            End If
        Next
        ' The example displays the following output:
        '       Formatting:
        '       00:00:03.6974900 ('FFFF') --> 6974
        '       00:00:03.8000900 ('ss\.FFFF') --> 03.8
        '       
        '       Parsing:
        '       0:0:03. ('h\:m\:ss\.FFFF') --> 00:00:03
        '       0:0:03.12 ('h\:m\:ss\.FFFF') --> 00:00:03.1200000
        '       Cannot parse 0:0:03.12795 with 'h\:m\:ss\.FFFF'.
        ' </Snippet24>
    End Sub

    Private Sub FFFFFSpecifier()
        ' <Snippet25>
        Console.WriteLine("Formatting:")
        Dim ts1 As TimeSpan = TimeSpan.Parse("0:0:3.697497")
        Console.WriteLine("{0} ('FFFFF') --> {0:FFFFF}", ts1)

        Dim ts2 As TimeSpan = TimeSpan.Parse("0:0:3.800009")
        Console.WriteLine("{0} ('ss\.FFFFF') --> {0:ss\.FFFFF}", ts2)
        Console.WriteLine()

        Console.WriteLine("Parsing:")
        Dim inputs() As String = {"0:0:03.", "0:0:03.12", "0:0:03.127956"}
        Dim fmt As String = "h\:m\:ss\.FFFFF"
        Dim ts3 As TimeSpan

        For Each input As String In inputs
            If TimeSpan.TryParseExact(input, fmt, Nothing, ts3)
                Console.WriteLine("{0} ('{1}') --> {2}", input, fmt, ts3)
            Else
                Console.WriteLine("Cannot parse {0} with '{1}'.",
                                  input, fmt)
            End If
        Next
        ' The example displays the following output:
        '       Formatting:
        '       00:00:03.6974970 ('FFFFF') --> 69749
        '       00:00:03.8000090 ('ss\.FFFFF') --> 03.8
        '       
        '       Parsing:
        '       0:0:03. ('h\:m\:ss\.FFFF') --> 00:00:03
        '       0:0:03.12 ('h\:m\:ss\.FFFF') --> 00:00:03.1200000
        '       Cannot parse 0:0:03.127956 with 'h\:m\:ss\.FFFF'.
        ' </Snippet25>
    End Sub

    Private Sub FFFFFFSpecifier()
        ' <Snippet26>
        Console.WriteLine("Formatting:")
        Dim ts1 As TimeSpan = TimeSpan.Parse("0:0:3.6974974")
        Console.WriteLine("{0} ('FFFFFF') --> {0:FFFFFF}", ts1)

        Dim ts2 As TimeSpan = TimeSpan.Parse("0:0:3.8000009")
        Console.WriteLine("{0} ('ss\.FFFFFF') --> {0:ss\.FFFFFF}", ts2)
        Console.WriteLine()

        Console.WriteLine("Parsing:")
        Dim inputs() As String = {"0:0:03.", "0:0:03.12", "0:0:03.1279569"}
        Dim fmt As String = "h\:m\:ss\.FFFFFF"
        Dim ts3 As TimeSpan

        For Each input As String In inputs
            If TimeSpan.TryParseExact(input, fmt, Nothing, ts3)
                Console.WriteLine("{0} ('{1}') --> {2}", input, fmt, ts3)
            Else
                Console.WriteLine("Cannot parse {0} with '{1}'.",
                                  input, fmt)
            End If
        Next
        ' The example displays the following output:
        '       Formatting:
        '       00:00:03.6974974 ('FFFFFF') --> 697497
        '       00:00:03.8000009 ('ss\.FFFFFF') --> 03.8
        '       
        '       Parsing:
        '       0:0:03. ('h\:m\:ss\.FFFFFF') --> 00:00:03
        '       0:0:03.12 ('h\:m\:ss\.FFFFFF') --> 00:00:03.1200000
        '       Cannot parse 0:0:03.1279569 with 'h\:m\:ss\.FFFFFF'.
        ' </Snippet26>
    End Sub

    Private Sub F7Specifier()
        ' <Snippet27>
        Console.WriteLine("Formatting:")
        Dim ts1 As TimeSpan = TimeSpan.Parse("0:0:3.6974974")
        Console.WriteLine("{0} ('FFFFFFF') --> {0:FFFFFFF}", ts1)

        Dim ts2 As TimeSpan = TimeSpan.Parse("0:0:3.9500000")
        Console.WriteLine("{0} ('ss\.FFFFFFF') --> {0:ss\.FFFFFFF}", ts2)
        Console.WriteLine()

        Console.WriteLine("Parsing:")
        Dim inputs() As String = {"0:0:03.", "0:0:03.12", "0:0:03.1279569"}
        Dim fmt As String = "h\:m\:ss\.FFFFFFF"
        Dim ts3 As TimeSpan

        For Each input As String In inputs
            If TimeSpan.TryParseExact(input, fmt, Nothing, ts3)
                Console.WriteLine("{0} ('{1}') --> {2}", input, fmt, ts3)
            Else
                Console.WriteLine("Cannot parse {0} with '{1}'.",
                                  input, fmt)
            End If
        Next
        ' The example displays the following output:
        '    Formatting:
        '    00:00:03.6974974 ('FFFFFFF') --> 6974974
        '    00:00:03.9500000 ('ss\.FFFFFFF') --> 03.95
        '    
        '    Parsing:
        '    0:0:03. ('h\:m\:ss\.FFFFFFF') --> 00:00:03
        '    0:0:03.12 ('h\:m\:ss\.FFFFFFF') --> 00:00:03.1200000
        '    0:0:03.1279569 ('h\:m\:ss\.FFFFFFF') --> 00:00:03.1279569     
        ' </Snippet27>
    End Sub
End Module

Back to table

The "FF" custom format specifier

The "FF" custom format specifier outputs the hundredths of a second in a time interval. In a formatting operation, any remaining fractional digits are truncated. If there are any trailing fractional zeros, they aren't included in the result string. In a parsing operation that calls the System.TimeSpan.ParseExact or System.TimeSpan.TryParseExact method, the presence of the tenths and hundredths of a second digit is optional.

The following example uses the "FF" custom format specifier to display the hundredths of a second in a System.TimeSpan value. It also uses this custom format specifier in a parsing operation.

sample · csharpsamples/snippets/csharp/VS_Snippets_CLR/conceptual.timespan.custom/cs/f_specifiers1.cs22

csharp
using System;

public class Example
{
   public static void Main()
   {
      FSpecifier();
      Console.WriteLine("-----");
      FFSpecifier();
      Console.WriteLine("-----");
      FFFSpecifier();
      Console.WriteLine("-----");
      FFFFSpecifier();
      Console.WriteLine("-----");
      FFFFFSpecifier();
      Console.WriteLine("-----");
      FFFFFFSpecifier();
      Console.WriteLine("-----");
      F7Specifier();
   }

   private static void FSpecifier()
   {
      // <Snippet21>
      Console.WriteLine("Formatting:");
      TimeSpan ts1 = TimeSpan.Parse("0:0:3.669");
      Console.WriteLine($"{ts1} ('%F') --> {ts1:%F}");

      TimeSpan ts2 = TimeSpan.Parse("0:0:3.091");
      Console.WriteLine($"{ts2} ('ss\\.F') --> {ts2:ss\\.F}");
      Console.WriteLine();

      Console.WriteLine("Parsing:");
      string[] inputs = { "0:0:03.", "0:0:03.1", "0:0:03.12" };
      string fmt = @"h\:m\:ss\.F";
      TimeSpan ts3;

      foreach (string input in inputs) {
         if (TimeSpan.TryParseExact(input, fmt, null, out ts3))
            Console.WriteLine($"{input} ('{fmt}') --> {ts3}");
         else
            Console.WriteLine($"Cannot parse {input} with '{fmt}'.");
      }
      // The example displays the following output:
      //       Formatting:
      //       00:00:03.6690000 ('%F') --> 6
      //       00:00:03.0910000 ('ss\.F') --> 03.
      //
      //       Parsing:
      //       0:0:03. ('h\:m\:ss\.F') --> 00:00:03
      //       0:0:03.1 ('h\:m\:ss\.F') --> 00:00:03.1000000
      //       Cannot parse 0:0:03.12 with 'h\:m\:ss\.F'.
      // </Snippet21>
   }

   private static void FFSpecifier()
   {
      // <Snippet22>
      Console.WriteLine("Formatting:");
      TimeSpan ts1 = TimeSpan.Parse("0:0:3.697");
      Console.WriteLine($"{ts1} ('FF') --> {ts1:FF}");

      TimeSpan ts2 = TimeSpan.Parse("0:0:3.809");
      Console.WriteLine($"{ts2} ('ss\\.FF') --> {ts2:ss\\.FF}");
      Console.WriteLine();

      Console.WriteLine("Parsing:");
      string[] inputs = { "0:0:03.", "0:0:03.1", "0:0:03.127" };
      string fmt = @"h\:m\:ss\.FF";
      TimeSpan ts3;

      foreach (string input in inputs) {
         if (TimeSpan.TryParseExact(input, fmt, null, out ts3))
            Console.WriteLine($"{input} ('{fmt}') --> {ts3}");
         else
            Console.WriteLine($"Cannot parse {input} with '{fmt}'.");
      }
      // The example displays the following output:
      //       Formatting:
      //       00:00:03.6970000 ('FF') --> 69
      //       00:00:03.8090000 ('ss\.FF') --> 03.8
      //
      //       Parsing:
      //       0:0:03. ('h\:m\:ss\.FF') --> 00:00:03
      //       0:0:03.1 ('h\:m\:ss\.FF') --> 00:00:03.1000000
      //       Cannot parse 0:0:03.127 with 'h\:m\:ss\.FF'.
      // </Snippet22>
   }

   private static void FFFSpecifier()
   {
      // <Snippet23>
      Console.WriteLine("Formatting:");
      TimeSpan ts1 = TimeSpan.Parse("0:0:3.6974");
      Console.WriteLine($"{ts1} ('FFF') --> {ts1:FFF}");

      TimeSpan ts2 = TimeSpan.Parse("0:0:3.8009");
      Console.WriteLine($"{ts2} ('ss\\.FFF') --> {ts2:ss\\.FFF}");
      Console.WriteLine();

      Console.WriteLine("Parsing:");
      string[] inputs = { "0:0:03.", "0:0:03.12", "0:0:03.1279" };
      string fmt = @"h\:m\:ss\.FFF";
      TimeSpan ts3;

      foreach (string input in inputs) {
         if (TimeSpan.TryParseExact(input, fmt, null, out ts3))
            Console.WriteLine($"{input} ('{fmt}') --> {ts3}");
         else
            Console.WriteLine($"Cannot parse {input} with '{fmt}'.");
      }
      // The example displays the following output:
      //       Formatting:
      //       00:00:03.6974000 ('FFF') --> 697
      //       00:00:03.8009000 ('ss\.FFF') --> 03.8
      //
      //       Parsing:
      //       0:0:03. ('h\:m\:ss\.FFF') --> 00:00:03
      //       0:0:03.12 ('h\:m\:ss\.FFF') --> 00:00:03.1200000
      //       Cannot parse 0:0:03.1279 with 'h\:m\:ss\.FFF'.
      // </Snippet23>
   }

   private static void FFFFSpecifier()
   {
      // <Snippet24>
      Console.WriteLine("Formatting:");
      TimeSpan ts1 = TimeSpan.Parse("0:0:3.69749");
      Console.WriteLine($"{ts1} ('FFFF') --> {ts1:FFFF}");

      TimeSpan ts2 = TimeSpan.Parse("0:0:3.80009");
      Console.WriteLine($"{ts2} ('ss\\.FFFF') --> {ts2:ss\\.FFFF}");
      Console.WriteLine();

      Console.WriteLine("Parsing:");
      string[] inputs = { "0:0:03.", "0:0:03.12", "0:0:03.12795" };
      string fmt = @"h\:m\:ss\.FFFF";
      TimeSpan ts3;

      foreach (string input in inputs) {
         if (TimeSpan.TryParseExact(input, fmt, null, out ts3))
            Console.WriteLine($"{input} ('{fmt}') --> {ts3}");
         else
            Console.WriteLine($"Cannot parse {input} with '{fmt}'.");
      }
      // The example displays the following output:
      //       Formatting:
      //       00:00:03.6974900 ('FFFF') --> 6974
      //       00:00:03.8000900 ('ss\.FFFF') --> 03.8
      //
      //       Parsing:
      //       0:0:03. ('h\:m\:ss\.FFFF') --> 00:00:03
      //       0:0:03.12 ('h\:m\:ss\.FFFF') --> 00:00:03.1200000
      //       Cannot parse 0:0:03.12795 with 'h\:m\:ss\.FFFF'.
      // </Snippet24>
   }

   private static void FFFFFSpecifier()
   {
      // <Snippet25>
      Console.WriteLine("Formatting:");
      TimeSpan ts1 = TimeSpan.Parse("0:0:3.697497");
      Console.WriteLine($"{ts1} ('FFFFF') --> {ts1:FFFFF}");

      TimeSpan ts2 = TimeSpan.Parse("0:0:3.800009");
      Console.WriteLine($"{ts2} ('ss\\.FFFFF') --> {ts2:ss\\.FFFFF}");
      Console.WriteLine();

      Console.WriteLine("Parsing:");
      string[] inputs = { "0:0:03.", "0:0:03.12", "0:0:03.127956" };
      string fmt = @"h\:m\:ss\.FFFFF";
      TimeSpan ts3;

      foreach (string input in inputs) {
         if (TimeSpan.TryParseExact(input, fmt, null, out ts3))
            Console.WriteLine($"{input} ('{fmt}') --> {ts3}");
         else
            Console.WriteLine($"Cannot parse {input} with '{fmt}'.");
      }
      // The example displays the following output:
      //       Formatting:
      //       00:00:03.6974970 ('FFFFF') --> 69749
      //       00:00:03.8000090 ('ss\.FFFFF') --> 03.8
      //
      //       Parsing:
      //       0:0:03. ('h\:m\:ss\.FFFF') --> 00:00:03
      //       0:0:03.12 ('h\:m\:ss\.FFFF') --> 00:00:03.1200000
      //       Cannot parse 0:0:03.127956 with 'h\:m\:ss\.FFFF'.
      // </Snippet25>
   }

   private static void FFFFFFSpecifier()
   {
      // <Snippet26>
      Console.WriteLine("Formatting:");
      TimeSpan ts1 = TimeSpan.Parse("0:0:3.6974974");
      Console.WriteLine($"{ts1} ('FFFFFF') --> {ts1:FFFFFF}");

      TimeSpan ts2 = TimeSpan.Parse("0:0:3.8000009");
      Console.WriteLine($"{ts2} ('ss\\.FFFFFF') --> {ts2:ss\\.FFFFFF}");
      Console.WriteLine();

      Console.WriteLine("Parsing:");
      string[] inputs = { "0:0:03.", "0:0:03.12", "0:0:03.1279569" };
      string fmt = @"h\:m\:ss\.FFFFFF";
      TimeSpan ts3;

      foreach (string input in inputs) {
         if (TimeSpan.TryParseExact(input, fmt, null, out ts3))
            Console.WriteLine($"{input} ('{fmt}') --> {ts3}");
         else
            Console.WriteLine($"Cannot parse {input} with '{fmt}'.");
      }
      // The example displays the following output:
      //       Formatting:
      //       00:00:03.6974974 ('FFFFFF') --> 697497
      //       00:00:03.8000009 ('ss\.FFFFFF') --> 03.8
      //
      //       Parsing:
      //       0:0:03. ('h\:m\:ss\.FFFFFF') --> 00:00:03
      //       0:0:03.12 ('h\:m\:ss\.FFFFFF') --> 00:00:03.1200000
      //       Cannot parse 0:0:03.1279569 with 'h\:m\:ss\.FFFFFF'.
      // </Snippet26>
   }

   private static void F7Specifier()
   {
      // <Snippet27>
      Console.WriteLine("Formatting:");
      TimeSpan ts1 = TimeSpan.Parse("0:0:3.6974974");
      Console.WriteLine($"{ts1} ('FFFFFFF') --> {ts1:FFFFFFF}");

      TimeSpan ts2 = TimeSpan.Parse("0:0:3.9500000");
      Console.WriteLine($"{ts2} ('ss\\.FFFFFFF') --> {ts2:ss\\.FFFFFFF}");
      Console.WriteLine();

      Console.WriteLine("Parsing:");
      string[] inputs = { "0:0:03.", "0:0:03.12", "0:0:03.1279569" };
      string fmt = @"h\:m\:ss\.FFFFFFF";
      TimeSpan ts3;

      foreach (string input in inputs) {
         if (TimeSpan.TryParseExact(input, fmt, null, out ts3))
            Console.WriteLine($"{input} ('{fmt}') --> {ts3}");
         else
            Console.WriteLine($"Cannot parse {input} with '{fmt}'.");
      }
      // The example displays the following output:
      //    Formatting:
      //    00:00:03.6974974 ('FFFFFFF') --> 6974974
      //    00:00:03.9500000 ('ss\.FFFFFFF') --> 03.95
      //
      //    Parsing:
      //    0:0:03. ('h\:m\:ss\.FFFFFFF') --> 00:00:03
      //    0:0:03.12 ('h\:m\:ss\.FFFFFFF') --> 00:00:03.1200000
      //    0:0:03.1279569 ('h\:m\:ss\.FFFFFFF') --> 00:00:03.1279569
      // </Snippet27>
   }
}

sample · vbsamples/snippets/visualbasic/VS_Snippets_CLR/conceptual.timespan.custom/vb/f_specifiers1.vb22

vb
' Visual Basic .NET Document
Option Strict On

Module Example
    Public Sub Main()
        FSpecifier()
        Console.WriteLine("-----")
        FFSpecifier()
        Console.WriteLine("-----")
        FFFSpecifier()
        Console.WriteLine("-----")
        FFFFSpecifier()
        Console.WriteLine("-----")
        FFFFFSpecifier()
        Console.WriteLine("-----")
        FFFFFFSpecifier()
        Console.WriteLine("-----")
        F7Specifier()
    End Sub

    Private Sub FSpecifier()
        ' <Snippet21>
        Console.WriteLine("Formatting:")
        Dim ts1 As TimeSpan = TimeSpan.Parse("0:0:3.669")
        Console.WriteLine("{0} ('%F') --> {0:%F}", ts1)

        Dim ts2 As TimeSpan = TimeSpan.Parse("0:0:3.091")
        Console.WriteLine("{0} ('ss\.F') --> {0:ss\.F}", ts2)
        Console.WriteLine()

        Console.WriteLine("Parsing:")
        Dim inputs() As String = {"0:0:03.", "0:0:03.1", "0:0:03.12"}
        Dim fmt As String = "h\:m\:ss\.F"
        Dim ts3 As TimeSpan

        For Each input As String In inputs
            If TimeSpan.TryParseExact(input, fmt, Nothing, ts3)
                Console.WriteLine("{0} ('{1}') --> {2}", input, fmt, ts3)
            Else
                Console.WriteLine("Cannot parse {0} with '{1}'.",
                                  input, fmt)
            End If
        Next
        ' The example displays the following output:
        '       Formatting:
        '       00:00:03.6690000 ('%F') --> 6
        '       00:00:03.0910000 ('ss\.F') --> 03.
        '       
        '       Parsing:
        '       0:0:03. ('h\:m\:ss\.F') --> 00:00:03
        '       0:0:03.1 ('h\:m\:ss\.F') --> 00:00:03.1000000
        '       Cannot parse 0:0:03.12 with 'h\:m\:ss\.F'.      
        ' </Snippet21>
    End Sub

    Private Sub FFSpecifier()
        ' <Snippet22>
        Console.WriteLine("Formatting:")
        Dim ts1 As TimeSpan = TimeSpan.Parse("0:0:3.697")
        Console.WriteLine("{0} ('FF') --> {0:FF}", ts1)

        Dim ts2 As TimeSpan = TimeSpan.Parse("0:0:3.809")
        Console.WriteLine("{0} ('ss\.FF') --> {0:ss\.FF}", ts2)
        Console.WriteLine()

        Console.WriteLine("Parsing:")
        Dim inputs() As String = {"0:0:03.", "0:0:03.1", "0:0:03.127"}
        Dim fmt As String = "h\:m\:ss\.FF"
        Dim ts3 As TimeSpan

        For Each input As String In inputs
            If TimeSpan.TryParseExact(input, fmt, Nothing, ts3)
                Console.WriteLine("{0} ('{1}') --> {2}", input, fmt, ts3)
            Else
                Console.WriteLine("Cannot parse {0} with '{1}'.",
                                  input, fmt)
            End If
        Next
        ' The example displays the following output:
        '       Formatting:
        '       00:00:03.6970000 ('FF') --> 69
        '       00:00:03.8090000 ('ss\.FF') --> 03.8
        '       
        '       Parsing:
        '       0:0:03. ('h\:m\:ss\.FF') --> 00:00:03
        '       0:0:03.1 ('h\:m\:ss\.FF') --> 00:00:03.1000000
        '       Cannot parse 0:0:03.127 with 'h\:m\:ss\.FF'.
        ' </Snippet22>
    End Sub

    Private Sub FFFSpecifier()
        ' <Snippet23>
        Console.WriteLine("Formatting:")
        Dim ts1 As TimeSpan = TimeSpan.Parse("0:0:3.6974")
        Console.WriteLine("{0} ('FFF') --> {0:FFF}", ts1)

        Dim ts2 As TimeSpan = TimeSpan.Parse("0:0:3.8009")
        Console.WriteLine("{0} ('ss\.FFF') --> {0:ss\.FFF}", ts2)
        Console.WriteLine()

        Console.WriteLine("Parsing:")
        Dim inputs() As String = {"0:0:03.", "0:0:03.12", "0:0:03.1279"}
        Dim fmt As String = "h\:m\:ss\.FFF"
        Dim ts3 As TimeSpan

        For Each input As String In inputs
            If TimeSpan.TryParseExact(input, fmt, Nothing, ts3)
                Console.WriteLine("{0} ('{1}') --> {2}", input, fmt, ts3)
            Else
                Console.WriteLine("Cannot parse {0} with '{1}'.",
                                  input, fmt)
            End If
        Next
        ' The example displays the following output:
        '       Formatting:
        '       00:00:03.6974000 ('FFF') --> 697
        '       00:00:03.8009000 ('ss\.FFF') --> 03.8
        '       
        '       Parsing:
        '       0:0:03. ('h\:m\:ss\.FFF') --> 00:00:03
        '       0:0:03.12 ('h\:m\:ss\.FFF') --> 00:00:03.1200000
        '       Cannot parse 0:0:03.1279 with 'h\:m\:ss\.FFF'.
        ' </Snippet23>
    End Sub

    Private Sub FFFFSpecifier()
        ' <Snippet24>
        Console.WriteLine("Formatting:")
        Dim ts1 As TimeSpan = TimeSpan.Parse("0:0:3.69749")
        Console.WriteLine("{0} ('FFFF') --> {0:FFFF}", ts1)

        Dim ts2 As TimeSpan = TimeSpan.Parse("0:0:3.80009")
        Console.WriteLine("{0} ('ss\.FFFF') --> {0:ss\.FFFF}", ts2)
        Console.WriteLine()

        Console.WriteLine("Parsing:")
        Dim inputs() As String = {"0:0:03.", "0:0:03.12", "0:0:03.12795"}
        Dim fmt As String = "h\:m\:ss\.FFFF"
        Dim ts3 As TimeSpan

        For Each input As String In inputs
            If TimeSpan.TryParseExact(input, fmt, Nothing, ts3)
                Console.WriteLine("{0} ('{1}') --> {2}", input, fmt, ts3)
            Else
                Console.WriteLine("Cannot parse {0} with '{1}'.",
                                  input, fmt)
            End If
        Next
        ' The example displays the following output:
        '       Formatting:
        '       00:00:03.6974900 ('FFFF') --> 6974
        '       00:00:03.8000900 ('ss\.FFFF') --> 03.8
        '       
        '       Parsing:
        '       0:0:03. ('h\:m\:ss\.FFFF') --> 00:00:03
        '       0:0:03.12 ('h\:m\:ss\.FFFF') --> 00:00:03.1200000
        '       Cannot parse 0:0:03.12795 with 'h\:m\:ss\.FFFF'.
        ' </Snippet24>
    End Sub

    Private Sub FFFFFSpecifier()
        ' <Snippet25>
        Console.WriteLine("Formatting:")
        Dim ts1 As TimeSpan = TimeSpan.Parse("0:0:3.697497")
        Console.WriteLine("{0} ('FFFFF') --> {0:FFFFF}", ts1)

        Dim ts2 As TimeSpan = TimeSpan.Parse("0:0:3.800009")
        Console.WriteLine("{0} ('ss\.FFFFF') --> {0:ss\.FFFFF}", ts2)
        Console.WriteLine()

        Console.WriteLine("Parsing:")
        Dim inputs() As String = {"0:0:03.", "0:0:03.12", "0:0:03.127956"}
        Dim fmt As String = "h\:m\:ss\.FFFFF"
        Dim ts3 As TimeSpan

        For Each input As String In inputs
            If TimeSpan.TryParseExact(input, fmt, Nothing, ts3)
                Console.WriteLine("{0} ('{1}') --> {2}", input, fmt, ts3)
            Else
                Console.WriteLine("Cannot parse {0} with '{1}'.",
                                  input, fmt)
            End If
        Next
        ' The example displays the following output:
        '       Formatting:
        '       00:00:03.6974970 ('FFFFF') --> 69749
        '       00:00:03.8000090 ('ss\.FFFFF') --> 03.8
        '       
        '       Parsing:
        '       0:0:03. ('h\:m\:ss\.FFFF') --> 00:00:03
        '       0:0:03.12 ('h\:m\:ss\.FFFF') --> 00:00:03.1200000
        '       Cannot parse 0:0:03.127956 with 'h\:m\:ss\.FFFF'.
        ' </Snippet25>
    End Sub

    Private Sub FFFFFFSpecifier()
        ' <Snippet26>
        Console.WriteLine("Formatting:")
        Dim ts1 As TimeSpan = TimeSpan.Parse("0:0:3.6974974")
        Console.WriteLine("{0} ('FFFFFF') --> {0:FFFFFF}", ts1)

        Dim ts2 As TimeSpan = TimeSpan.Parse("0:0:3.8000009")
        Console.WriteLine("{0} ('ss\.FFFFFF') --> {0:ss\.FFFFFF}", ts2)
        Console.WriteLine()

        Console.WriteLine("Parsing:")
        Dim inputs() As String = {"0:0:03.", "0:0:03.12", "0:0:03.1279569"}
        Dim fmt As String = "h\:m\:ss\.FFFFFF"
        Dim ts3 As TimeSpan

        For Each input As String In inputs
            If TimeSpan.TryParseExact(input, fmt, Nothing, ts3)
                Console.WriteLine("{0} ('{1}') --> {2}", input, fmt, ts3)
            Else
                Console.WriteLine("Cannot parse {0} with '{1}'.",
                                  input, fmt)
            End If
        Next
        ' The example displays the following output:
        '       Formatting:
        '       00:00:03.6974974 ('FFFFFF') --> 697497
        '       00:00:03.8000009 ('ss\.FFFFFF') --> 03.8
        '       
        '       Parsing:
        '       0:0:03. ('h\:m\:ss\.FFFFFF') --> 00:00:03
        '       0:0:03.12 ('h\:m\:ss\.FFFFFF') --> 00:00:03.1200000
        '       Cannot parse 0:0:03.1279569 with 'h\:m\:ss\.FFFFFF'.
        ' </Snippet26>
    End Sub

    Private Sub F7Specifier()
        ' <Snippet27>
        Console.WriteLine("Formatting:")
        Dim ts1 As TimeSpan = TimeSpan.Parse("0:0:3.6974974")
        Console.WriteLine("{0} ('FFFFFFF') --> {0:FFFFFFF}", ts1)

        Dim ts2 As TimeSpan = TimeSpan.Parse("0:0:3.9500000")
        Console.WriteLine("{0} ('ss\.FFFFFFF') --> {0:ss\.FFFFFFF}", ts2)
        Console.WriteLine()

        Console.WriteLine("Parsing:")
        Dim inputs() As String = {"0:0:03.", "0:0:03.12", "0:0:03.1279569"}
        Dim fmt As String = "h\:m\:ss\.FFFFFFF"
        Dim ts3 As TimeSpan

        For Each input As String In inputs
            If TimeSpan.TryParseExact(input, fmt, Nothing, ts3)
                Console.WriteLine("{0} ('{1}') --> {2}", input, fmt, ts3)
            Else
                Console.WriteLine("Cannot parse {0} with '{1}'.",
                                  input, fmt)
            End If
        Next
        ' The example displays the following output:
        '    Formatting:
        '    00:00:03.6974974 ('FFFFFFF') --> 6974974
        '    00:00:03.9500000 ('ss\.FFFFFFF') --> 03.95
        '    
        '    Parsing:
        '    0:0:03. ('h\:m\:ss\.FFFFFFF') --> 00:00:03
        '    0:0:03.12 ('h\:m\:ss\.FFFFFFF') --> 00:00:03.1200000
        '    0:0:03.1279569 ('h\:m\:ss\.FFFFFFF') --> 00:00:03.1279569     
        ' </Snippet27>
    End Sub
End Module

Back to table

The "FFF" custom format specifier

The "FFF" custom format specifier (with three F characters) outputs the milliseconds in a time interval. In a formatting operation, any remaining fractional digits are truncated. If there are any trailing fractional zeros, they aren't included in the result string. In a parsing operation that calls the System.TimeSpan.ParseExact or System.TimeSpan.TryParseExact method, the presence of the tenths, hundredths, and thousandths of a second digit is optional.

The following example uses the "FFF" custom format specifier to display the thousandths of a second in a System.TimeSpan value. It also uses this custom format specifier in a parsing operation.

sample · csharpsamples/snippets/csharp/VS_Snippets_CLR/conceptual.timespan.custom/cs/f_specifiers1.cs23

csharp
using System;

public class Example
{
   public static void Main()
   {
      FSpecifier();
      Console.WriteLine("-----");
      FFSpecifier();
      Console.WriteLine("-----");
      FFFSpecifier();
      Console.WriteLine("-----");
      FFFFSpecifier();
      Console.WriteLine("-----");
      FFFFFSpecifier();
      Console.WriteLine("-----");
      FFFFFFSpecifier();
      Console.WriteLine("-----");
      F7Specifier();
   }

   private static void FSpecifier()
   {
      // <Snippet21>
      Console.WriteLine("Formatting:");
      TimeSpan ts1 = TimeSpan.Parse("0:0:3.669");
      Console.WriteLine($"{ts1} ('%F') --> {ts1:%F}");

      TimeSpan ts2 = TimeSpan.Parse("0:0:3.091");
      Console.WriteLine($"{ts2} ('ss\\.F') --> {ts2:ss\\.F}");
      Console.WriteLine();

      Console.WriteLine("Parsing:");
      string[] inputs = { "0:0:03.", "0:0:03.1", "0:0:03.12" };
      string fmt = @"h\:m\:ss\.F";
      TimeSpan ts3;

      foreach (string input in inputs) {
         if (TimeSpan.TryParseExact(input, fmt, null, out ts3))
            Console.WriteLine($"{input} ('{fmt}') --> {ts3}");
         else
            Console.WriteLine($"Cannot parse {input} with '{fmt}'.");
      }
      // The example displays the following output:
      //       Formatting:
      //       00:00:03.6690000 ('%F') --> 6
      //       00:00:03.0910000 ('ss\.F') --> 03.
      //
      //       Parsing:
      //       0:0:03. ('h\:m\:ss\.F') --> 00:00:03
      //       0:0:03.1 ('h\:m\:ss\.F') --> 00:00:03.1000000
      //       Cannot parse 0:0:03.12 with 'h\:m\:ss\.F'.
      // </Snippet21>
   }

   private static void FFSpecifier()
   {
      // <Snippet22>
      Console.WriteLine("Formatting:");
      TimeSpan ts1 = TimeSpan.Parse("0:0:3.697");
      Console.WriteLine($"{ts1} ('FF') --> {ts1:FF}");

      TimeSpan ts2 = TimeSpan.Parse("0:0:3.809");
      Console.WriteLine($"{ts2} ('ss\\.FF') --> {ts2:ss\\.FF}");
      Console.WriteLine();

      Console.WriteLine("Parsing:");
      string[] inputs = { "0:0:03.", "0:0:03.1", "0:0:03.127" };
      string fmt = @"h\:m\:ss\.FF";
      TimeSpan ts3;

      foreach (string input in inputs) {
         if (TimeSpan.TryParseExact(input, fmt, null, out ts3))
            Console.WriteLine($"{input} ('{fmt}') --> {ts3}");
         else
            Console.WriteLine($"Cannot parse {input} with '{fmt}'.");
      }
      // The example displays the following output:
      //       Formatting:
      //       00:00:03.6970000 ('FF') --> 69
      //       00:00:03.8090000 ('ss\.FF') --> 03.8
      //
      //       Parsing:
      //       0:0:03. ('h\:m\:ss\.FF') --> 00:00:03
      //       0:0:03.1 ('h\:m\:ss\.FF') --> 00:00:03.1000000
      //       Cannot parse 0:0:03.127 with 'h\:m\:ss\.FF'.
      // </Snippet22>
   }

   private static void FFFSpecifier()
   {
      // <Snippet23>
      Console.WriteLine("Formatting:");
      TimeSpan ts1 = TimeSpan.Parse("0:0:3.6974");
      Console.WriteLine($"{ts1} ('FFF') --> {ts1:FFF}");

      TimeSpan ts2 = TimeSpan.Parse("0:0:3.8009");
      Console.WriteLine($"{ts2} ('ss\\.FFF') --> {ts2:ss\\.FFF}");
      Console.WriteLine();

      Console.WriteLine("Parsing:");
      string[] inputs = { "0:0:03.", "0:0:03.12", "0:0:03.1279" };
      string fmt = @"h\:m\:ss\.FFF";
      TimeSpan ts3;

      foreach (string input in inputs) {
         if (TimeSpan.TryParseExact(input, fmt, null, out ts3))
            Console.WriteLine($"{input} ('{fmt}') --> {ts3}");
         else
            Console.WriteLine($"Cannot parse {input} with '{fmt}'.");
      }
      // The example displays the following output:
      //       Formatting:
      //       00:00:03.6974000 ('FFF') --> 697
      //       00:00:03.8009000 ('ss\.FFF') --> 03.8
      //
      //       Parsing:
      //       0:0:03. ('h\:m\:ss\.FFF') --> 00:00:03
      //       0:0:03.12 ('h\:m\:ss\.FFF') --> 00:00:03.1200000
      //       Cannot parse 0:0:03.1279 with 'h\:m\:ss\.FFF'.
      // </Snippet23>
   }

   private static void FFFFSpecifier()
   {
      // <Snippet24>
      Console.WriteLine("Formatting:");
      TimeSpan ts1 = TimeSpan.Parse("0:0:3.69749");
      Console.WriteLine($"{ts1} ('FFFF') --> {ts1:FFFF}");

      TimeSpan ts2 = TimeSpan.Parse("0:0:3.80009");
      Console.WriteLine($"{ts2} ('ss\\.FFFF') --> {ts2:ss\\.FFFF}");
      Console.WriteLine();

      Console.WriteLine("Parsing:");
      string[] inputs = { "0:0:03.", "0:0:03.12", "0:0:03.12795" };
      string fmt = @"h\:m\:ss\.FFFF";
      TimeSpan ts3;

      foreach (string input in inputs) {
         if (TimeSpan.TryParseExact(input, fmt, null, out ts3))
            Console.WriteLine($"{input} ('{fmt}') --> {ts3}");
         else
            Console.WriteLine($"Cannot parse {input} with '{fmt}'.");
      }
      // The example displays the following output:
      //       Formatting:
      //       00:00:03.6974900 ('FFFF') --> 6974
      //       00:00:03.8000900 ('ss\.FFFF') --> 03.8
      //
      //       Parsing:
      //       0:0:03. ('h\:m\:ss\.FFFF') --> 00:00:03
      //       0:0:03.12 ('h\:m\:ss\.FFFF') --> 00:00:03.1200000
      //       Cannot parse 0:0:03.12795 with 'h\:m\:ss\.FFFF'.
      // </Snippet24>
   }

   private static void FFFFFSpecifier()
   {
      // <Snippet25>
      Console.WriteLine("Formatting:");
      TimeSpan ts1 = TimeSpan.Parse("0:0:3.697497");
      Console.WriteLine($"{ts1} ('FFFFF') --> {ts1:FFFFF}");

      TimeSpan ts2 = TimeSpan.Parse("0:0:3.800009");
      Console.WriteLine($"{ts2} ('ss\\.FFFFF') --> {ts2:ss\\.FFFFF}");
      Console.WriteLine();

      Console.WriteLine("Parsing:");
      string[] inputs = { "0:0:03.", "0:0:03.12", "0:0:03.127956" };
      string fmt = @"h\:m\:ss\.FFFFF";
      TimeSpan ts3;

      foreach (string input in inputs) {
         if (TimeSpan.TryParseExact(input, fmt, null, out ts3))
            Console.WriteLine($"{input} ('{fmt}') --> {ts3}");
         else
            Console.WriteLine($"Cannot parse {input} with '{fmt}'.");
      }
      // The example displays the following output:
      //       Formatting:
      //       00:00:03.6974970 ('FFFFF') --> 69749
      //       00:00:03.8000090 ('ss\.FFFFF') --> 03.8
      //
      //       Parsing:
      //       0:0:03. ('h\:m\:ss\.FFFF') --> 00:00:03
      //       0:0:03.12 ('h\:m\:ss\.FFFF') --> 00:00:03.1200000
      //       Cannot parse 0:0:03.127956 with 'h\:m\:ss\.FFFF'.
      // </Snippet25>
   }

   private static void FFFFFFSpecifier()
   {
      // <Snippet26>
      Console.WriteLine("Formatting:");
      TimeSpan ts1 = TimeSpan.Parse("0:0:3.6974974");
      Console.WriteLine($"{ts1} ('FFFFFF') --> {ts1:FFFFFF}");

      TimeSpan ts2 = TimeSpan.Parse("0:0:3.8000009");
      Console.WriteLine($"{ts2} ('ss\\.FFFFFF') --> {ts2:ss\\.FFFFFF}");
      Console.WriteLine();

      Console.WriteLine("Parsing:");
      string[] inputs = { "0:0:03.", "0:0:03.12", "0:0:03.1279569" };
      string fmt = @"h\:m\:ss\.FFFFFF";
      TimeSpan ts3;

      foreach (string input in inputs) {
         if (TimeSpan.TryParseExact(input, fmt, null, out ts3))
            Console.WriteLine($"{input} ('{fmt}') --> {ts3}");
         else
            Console.WriteLine($"Cannot parse {input} with '{fmt}'.");
      }
      // The example displays the following output:
      //       Formatting:
      //       00:00:03.6974974 ('FFFFFF') --> 697497
      //       00:00:03.8000009 ('ss\.FFFFFF') --> 03.8
      //
      //       Parsing:
      //       0:0:03. ('h\:m\:ss\.FFFFFF') --> 00:00:03
      //       0:0:03.12 ('h\:m\:ss\.FFFFFF') --> 00:00:03.1200000
      //       Cannot parse 0:0:03.1279569 with 'h\:m\:ss\.FFFFFF'.
      // </Snippet26>
   }

   private static void F7Specifier()
   {
      // <Snippet27>
      Console.WriteLine("Formatting:");
      TimeSpan ts1 = TimeSpan.Parse("0:0:3.6974974");
      Console.WriteLine($"{ts1} ('FFFFFFF') --> {ts1:FFFFFFF}");

      TimeSpan ts2 = TimeSpan.Parse("0:0:3.9500000");
      Console.WriteLine($"{ts2} ('ss\\.FFFFFFF') --> {ts2:ss\\.FFFFFFF}");
      Console.WriteLine();

      Console.WriteLine("Parsing:");
      string[] inputs = { "0:0:03.", "0:0:03.12", "0:0:03.1279569" };
      string fmt = @"h\:m\:ss\.FFFFFFF";
      TimeSpan ts3;

      foreach (string input in inputs) {
         if (TimeSpan.TryParseExact(input, fmt, null, out ts3))
            Console.WriteLine($"{input} ('{fmt}') --> {ts3}");
         else
            Console.WriteLine($"Cannot parse {input} with '{fmt}'.");
      }
      // The example displays the following output:
      //    Formatting:
      //    00:00:03.6974974 ('FFFFFFF') --> 6974974
      //    00:00:03.9500000 ('ss\.FFFFFFF') --> 03.95
      //
      //    Parsing:
      //    0:0:03. ('h\:m\:ss\.FFFFFFF') --> 00:00:03
      //    0:0:03.12 ('h\:m\:ss\.FFFFFFF') --> 00:00:03.1200000
      //    0:0:03.1279569 ('h\:m\:ss\.FFFFFFF') --> 00:00:03.1279569
      // </Snippet27>
   }
}

sample · vbsamples/snippets/visualbasic/VS_Snippets_CLR/conceptual.timespan.custom/vb/f_specifiers1.vb23

vb
' Visual Basic .NET Document
Option Strict On

Module Example
    Public Sub Main()
        FSpecifier()
        Console.WriteLine("-----")
        FFSpecifier()
        Console.WriteLine("-----")
        FFFSpecifier()
        Console.WriteLine("-----")
        FFFFSpecifier()
        Console.WriteLine("-----")
        FFFFFSpecifier()
        Console.WriteLine("-----")
        FFFFFFSpecifier()
        Console.WriteLine("-----")
        F7Specifier()
    End Sub

    Private Sub FSpecifier()
        ' <Snippet21>
        Console.WriteLine("Formatting:")
        Dim ts1 As TimeSpan = TimeSpan.Parse("0:0:3.669")
        Console.WriteLine("{0} ('%F') --> {0:%F}", ts1)

        Dim ts2 As TimeSpan = TimeSpan.Parse("0:0:3.091")
        Console.WriteLine("{0} ('ss\.F') --> {0:ss\.F}", ts2)
        Console.WriteLine()

        Console.WriteLine("Parsing:")
        Dim inputs() As String = {"0:0:03.", "0:0:03.1", "0:0:03.12"}
        Dim fmt As String = "h\:m\:ss\.F"
        Dim ts3 As TimeSpan

        For Each input As String In inputs
            If TimeSpan.TryParseExact(input, fmt, Nothing, ts3)
                Console.WriteLine("{0} ('{1}') --> {2}", input, fmt, ts3)
            Else
                Console.WriteLine("Cannot parse {0} with '{1}'.",
                                  input, fmt)
            End If
        Next
        ' The example displays the following output:
        '       Formatting:
        '       00:00:03.6690000 ('%F') --> 6
        '       00:00:03.0910000 ('ss\.F') --> 03.
        '       
        '       Parsing:
        '       0:0:03. ('h\:m\:ss\.F') --> 00:00:03
        '       0:0:03.1 ('h\:m\:ss\.F') --> 00:00:03.1000000
        '       Cannot parse 0:0:03.12 with 'h\:m\:ss\.F'.      
        ' </Snippet21>
    End Sub

    Private Sub FFSpecifier()
        ' <Snippet22>
        Console.WriteLine("Formatting:")
        Dim ts1 As TimeSpan = TimeSpan.Parse("0:0:3.697")
        Console.WriteLine("{0} ('FF') --> {0:FF}", ts1)

        Dim ts2 As TimeSpan = TimeSpan.Parse("0:0:3.809")
        Console.WriteLine("{0} ('ss\.FF') --> {0:ss\.FF}", ts2)
        Console.WriteLine()

        Console.WriteLine("Parsing:")
        Dim inputs() As String = {"0:0:03.", "0:0:03.1", "0:0:03.127"}
        Dim fmt As String = "h\:m\:ss\.FF"
        Dim ts3 As TimeSpan

        For Each input As String In inputs
            If TimeSpan.TryParseExact(input, fmt, Nothing, ts3)
                Console.WriteLine("{0} ('{1}') --> {2}", input, fmt, ts3)
            Else
                Console.WriteLine("Cannot parse {0} with '{1}'.",
                                  input, fmt)
            End If
        Next
        ' The example displays the following output:
        '       Formatting:
        '       00:00:03.6970000 ('FF') --> 69
        '       00:00:03.8090000 ('ss\.FF') --> 03.8
        '       
        '       Parsing:
        '       0:0:03. ('h\:m\:ss\.FF') --> 00:00:03
        '       0:0:03.1 ('h\:m\:ss\.FF') --> 00:00:03.1000000
        '       Cannot parse 0:0:03.127 with 'h\:m\:ss\.FF'.
        ' </Snippet22>
    End Sub

    Private Sub FFFSpecifier()
        ' <Snippet23>
        Console.WriteLine("Formatting:")
        Dim ts1 As TimeSpan = TimeSpan.Parse("0:0:3.6974")
        Console.WriteLine("{0} ('FFF') --> {0:FFF}", ts1)

        Dim ts2 As TimeSpan = TimeSpan.Parse("0:0:3.8009")
        Console.WriteLine("{0} ('ss\.FFF') --> {0:ss\.FFF}", ts2)
        Console.WriteLine()

        Console.WriteLine("Parsing:")
        Dim inputs() As String = {"0:0:03.", "0:0:03.12", "0:0:03.1279"}
        Dim fmt As String = "h\:m\:ss\.FFF"
        Dim ts3 As TimeSpan

        For Each input As String In inputs
            If TimeSpan.TryParseExact(input, fmt, Nothing, ts3)
                Console.WriteLine("{0} ('{1}') --> {2}", input, fmt, ts3)
            Else
                Console.WriteLine("Cannot parse {0} with '{1}'.",
                                  input, fmt)
            End If
        Next
        ' The example displays the following output:
        '       Formatting:
        '       00:00:03.6974000 ('FFF') --> 697
        '       00:00:03.8009000 ('ss\.FFF') --> 03.8
        '       
        '       Parsing:
        '       0:0:03. ('h\:m\:ss\.FFF') --> 00:00:03
        '       0:0:03.12 ('h\:m\:ss\.FFF') --> 00:00:03.1200000
        '       Cannot parse 0:0:03.1279 with 'h\:m\:ss\.FFF'.
        ' </Snippet23>
    End Sub

    Private Sub FFFFSpecifier()
        ' <Snippet24>
        Console.WriteLine("Formatting:")
        Dim ts1 As TimeSpan = TimeSpan.Parse("0:0:3.69749")
        Console.WriteLine("{0} ('FFFF') --> {0:FFFF}", ts1)

        Dim ts2 As TimeSpan = TimeSpan.Parse("0:0:3.80009")
        Console.WriteLine("{0} ('ss\.FFFF') --> {0:ss\.FFFF}", ts2)
        Console.WriteLine()

        Console.WriteLine("Parsing:")
        Dim inputs() As String = {"0:0:03.", "0:0:03.12", "0:0:03.12795"}
        Dim fmt As String = "h\:m\:ss\.FFFF"
        Dim ts3 As TimeSpan

        For Each input As String In inputs
            If TimeSpan.TryParseExact(input, fmt, Nothing, ts3)
                Console.WriteLine("{0} ('{1}') --> {2}", input, fmt, ts3)
            Else
                Console.WriteLine("Cannot parse {0} with '{1}'.",
                                  input, fmt)
            End If
        Next
        ' The example displays the following output:
        '       Formatting:
        '       00:00:03.6974900 ('FFFF') --> 6974
        '       00:00:03.8000900 ('ss\.FFFF') --> 03.8
        '       
        '       Parsing:
        '       0:0:03. ('h\:m\:ss\.FFFF') --> 00:00:03
        '       0:0:03.12 ('h\:m\:ss\.FFFF') --> 00:00:03.1200000
        '       Cannot parse 0:0:03.12795 with 'h\:m\:ss\.FFFF'.
        ' </Snippet24>
    End Sub

    Private Sub FFFFFSpecifier()
        ' <Snippet25>
        Console.WriteLine("Formatting:")
        Dim ts1 As TimeSpan = TimeSpan.Parse("0:0:3.697497")
        Console.WriteLine("{0} ('FFFFF') --> {0:FFFFF}", ts1)

        Dim ts2 As TimeSpan = TimeSpan.Parse("0:0:3.800009")
        Console.WriteLine("{0} ('ss\.FFFFF') --> {0:ss\.FFFFF}", ts2)
        Console.WriteLine()

        Console.WriteLine("Parsing:")
        Dim inputs() As String = {"0:0:03.", "0:0:03.12", "0:0:03.127956"}
        Dim fmt As String = "h\:m\:ss\.FFFFF"
        Dim ts3 As TimeSpan

        For Each input As String In inputs
            If TimeSpan.TryParseExact(input, fmt, Nothing, ts3)
                Console.WriteLine("{0} ('{1}') --> {2}", input, fmt, ts3)
            Else
                Console.WriteLine("Cannot parse {0} with '{1}'.",
                                  input, fmt)
            End If
        Next
        ' The example displays the following output:
        '       Formatting:
        '       00:00:03.6974970 ('FFFFF') --> 69749
        '       00:00:03.8000090 ('ss\.FFFFF') --> 03.8
        '       
        '       Parsing:
        '       0:0:03. ('h\:m\:ss\.FFFF') --> 00:00:03
        '       0:0:03.12 ('h\:m\:ss\.FFFF') --> 00:00:03.1200000
        '       Cannot parse 0:0:03.127956 with 'h\:m\:ss\.FFFF'.
        ' </Snippet25>
    End Sub

    Private Sub FFFFFFSpecifier()
        ' <Snippet26>
        Console.WriteLine("Formatting:")
        Dim ts1 As TimeSpan = TimeSpan.Parse("0:0:3.6974974")
        Console.WriteLine("{0} ('FFFFFF') --> {0:FFFFFF}", ts1)

        Dim ts2 As TimeSpan = TimeSpan.Parse("0:0:3.8000009")
        Console.WriteLine("{0} ('ss\.FFFFFF') --> {0:ss\.FFFFFF}", ts2)
        Console.WriteLine()

        Console.WriteLine("Parsing:")
        Dim inputs() As String = {"0:0:03.", "0:0:03.12", "0:0:03.1279569"}
        Dim fmt As String = "h\:m\:ss\.FFFFFF"
        Dim ts3 As TimeSpan

        For Each input As String In inputs
            If TimeSpan.TryParseExact(input, fmt, Nothing, ts3)
                Console.WriteLine("{0} ('{1}') --> {2}", input, fmt, ts3)
            Else
                Console.WriteLine("Cannot parse {0} with '{1}'.",
                                  input, fmt)
            End If
        Next
        ' The example displays the following output:
        '       Formatting:
        '       00:00:03.6974974 ('FFFFFF') --> 697497
        '       00:00:03.8000009 ('ss\.FFFFFF') --> 03.8
        '       
        '       Parsing:
        '       0:0:03. ('h\:m\:ss\.FFFFFF') --> 00:00:03
        '       0:0:03.12 ('h\:m\:ss\.FFFFFF') --> 00:00:03.1200000
        '       Cannot parse 0:0:03.1279569 with 'h\:m\:ss\.FFFFFF'.
        ' </Snippet26>
    End Sub

    Private Sub F7Specifier()
        ' <Snippet27>
        Console.WriteLine("Formatting:")
        Dim ts1 As TimeSpan = TimeSpan.Parse("0:0:3.6974974")
        Console.WriteLine("{0} ('FFFFFFF') --> {0:FFFFFFF}", ts1)

        Dim ts2 As TimeSpan = TimeSpan.Parse("0:0:3.9500000")
        Console.WriteLine("{0} ('ss\.FFFFFFF') --> {0:ss\.FFFFFFF}", ts2)
        Console.WriteLine()

        Console.WriteLine("Parsing:")
        Dim inputs() As String = {"0:0:03.", "0:0:03.12", "0:0:03.1279569"}
        Dim fmt As String = "h\:m\:ss\.FFFFFFF"
        Dim ts3 As TimeSpan

        For Each input As String In inputs
            If TimeSpan.TryParseExact(input, fmt, Nothing, ts3)
                Console.WriteLine("{0} ('{1}') --> {2}", input, fmt, ts3)
            Else
                Console.WriteLine("Cannot parse {0} with '{1}'.",
                                  input, fmt)
            End If
        Next
        ' The example displays the following output:
        '    Formatting:
        '    00:00:03.6974974 ('FFFFFFF') --> 6974974
        '    00:00:03.9500000 ('ss\.FFFFFFF') --> 03.95
        '    
        '    Parsing:
        '    0:0:03. ('h\:m\:ss\.FFFFFFF') --> 00:00:03
        '    0:0:03.12 ('h\:m\:ss\.FFFFFFF') --> 00:00:03.1200000
        '    0:0:03.1279569 ('h\:m\:ss\.FFFFFFF') --> 00:00:03.1279569     
        ' </Snippet27>
    End Sub
End Module

Back to table

The "FFFF" custom format specifier

The "FFFF" custom format specifier (with four F characters) outputs the ten-thousandths of a second in a time interval. In a formatting operation, any remaining fractional digits are truncated. If there are any trailing fractional zeros, they aren't included in the result string. In a parsing operation that calls the System.TimeSpan.ParseExact or System.TimeSpan.TryParseExact method, the presence of the tenths, hundredths, thousandths, and ten-thousandths of a second digit is optional.

The following example uses the "FFFF" custom format specifier to display the ten-thousandths of a second in a System.TimeSpan value. It also uses the "FFFF" custom format specifier in a parsing operation.

sample · csharpsamples/snippets/csharp/VS_Snippets_CLR/conceptual.timespan.custom/cs/f_specifiers1.cs24

csharp
using System;

public class Example
{
   public static void Main()
   {
      FSpecifier();
      Console.WriteLine("-----");
      FFSpecifier();
      Console.WriteLine("-----");
      FFFSpecifier();
      Console.WriteLine("-----");
      FFFFSpecifier();
      Console.WriteLine("-----");
      FFFFFSpecifier();
      Console.WriteLine("-----");
      FFFFFFSpecifier();
      Console.WriteLine("-----");
      F7Specifier();
   }

   private static void FSpecifier()
   {
      // <Snippet21>
      Console.WriteLine("Formatting:");
      TimeSpan ts1 = TimeSpan.Parse("0:0:3.669");
      Console.WriteLine($"{ts1} ('%F') --> {ts1:%F}");

      TimeSpan ts2 = TimeSpan.Parse("0:0:3.091");
      Console.WriteLine($"{ts2} ('ss\\.F') --> {ts2:ss\\.F}");
      Console.WriteLine();

      Console.WriteLine("Parsing:");
      string[] inputs = { "0:0:03.", "0:0:03.1", "0:0:03.12" };
      string fmt = @"h\:m\:ss\.F";
      TimeSpan ts3;

      foreach (string input in inputs) {
         if (TimeSpan.TryParseExact(input, fmt, null, out ts3))
            Console.WriteLine($"{input} ('{fmt}') --> {ts3}");
         else
            Console.WriteLine($"Cannot parse {input} with '{fmt}'.");
      }
      // The example displays the following output:
      //       Formatting:
      //       00:00:03.6690000 ('%F') --> 6
      //       00:00:03.0910000 ('ss\.F') --> 03.
      //
      //       Parsing:
      //       0:0:03. ('h\:m\:ss\.F') --> 00:00:03
      //       0:0:03.1 ('h\:m\:ss\.F') --> 00:00:03.1000000
      //       Cannot parse 0:0:03.12 with 'h\:m\:ss\.F'.
      // </Snippet21>
   }

   private static void FFSpecifier()
   {
      // <Snippet22>
      Console.WriteLine("Formatting:");
      TimeSpan ts1 = TimeSpan.Parse("0:0:3.697");
      Console.WriteLine($"{ts1} ('FF') --> {ts1:FF}");

      TimeSpan ts2 = TimeSpan.Parse("0:0:3.809");
      Console.WriteLine($"{ts2} ('ss\\.FF') --> {ts2:ss\\.FF}");
      Console.WriteLine();

      Console.WriteLine("Parsing:");
      string[] inputs = { "0:0:03.", "0:0:03.1", "0:0:03.127" };
      string fmt = @"h\:m\:ss\.FF";
      TimeSpan ts3;

      foreach (string input in inputs) {
         if (TimeSpan.TryParseExact(input, fmt, null, out ts3))
            Console.WriteLine($"{input} ('{fmt}') --> {ts3}");
         else
            Console.WriteLine($"Cannot parse {input} with '{fmt}'.");
      }
      // The example displays the following output:
      //       Formatting:
      //       00:00:03.6970000 ('FF') --> 69
      //       00:00:03.8090000 ('ss\.FF') --> 03.8
      //
      //       Parsing:
      //       0:0:03. ('h\:m\:ss\.FF') --> 00:00:03
      //       0:0:03.1 ('h\:m\:ss\.FF') --> 00:00:03.1000000
      //       Cannot parse 0:0:03.127 with 'h\:m\:ss\.FF'.
      // </Snippet22>
   }

   private static void FFFSpecifier()
   {
      // <Snippet23>
      Console.WriteLine("Formatting:");
      TimeSpan ts1 = TimeSpan.Parse("0:0:3.6974");
      Console.WriteLine($"{ts1} ('FFF') --> {ts1:FFF}");

      TimeSpan ts2 = TimeSpan.Parse("0:0:3.8009");
      Console.WriteLine($"{ts2} ('ss\\.FFF') --> {ts2:ss\\.FFF}");
      Console.WriteLine();

      Console.WriteLine("Parsing:");
      string[] inputs = { "0:0:03.", "0:0:03.12", "0:0:03.1279" };
      string fmt = @"h\:m\:ss\.FFF";
      TimeSpan ts3;

      foreach (string input in inputs) {
         if (TimeSpan.TryParseExact(input, fmt, null, out ts3))
            Console.WriteLine($"{input} ('{fmt}') --> {ts3}");
         else
            Console.WriteLine($"Cannot parse {input} with '{fmt}'.");
      }
      // The example displays the following output:
      //       Formatting:
      //       00:00:03.6974000 ('FFF') --> 697
      //       00:00:03.8009000 ('ss\.FFF') --> 03.8
      //
      //       Parsing:
      //       0:0:03. ('h\:m\:ss\.FFF') --> 00:00:03
      //       0:0:03.12 ('h\:m\:ss\.FFF') --> 00:00:03.1200000
      //       Cannot parse 0:0:03.1279 with 'h\:m\:ss\.FFF'.
      // </Snippet23>
   }

   private static void FFFFSpecifier()
   {
      // <Snippet24>
      Console.WriteLine("Formatting:");
      TimeSpan ts1 = TimeSpan.Parse("0:0:3.69749");
      Console.WriteLine($"{ts1} ('FFFF') --> {ts1:FFFF}");

      TimeSpan ts2 = TimeSpan.Parse("0:0:3.80009");
      Console.WriteLine($"{ts2} ('ss\\.FFFF') --> {ts2:ss\\.FFFF}");
      Console.WriteLine();

      Console.WriteLine("Parsing:");
      string[] inputs = { "0:0:03.", "0:0:03.12", "0:0:03.12795" };
      string fmt = @"h\:m\:ss\.FFFF";
      TimeSpan ts3;

      foreach (string input in inputs) {
         if (TimeSpan.TryParseExact(input, fmt, null, out ts3))
            Console.WriteLine($"{input} ('{fmt}') --> {ts3}");
         else
            Console.WriteLine($"Cannot parse {input} with '{fmt}'.");
      }
      // The example displays the following output:
      //       Formatting:
      //       00:00:03.6974900 ('FFFF') --> 6974
      //       00:00:03.8000900 ('ss\.FFFF') --> 03.8
      //
      //       Parsing:
      //       0:0:03. ('h\:m\:ss\.FFFF') --> 00:00:03
      //       0:0:03.12 ('h\:m\:ss\.FFFF') --> 00:00:03.1200000
      //       Cannot parse 0:0:03.12795 with 'h\:m\:ss\.FFFF'.
      // </Snippet24>
   }

   private static void FFFFFSpecifier()
   {
      // <Snippet25>
      Console.WriteLine("Formatting:");
      TimeSpan ts1 = TimeSpan.Parse("0:0:3.697497");
      Console.WriteLine($"{ts1} ('FFFFF') --> {ts1:FFFFF}");

      TimeSpan ts2 = TimeSpan.Parse("0:0:3.800009");
      Console.WriteLine($"{ts2} ('ss\\.FFFFF') --> {ts2:ss\\.FFFFF}");
      Console.WriteLine();

      Console.WriteLine("Parsing:");
      string[] inputs = { "0:0:03.", "0:0:03.12", "0:0:03.127956" };
      string fmt = @"h\:m\:ss\.FFFFF";
      TimeSpan ts3;

      foreach (string input in inputs) {
         if (TimeSpan.TryParseExact(input, fmt, null, out ts3))
            Console.WriteLine($"{input} ('{fmt}') --> {ts3}");
         else
            Console.WriteLine($"Cannot parse {input} with '{fmt}'.");
      }
      // The example displays the following output:
      //       Formatting:
      //       00:00:03.6974970 ('FFFFF') --> 69749
      //       00:00:03.8000090 ('ss\.FFFFF') --> 03.8
      //
      //       Parsing:
      //       0:0:03. ('h\:m\:ss\.FFFF') --> 00:00:03
      //       0:0:03.12 ('h\:m\:ss\.FFFF') --> 00:00:03.1200000
      //       Cannot parse 0:0:03.127956 with 'h\:m\:ss\.FFFF'.
      // </Snippet25>
   }

   private static void FFFFFFSpecifier()
   {
      // <Snippet26>
      Console.WriteLine("Formatting:");
      TimeSpan ts1 = TimeSpan.Parse("0:0:3.6974974");
      Console.WriteLine($"{ts1} ('FFFFFF') --> {ts1:FFFFFF}");

      TimeSpan ts2 = TimeSpan.Parse("0:0:3.8000009");
      Console.WriteLine($"{ts2} ('ss\\.FFFFFF') --> {ts2:ss\\.FFFFFF}");
      Console.WriteLine();

      Console.WriteLine("Parsing:");
      string[] inputs = { "0:0:03.", "0:0:03.12", "0:0:03.1279569" };
      string fmt = @"h\:m\:ss\.FFFFFF";
      TimeSpan ts3;

      foreach (string input in inputs) {
         if (TimeSpan.TryParseExact(input, fmt, null, out ts3))
            Console.WriteLine($"{input} ('{fmt}') --> {ts3}");
         else
            Console.WriteLine($"Cannot parse {input} with '{fmt}'.");
      }
      // The example displays the following output:
      //       Formatting:
      //       00:00:03.6974974 ('FFFFFF') --> 697497
      //       00:00:03.8000009 ('ss\.FFFFFF') --> 03.8
      //
      //       Parsing:
      //       0:0:03. ('h\:m\:ss\.FFFFFF') --> 00:00:03
      //       0:0:03.12 ('h\:m\:ss\.FFFFFF') --> 00:00:03.1200000
      //       Cannot parse 0:0:03.1279569 with 'h\:m\:ss\.FFFFFF'.
      // </Snippet26>
   }

   private static void F7Specifier()
   {
      // <Snippet27>
      Console.WriteLine("Formatting:");
      TimeSpan ts1 = TimeSpan.Parse("0:0:3.6974974");
      Console.WriteLine($"{ts1} ('FFFFFFF') --> {ts1:FFFFFFF}");

      TimeSpan ts2 = TimeSpan.Parse("0:0:3.9500000");
      Console.WriteLine($"{ts2} ('ss\\.FFFFFFF') --> {ts2:ss\\.FFFFFFF}");
      Console.WriteLine();

      Console.WriteLine("Parsing:");
      string[] inputs = { "0:0:03.", "0:0:03.12", "0:0:03.1279569" };
      string fmt = @"h\:m\:ss\.FFFFFFF";
      TimeSpan ts3;

      foreach (string input in inputs) {
         if (TimeSpan.TryParseExact(input, fmt, null, out ts3))
            Console.WriteLine($"{input} ('{fmt}') --> {ts3}");
         else
            Console.WriteLine($"Cannot parse {input} with '{fmt}'.");
      }
      // The example displays the following output:
      //    Formatting:
      //    00:00:03.6974974 ('FFFFFFF') --> 6974974
      //    00:00:03.9500000 ('ss\.FFFFFFF') --> 03.95
      //
      //    Parsing:
      //    0:0:03. ('h\:m\:ss\.FFFFFFF') --> 00:00:03
      //    0:0:03.12 ('h\:m\:ss\.FFFFFFF') --> 00:00:03.1200000
      //    0:0:03.1279569 ('h\:m\:ss\.FFFFFFF') --> 00:00:03.1279569
      // </Snippet27>
   }
}

sample · vbsamples/snippets/visualbasic/VS_Snippets_CLR/conceptual.timespan.custom/vb/f_specifiers1.vb24

vb
' Visual Basic .NET Document
Option Strict On

Module Example
    Public Sub Main()
        FSpecifier()
        Console.WriteLine("-----")
        FFSpecifier()
        Console.WriteLine("-----")
        FFFSpecifier()
        Console.WriteLine("-----")
        FFFFSpecifier()
        Console.WriteLine("-----")
        FFFFFSpecifier()
        Console.WriteLine("-----")
        FFFFFFSpecifier()
        Console.WriteLine("-----")
        F7Specifier()
    End Sub

    Private Sub FSpecifier()
        ' <Snippet21>
        Console.WriteLine("Formatting:")
        Dim ts1 As TimeSpan = TimeSpan.Parse("0:0:3.669")
        Console.WriteLine("{0} ('%F') --> {0:%F}", ts1)

        Dim ts2 As TimeSpan = TimeSpan.Parse("0:0:3.091")
        Console.WriteLine("{0} ('ss\.F') --> {0:ss\.F}", ts2)
        Console.WriteLine()

        Console.WriteLine("Parsing:")
        Dim inputs() As String = {"0:0:03.", "0:0:03.1", "0:0:03.12"}
        Dim fmt As String = "h\:m\:ss\.F"
        Dim ts3 As TimeSpan

        For Each input As String In inputs
            If TimeSpan.TryParseExact(input, fmt, Nothing, ts3)
                Console.WriteLine("{0} ('{1}') --> {2}", input, fmt, ts3)
            Else
                Console.WriteLine("Cannot parse {0} with '{1}'.",
                                  input, fmt)
            End If
        Next
        ' The example displays the following output:
        '       Formatting:
        '       00:00:03.6690000 ('%F') --> 6
        '       00:00:03.0910000 ('ss\.F') --> 03.
        '       
        '       Parsing:
        '       0:0:03. ('h\:m\:ss\.F') --> 00:00:03
        '       0:0:03.1 ('h\:m\:ss\.F') --> 00:00:03.1000000
        '       Cannot parse 0:0:03.12 with 'h\:m\:ss\.F'.      
        ' </Snippet21>
    End Sub

    Private Sub FFSpecifier()
        ' <Snippet22>
        Console.WriteLine("Formatting:")
        Dim ts1 As TimeSpan = TimeSpan.Parse("0:0:3.697")
        Console.WriteLine("{0} ('FF') --> {0:FF}", ts1)

        Dim ts2 As TimeSpan = TimeSpan.Parse("0:0:3.809")
        Console.WriteLine("{0} ('ss\.FF') --> {0:ss\.FF}", ts2)
        Console.WriteLine()

        Console.WriteLine("Parsing:")
        Dim inputs() As String = {"0:0:03.", "0:0:03.1", "0:0:03.127"}
        Dim fmt As String = "h\:m\:ss\.FF"
        Dim ts3 As TimeSpan

        For Each input As String In inputs
            If TimeSpan.TryParseExact(input, fmt, Nothing, ts3)
                Console.WriteLine("{0} ('{1}') --> {2}", input, fmt, ts3)
            Else
                Console.WriteLine("Cannot parse {0} with '{1}'.",
                                  input, fmt)
            End If
        Next
        ' The example displays the following output:
        '       Formatting:
        '       00:00:03.6970000 ('FF') --> 69
        '       00:00:03.8090000 ('ss\.FF') --> 03.8
        '       
        '       Parsing:
        '       0:0:03. ('h\:m\:ss\.FF') --> 00:00:03
        '       0:0:03.1 ('h\:m\:ss\.FF') --> 00:00:03.1000000
        '       Cannot parse 0:0:03.127 with 'h\:m\:ss\.FF'.
        ' </Snippet22>
    End Sub

    Private Sub FFFSpecifier()
        ' <Snippet23>
        Console.WriteLine("Formatting:")
        Dim ts1 As TimeSpan = TimeSpan.Parse("0:0:3.6974")
        Console.WriteLine("{0} ('FFF') --> {0:FFF}", ts1)

        Dim ts2 As TimeSpan = TimeSpan.Parse("0:0:3.8009")
        Console.WriteLine("{0} ('ss\.FFF') --> {0:ss\.FFF}", ts2)
        Console.WriteLine()

        Console.WriteLine("Parsing:")
        Dim inputs() As String = {"0:0:03.", "0:0:03.12", "0:0:03.1279"}
        Dim fmt As String = "h\:m\:ss\.FFF"
        Dim ts3 As TimeSpan

        For Each input As String In inputs
            If TimeSpan.TryParseExact(input, fmt, Nothing, ts3)
                Console.WriteLine("{0} ('{1}') --> {2}", input, fmt, ts3)
            Else
                Console.WriteLine("Cannot parse {0} with '{1}'.",
                                  input, fmt)
            End If
        Next
        ' The example displays the following output:
        '       Formatting:
        '       00:00:03.6974000 ('FFF') --> 697
        '       00:00:03.8009000 ('ss\.FFF') --> 03.8
        '       
        '       Parsing:
        '       0:0:03. ('h\:m\:ss\.FFF') --> 00:00:03
        '       0:0:03.12 ('h\:m\:ss\.FFF') --> 00:00:03.1200000
        '       Cannot parse 0:0:03.1279 with 'h\:m\:ss\.FFF'.
        ' </Snippet23>
    End Sub

    Private Sub FFFFSpecifier()
        ' <Snippet24>
        Console.WriteLine("Formatting:")
        Dim ts1 As TimeSpan = TimeSpan.Parse("0:0:3.69749")
        Console.WriteLine("{0} ('FFFF') --> {0:FFFF}", ts1)

        Dim ts2 As TimeSpan = TimeSpan.Parse("0:0:3.80009")
        Console.WriteLine("{0} ('ss\.FFFF') --> {0:ss\.FFFF}", ts2)
        Console.WriteLine()

        Console.WriteLine("Parsing:")
        Dim inputs() As String = {"0:0:03.", "0:0:03.12", "0:0:03.12795"}
        Dim fmt As String = "h\:m\:ss\.FFFF"
        Dim ts3 As TimeSpan

        For Each input As String In inputs
            If TimeSpan.TryParseExact(input, fmt, Nothing, ts3)
                Console.WriteLine("{0} ('{1}') --> {2}", input, fmt, ts3)
            Else
                Console.WriteLine("Cannot parse {0} with '{1}'.",
                                  input, fmt)
            End If
        Next
        ' The example displays the following output:
        '       Formatting:
        '       00:00:03.6974900 ('FFFF') --> 6974
        '       00:00:03.8000900 ('ss\.FFFF') --> 03.8
        '       
        '       Parsing:
        '       0:0:03. ('h\:m\:ss\.FFFF') --> 00:00:03
        '       0:0:03.12 ('h\:m\:ss\.FFFF') --> 00:00:03.1200000
        '       Cannot parse 0:0:03.12795 with 'h\:m\:ss\.FFFF'.
        ' </Snippet24>
    End Sub

    Private Sub FFFFFSpecifier()
        ' <Snippet25>
        Console.WriteLine("Formatting:")
        Dim ts1 As TimeSpan = TimeSpan.Parse("0:0:3.697497")
        Console.WriteLine("{0} ('FFFFF') --> {0:FFFFF}", ts1)

        Dim ts2 As TimeSpan = TimeSpan.Parse("0:0:3.800009")
        Console.WriteLine("{0} ('ss\.FFFFF') --> {0:ss\.FFFFF}", ts2)
        Console.WriteLine()

        Console.WriteLine("Parsing:")
        Dim inputs() As String = {"0:0:03.", "0:0:03.12", "0:0:03.127956"}
        Dim fmt As String = "h\:m\:ss\.FFFFF"
        Dim ts3 As TimeSpan

        For Each input As String In inputs
            If TimeSpan.TryParseExact(input, fmt, Nothing, ts3)
                Console.WriteLine("{0} ('{1}') --> {2}", input, fmt, ts3)
            Else
                Console.WriteLine("Cannot parse {0} with '{1}'.",
                                  input, fmt)
            End If
        Next
        ' The example displays the following output:
        '       Formatting:
        '       00:00:03.6974970 ('FFFFF') --> 69749
        '       00:00:03.8000090 ('ss\.FFFFF') --> 03.8
        '       
        '       Parsing:
        '       0:0:03. ('h\:m\:ss\.FFFF') --> 00:00:03
        '       0:0:03.12 ('h\:m\:ss\.FFFF') --> 00:00:03.1200000
        '       Cannot parse 0:0:03.127956 with 'h\:m\:ss\.FFFF'.
        ' </Snippet25>
    End Sub

    Private Sub FFFFFFSpecifier()
        ' <Snippet26>
        Console.WriteLine("Formatting:")
        Dim ts1 As TimeSpan = TimeSpan.Parse("0:0:3.6974974")
        Console.WriteLine("{0} ('FFFFFF') --> {0:FFFFFF}", ts1)

        Dim ts2 As TimeSpan = TimeSpan.Parse("0:0:3.8000009")
        Console.WriteLine("{0} ('ss\.FFFFFF') --> {0:ss\.FFFFFF}", ts2)
        Console.WriteLine()

        Console.WriteLine("Parsing:")
        Dim inputs() As String = {"0:0:03.", "0:0:03.12", "0:0:03.1279569"}
        Dim fmt As String = "h\:m\:ss\.FFFFFF"
        Dim ts3 As TimeSpan

        For Each input As String In inputs
            If TimeSpan.TryParseExact(input, fmt, Nothing, ts3)
                Console.WriteLine("{0} ('{1}') --> {2}", input, fmt, ts3)
            Else
                Console.WriteLine("Cannot parse {0} with '{1}'.",
                                  input, fmt)
            End If
        Next
        ' The example displays the following output:
        '       Formatting:
        '       00:00:03.6974974 ('FFFFFF') --> 697497
        '       00:00:03.8000009 ('ss\.FFFFFF') --> 03.8
        '       
        '       Parsing:
        '       0:0:03. ('h\:m\:ss\.FFFFFF') --> 00:00:03
        '       0:0:03.12 ('h\:m\:ss\.FFFFFF') --> 00:00:03.1200000
        '       Cannot parse 0:0:03.1279569 with 'h\:m\:ss\.FFFFFF'.
        ' </Snippet26>
    End Sub

    Private Sub F7Specifier()
        ' <Snippet27>
        Console.WriteLine("Formatting:")
        Dim ts1 As TimeSpan = TimeSpan.Parse("0:0:3.6974974")
        Console.WriteLine("{0} ('FFFFFFF') --> {0:FFFFFFF}", ts1)

        Dim ts2 As TimeSpan = TimeSpan.Parse("0:0:3.9500000")
        Console.WriteLine("{0} ('ss\.FFFFFFF') --> {0:ss\.FFFFFFF}", ts2)
        Console.WriteLine()

        Console.WriteLine("Parsing:")
        Dim inputs() As String = {"0:0:03.", "0:0:03.12", "0:0:03.1279569"}
        Dim fmt As String = "h\:m\:ss\.FFFFFFF"
        Dim ts3 As TimeSpan

        For Each input As String In inputs
            If TimeSpan.TryParseExact(input, fmt, Nothing, ts3)
                Console.WriteLine("{0} ('{1}') --> {2}", input, fmt, ts3)
            Else
                Console.WriteLine("Cannot parse {0} with '{1}'.",
                                  input, fmt)
            End If
        Next
        ' The example displays the following output:
        '    Formatting:
        '    00:00:03.6974974 ('FFFFFFF') --> 6974974
        '    00:00:03.9500000 ('ss\.FFFFFFF') --> 03.95
        '    
        '    Parsing:
        '    0:0:03. ('h\:m\:ss\.FFFFFFF') --> 00:00:03
        '    0:0:03.12 ('h\:m\:ss\.FFFFFFF') --> 00:00:03.1200000
        '    0:0:03.1279569 ('h\:m\:ss\.FFFFFFF') --> 00:00:03.1279569     
        ' </Snippet27>
    End Sub
End Module

Back to table

The "FFFFF" custom format specifier

The "FFFFF" custom format specifier (with five F characters) outputs the hundred-thousandths of a second in a time interval. In a formatting operation, any remaining fractional digits are truncated. If there are any trailing fractional zeros, they aren't included in the result string. In a parsing operation that calls the System.TimeSpan.ParseExact or System.TimeSpan.TryParseExact method, the presence of the tenths, hundredths, thousandths, ten-thousandths, and hundred-thousandths of a second digit is optional.

The following example uses the "FFFFF" custom format specifier to display the hundred-thousandths of a second in a System.TimeSpan value. It also uses the "FFFFF" custom format specifier in a parsing operation.

sample · csharpsamples/snippets/csharp/VS_Snippets_CLR/conceptual.timespan.custom/cs/f_specifiers1.cs25

csharp
using System;

public class Example
{
   public static void Main()
   {
      FSpecifier();
      Console.WriteLine("-----");
      FFSpecifier();
      Console.WriteLine("-----");
      FFFSpecifier();
      Console.WriteLine("-----");
      FFFFSpecifier();
      Console.WriteLine("-----");
      FFFFFSpecifier();
      Console.WriteLine("-----");
      FFFFFFSpecifier();
      Console.WriteLine("-----");
      F7Specifier();
   }

   private static void FSpecifier()
   {
      // <Snippet21>
      Console.WriteLine("Formatting:");
      TimeSpan ts1 = TimeSpan.Parse("0:0:3.669");
      Console.WriteLine($"{ts1} ('%F') --> {ts1:%F}");

      TimeSpan ts2 = TimeSpan.Parse("0:0:3.091");
      Console.WriteLine($"{ts2} ('ss\\.F') --> {ts2:ss\\.F}");
      Console.WriteLine();

      Console.WriteLine("Parsing:");
      string[] inputs = { "0:0:03.", "0:0:03.1", "0:0:03.12" };
      string fmt = @"h\:m\:ss\.F";
      TimeSpan ts3;

      foreach (string input in inputs) {
         if (TimeSpan.TryParseExact(input, fmt, null, out ts3))
            Console.WriteLine($"{input} ('{fmt}') --> {ts3}");
         else
            Console.WriteLine($"Cannot parse {input} with '{fmt}'.");
      }
      // The example displays the following output:
      //       Formatting:
      //       00:00:03.6690000 ('%F') --> 6
      //       00:00:03.0910000 ('ss\.F') --> 03.
      //
      //       Parsing:
      //       0:0:03. ('h\:m\:ss\.F') --> 00:00:03
      //       0:0:03.1 ('h\:m\:ss\.F') --> 00:00:03.1000000
      //       Cannot parse 0:0:03.12 with 'h\:m\:ss\.F'.
      // </Snippet21>
   }

   private static void FFSpecifier()
   {
      // <Snippet22>
      Console.WriteLine("Formatting:");
      TimeSpan ts1 = TimeSpan.Parse("0:0:3.697");
      Console.WriteLine($"{ts1} ('FF') --> {ts1:FF}");

      TimeSpan ts2 = TimeSpan.Parse("0:0:3.809");
      Console.WriteLine($"{ts2} ('ss\\.FF') --> {ts2:ss\\.FF}");
      Console.WriteLine();

      Console.WriteLine("Parsing:");
      string[] inputs = { "0:0:03.", "0:0:03.1", "0:0:03.127" };
      string fmt = @"h\:m\:ss\.FF";
      TimeSpan ts3;

      foreach (string input in inputs) {
         if (TimeSpan.TryParseExact(input, fmt, null, out ts3))
            Console.WriteLine($"{input} ('{fmt}') --> {ts3}");
         else
            Console.WriteLine($"Cannot parse {input} with '{fmt}'.");
      }
      // The example displays the following output:
      //       Formatting:
      //       00:00:03.6970000 ('FF') --> 69
      //       00:00:03.8090000 ('ss\.FF') --> 03.8
      //
      //       Parsing:
      //       0:0:03. ('h\:m\:ss\.FF') --> 00:00:03
      //       0:0:03.1 ('h\:m\:ss\.FF') --> 00:00:03.1000000
      //       Cannot parse 0:0:03.127 with 'h\:m\:ss\.FF'.
      // </Snippet22>
   }

   private static void FFFSpecifier()
   {
      // <Snippet23>
      Console.WriteLine("Formatting:");
      TimeSpan ts1 = TimeSpan.Parse("0:0:3.6974");
      Console.WriteLine($"{ts1} ('FFF') --> {ts1:FFF}");

      TimeSpan ts2 = TimeSpan.Parse("0:0:3.8009");
      Console.WriteLine($"{ts2} ('ss\\.FFF') --> {ts2:ss\\.FFF}");
      Console.WriteLine();

      Console.WriteLine("Parsing:");
      string[] inputs = { "0:0:03.", "0:0:03.12", "0:0:03.1279" };
      string fmt = @"h\:m\:ss\.FFF";
      TimeSpan ts3;

      foreach (string input in inputs) {
         if (TimeSpan.TryParseExact(input, fmt, null, out ts3))
            Console.WriteLine($"{input} ('{fmt}') --> {ts3}");
         else
            Console.WriteLine($"Cannot parse {input} with '{fmt}'.");
      }
      // The example displays the following output:
      //       Formatting:
      //       00:00:03.6974000 ('FFF') --> 697
      //       00:00:03.8009000 ('ss\.FFF') --> 03.8
      //
      //       Parsing:
      //       0:0:03. ('h\:m\:ss\.FFF') --> 00:00:03
      //       0:0:03.12 ('h\:m\:ss\.FFF') --> 00:00:03.1200000
      //       Cannot parse 0:0:03.1279 with 'h\:m\:ss\.FFF'.
      // </Snippet23>
   }

   private static void FFFFSpecifier()
   {
      // <Snippet24>
      Console.WriteLine("Formatting:");
      TimeSpan ts1 = TimeSpan.Parse("0:0:3.69749");
      Console.WriteLine($"{ts1} ('FFFF') --> {ts1:FFFF}");

      TimeSpan ts2 = TimeSpan.Parse("0:0:3.80009");
      Console.WriteLine($"{ts2} ('ss\\.FFFF') --> {ts2:ss\\.FFFF}");
      Console.WriteLine();

      Console.WriteLine("Parsing:");
      string[] inputs = { "0:0:03.", "0:0:03.12", "0:0:03.12795" };
      string fmt = @"h\:m\:ss\.FFFF";
      TimeSpan ts3;

      foreach (string input in inputs) {
         if (TimeSpan.TryParseExact(input, fmt, null, out ts3))
            Console.WriteLine($"{input} ('{fmt}') --> {ts3}");
         else
            Console.WriteLine($"Cannot parse {input} with '{fmt}'.");
      }
      // The example displays the following output:
      //       Formatting:
      //       00:00:03.6974900 ('FFFF') --> 6974
      //       00:00:03.8000900 ('ss\.FFFF') --> 03.8
      //
      //       Parsing:
      //       0:0:03. ('h\:m\:ss\.FFFF') --> 00:00:03
      //       0:0:03.12 ('h\:m\:ss\.FFFF') --> 00:00:03.1200000
      //       Cannot parse 0:0:03.12795 with 'h\:m\:ss\.FFFF'.
      // </Snippet24>
   }

   private static void FFFFFSpecifier()
   {
      // <Snippet25>
      Console.WriteLine("Formatting:");
      TimeSpan ts1 = TimeSpan.Parse("0:0:3.697497");
      Console.WriteLine($"{ts1} ('FFFFF') --> {ts1:FFFFF}");

      TimeSpan ts2 = TimeSpan.Parse("0:0:3.800009");
      Console.WriteLine($"{ts2} ('ss\\.FFFFF') --> {ts2:ss\\.FFFFF}");
      Console.WriteLine();

      Console.WriteLine("Parsing:");
      string[] inputs = { "0:0:03.", "0:0:03.12", "0:0:03.127956" };
      string fmt = @"h\:m\:ss\.FFFFF";
      TimeSpan ts3;

      foreach (string input in inputs) {
         if (TimeSpan.TryParseExact(input, fmt, null, out ts3))
            Console.WriteLine($"{input} ('{fmt}') --> {ts3}");
         else
            Console.WriteLine($"Cannot parse {input} with '{fmt}'.");
      }
      // The example displays the following output:
      //       Formatting:
      //       00:00:03.6974970 ('FFFFF') --> 69749
      //       00:00:03.8000090 ('ss\.FFFFF') --> 03.8
      //
      //       Parsing:
      //       0:0:03. ('h\:m\:ss\.FFFF') --> 00:00:03
      //       0:0:03.12 ('h\:m\:ss\.FFFF') --> 00:00:03.1200000
      //       Cannot parse 0:0:03.127956 with 'h\:m\:ss\.FFFF'.
      // </Snippet25>
   }

   private static void FFFFFFSpecifier()
   {
      // <Snippet26>
      Console.WriteLine("Formatting:");
      TimeSpan ts1 = TimeSpan.Parse("0:0:3.6974974");
      Console.WriteLine($"{ts1} ('FFFFFF') --> {ts1:FFFFFF}");

      TimeSpan ts2 = TimeSpan.Parse("0:0:3.8000009");
      Console.WriteLine($"{ts2} ('ss\\.FFFFFF') --> {ts2:ss\\.FFFFFF}");
      Console.WriteLine();

      Console.WriteLine("Parsing:");
      string[] inputs = { "0:0:03.", "0:0:03.12", "0:0:03.1279569" };
      string fmt = @"h\:m\:ss\.FFFFFF";
      TimeSpan ts3;

      foreach (string input in inputs) {
         if (TimeSpan.TryParseExact(input, fmt, null, out ts3))
            Console.WriteLine($"{input} ('{fmt}') --> {ts3}");
         else
            Console.WriteLine($"Cannot parse {input} with '{fmt}'.");
      }
      // The example displays the following output:
      //       Formatting:
      //       00:00:03.6974974 ('FFFFFF') --> 697497
      //       00:00:03.8000009 ('ss\.FFFFFF') --> 03.8
      //
      //       Parsing:
      //       0:0:03. ('h\:m\:ss\.FFFFFF') --> 00:00:03
      //       0:0:03.12 ('h\:m\:ss\.FFFFFF') --> 00:00:03.1200000
      //       Cannot parse 0:0:03.1279569 with 'h\:m\:ss\.FFFFFF'.
      // </Snippet26>
   }

   private static void F7Specifier()
   {
      // <Snippet27>
      Console.WriteLine("Formatting:");
      TimeSpan ts1 = TimeSpan.Parse("0:0:3.6974974");
      Console.WriteLine($"{ts1} ('FFFFFFF') --> {ts1:FFFFFFF}");

      TimeSpan ts2 = TimeSpan.Parse("0:0:3.9500000");
      Console.WriteLine($"{ts2} ('ss\\.FFFFFFF') --> {ts2:ss\\.FFFFFFF}");
      Console.WriteLine();

      Console.WriteLine("Parsing:");
      string[] inputs = { "0:0:03.", "0:0:03.12", "0:0:03.1279569" };
      string fmt = @"h\:m\:ss\.FFFFFFF";
      TimeSpan ts3;

      foreach (string input in inputs) {
         if (TimeSpan.TryParseExact(input, fmt, null, out ts3))
            Console.WriteLine($"{input} ('{fmt}') --> {ts3}");
         else
            Console.WriteLine($"Cannot parse {input} with '{fmt}'.");
      }
      // The example displays the following output:
      //    Formatting:
      //    00:00:03.6974974 ('FFFFFFF') --> 6974974
      //    00:00:03.9500000 ('ss\.FFFFFFF') --> 03.95
      //
      //    Parsing:
      //    0:0:03. ('h\:m\:ss\.FFFFFFF') --> 00:00:03
      //    0:0:03.12 ('h\:m\:ss\.FFFFFFF') --> 00:00:03.1200000
      //    0:0:03.1279569 ('h\:m\:ss\.FFFFFFF') --> 00:00:03.1279569
      // </Snippet27>
   }
}

sample · vbsamples/snippets/visualbasic/VS_Snippets_CLR/conceptual.timespan.custom/vb/f_specifiers1.vb25

vb
' Visual Basic .NET Document
Option Strict On

Module Example
    Public Sub Main()
        FSpecifier()
        Console.WriteLine("-----")
        FFSpecifier()
        Console.WriteLine("-----")
        FFFSpecifier()
        Console.WriteLine("-----")
        FFFFSpecifier()
        Console.WriteLine("-----")
        FFFFFSpecifier()
        Console.WriteLine("-----")
        FFFFFFSpecifier()
        Console.WriteLine("-----")
        F7Specifier()
    End Sub

    Private Sub FSpecifier()
        ' <Snippet21>
        Console.WriteLine("Formatting:")
        Dim ts1 As TimeSpan = TimeSpan.Parse("0:0:3.669")
        Console.WriteLine("{0} ('%F') --> {0:%F}", ts1)

        Dim ts2 As TimeSpan = TimeSpan.Parse("0:0:3.091")
        Console.WriteLine("{0} ('ss\.F') --> {0:ss\.F}", ts2)
        Console.WriteLine()

        Console.WriteLine("Parsing:")
        Dim inputs() As String = {"0:0:03.", "0:0:03.1", "0:0:03.12"}
        Dim fmt As String = "h\:m\:ss\.F"
        Dim ts3 As TimeSpan

        For Each input As String In inputs
            If TimeSpan.TryParseExact(input, fmt, Nothing, ts3)
                Console.WriteLine("{0} ('{1}') --> {2}", input, fmt, ts3)
            Else
                Console.WriteLine("Cannot parse {0} with '{1}'.",
                                  input, fmt)
            End If
        Next
        ' The example displays the following output:
        '       Formatting:
        '       00:00:03.6690000 ('%F') --> 6
        '       00:00:03.0910000 ('ss\.F') --> 03.
        '       
        '       Parsing:
        '       0:0:03. ('h\:m\:ss\.F') --> 00:00:03
        '       0:0:03.1 ('h\:m\:ss\.F') --> 00:00:03.1000000
        '       Cannot parse 0:0:03.12 with 'h\:m\:ss\.F'.      
        ' </Snippet21>
    End Sub

    Private Sub FFSpecifier()
        ' <Snippet22>
        Console.WriteLine("Formatting:")
        Dim ts1 As TimeSpan = TimeSpan.Parse("0:0:3.697")
        Console.WriteLine("{0} ('FF') --> {0:FF}", ts1)

        Dim ts2 As TimeSpan = TimeSpan.Parse("0:0:3.809")
        Console.WriteLine("{0} ('ss\.FF') --> {0:ss\.FF}", ts2)
        Console.WriteLine()

        Console.WriteLine("Parsing:")
        Dim inputs() As String = {"0:0:03.", "0:0:03.1", "0:0:03.127"}
        Dim fmt As String = "h\:m\:ss\.FF"
        Dim ts3 As TimeSpan

        For Each input As String In inputs
            If TimeSpan.TryParseExact(input, fmt, Nothing, ts3)
                Console.WriteLine("{0} ('{1}') --> {2}", input, fmt, ts3)
            Else
                Console.WriteLine("Cannot parse {0} with '{1}'.",
                                  input, fmt)
            End If
        Next
        ' The example displays the following output:
        '       Formatting:
        '       00:00:03.6970000 ('FF') --> 69
        '       00:00:03.8090000 ('ss\.FF') --> 03.8
        '       
        '       Parsing:
        '       0:0:03. ('h\:m\:ss\.FF') --> 00:00:03
        '       0:0:03.1 ('h\:m\:ss\.FF') --> 00:00:03.1000000
        '       Cannot parse 0:0:03.127 with 'h\:m\:ss\.FF'.
        ' </Snippet22>
    End Sub

    Private Sub FFFSpecifier()
        ' <Snippet23>
        Console.WriteLine("Formatting:")
        Dim ts1 As TimeSpan = TimeSpan.Parse("0:0:3.6974")
        Console.WriteLine("{0} ('FFF') --> {0:FFF}", ts1)

        Dim ts2 As TimeSpan = TimeSpan.Parse("0:0:3.8009")
        Console.WriteLine("{0} ('ss\.FFF') --> {0:ss\.FFF}", ts2)
        Console.WriteLine()

        Console.WriteLine("Parsing:")
        Dim inputs() As String = {"0:0:03.", "0:0:03.12", "0:0:03.1279"}
        Dim fmt As String = "h\:m\:ss\.FFF"
        Dim ts3 As TimeSpan

        For Each input As String In inputs
            If TimeSpan.TryParseExact(input, fmt, Nothing, ts3)
                Console.WriteLine("{0} ('{1}') --> {2}", input, fmt, ts3)
            Else
                Console.WriteLine("Cannot parse {0} with '{1}'.",
                                  input, fmt)
            End If
        Next
        ' The example displays the following output:
        '       Formatting:
        '       00:00:03.6974000 ('FFF') --> 697
        '       00:00:03.8009000 ('ss\.FFF') --> 03.8
        '       
        '       Parsing:
        '       0:0:03. ('h\:m\:ss\.FFF') --> 00:00:03
        '       0:0:03.12 ('h\:m\:ss\.FFF') --> 00:00:03.1200000
        '       Cannot parse 0:0:03.1279 with 'h\:m\:ss\.FFF'.
        ' </Snippet23>
    End Sub

    Private Sub FFFFSpecifier()
        ' <Snippet24>
        Console.WriteLine("Formatting:")
        Dim ts1 As TimeSpan = TimeSpan.Parse("0:0:3.69749")
        Console.WriteLine("{0} ('FFFF') --> {0:FFFF}", ts1)

        Dim ts2 As TimeSpan = TimeSpan.Parse("0:0:3.80009")
        Console.WriteLine("{0} ('ss\.FFFF') --> {0:ss\.FFFF}", ts2)
        Console.WriteLine()

        Console.WriteLine("Parsing:")
        Dim inputs() As String = {"0:0:03.", "0:0:03.12", "0:0:03.12795"}
        Dim fmt As String = "h\:m\:ss\.FFFF"
        Dim ts3 As TimeSpan

        For Each input As String In inputs
            If TimeSpan.TryParseExact(input, fmt, Nothing, ts3)
                Console.WriteLine("{0} ('{1}') --> {2}", input, fmt, ts3)
            Else
                Console.WriteLine("Cannot parse {0} with '{1}'.",
                                  input, fmt)
            End If
        Next
        ' The example displays the following output:
        '       Formatting:
        '       00:00:03.6974900 ('FFFF') --> 6974
        '       00:00:03.8000900 ('ss\.FFFF') --> 03.8
        '       
        '       Parsing:
        '       0:0:03. ('h\:m\:ss\.FFFF') --> 00:00:03
        '       0:0:03.12 ('h\:m\:ss\.FFFF') --> 00:00:03.1200000
        '       Cannot parse 0:0:03.12795 with 'h\:m\:ss\.FFFF'.
        ' </Snippet24>
    End Sub

    Private Sub FFFFFSpecifier()
        ' <Snippet25>
        Console.WriteLine("Formatting:")
        Dim ts1 As TimeSpan = TimeSpan.Parse("0:0:3.697497")
        Console.WriteLine("{0} ('FFFFF') --> {0:FFFFF}", ts1)

        Dim ts2 As TimeSpan = TimeSpan.Parse("0:0:3.800009")
        Console.WriteLine("{0} ('ss\.FFFFF') --> {0:ss\.FFFFF}", ts2)
        Console.WriteLine()

        Console.WriteLine("Parsing:")
        Dim inputs() As String = {"0:0:03.", "0:0:03.12", "0:0:03.127956"}
        Dim fmt As String = "h\:m\:ss\.FFFFF"
        Dim ts3 As TimeSpan

        For Each input As String In inputs
            If TimeSpan.TryParseExact(input, fmt, Nothing, ts3)
                Console.WriteLine("{0} ('{1}') --> {2}", input, fmt, ts3)
            Else
                Console.WriteLine("Cannot parse {0} with '{1}'.",
                                  input, fmt)
            End If
        Next
        ' The example displays the following output:
        '       Formatting:
        '       00:00:03.6974970 ('FFFFF') --> 69749
        '       00:00:03.8000090 ('ss\.FFFFF') --> 03.8
        '       
        '       Parsing:
        '       0:0:03. ('h\:m\:ss\.FFFF') --> 00:00:03
        '       0:0:03.12 ('h\:m\:ss\.FFFF') --> 00:00:03.1200000
        '       Cannot parse 0:0:03.127956 with 'h\:m\:ss\.FFFF'.
        ' </Snippet25>
    End Sub

    Private Sub FFFFFFSpecifier()
        ' <Snippet26>
        Console.WriteLine("Formatting:")
        Dim ts1 As TimeSpan = TimeSpan.Parse("0:0:3.6974974")
        Console.WriteLine("{0} ('FFFFFF') --> {0:FFFFFF}", ts1)

        Dim ts2 As TimeSpan = TimeSpan.Parse("0:0:3.8000009")
        Console.WriteLine("{0} ('ss\.FFFFFF') --> {0:ss\.FFFFFF}", ts2)
        Console.WriteLine()

        Console.WriteLine("Parsing:")
        Dim inputs() As String = {"0:0:03.", "0:0:03.12", "0:0:03.1279569"}
        Dim fmt As String = "h\:m\:ss\.FFFFFF"
        Dim ts3 As TimeSpan

        For Each input As String In inputs
            If TimeSpan.TryParseExact(input, fmt, Nothing, ts3)
                Console.WriteLine("{0} ('{1}') --> {2}", input, fmt, ts3)
            Else
                Console.WriteLine("Cannot parse {0} with '{1}'.",
                                  input, fmt)
            End If
        Next
        ' The example displays the following output:
        '       Formatting:
        '       00:00:03.6974974 ('FFFFFF') --> 697497
        '       00:00:03.8000009 ('ss\.FFFFFF') --> 03.8
        '       
        '       Parsing:
        '       0:0:03. ('h\:m\:ss\.FFFFFF') --> 00:00:03
        '       0:0:03.12 ('h\:m\:ss\.FFFFFF') --> 00:00:03.1200000
        '       Cannot parse 0:0:03.1279569 with 'h\:m\:ss\.FFFFFF'.
        ' </Snippet26>
    End Sub

    Private Sub F7Specifier()
        ' <Snippet27>
        Console.WriteLine("Formatting:")
        Dim ts1 As TimeSpan = TimeSpan.Parse("0:0:3.6974974")
        Console.WriteLine("{0} ('FFFFFFF') --> {0:FFFFFFF}", ts1)

        Dim ts2 As TimeSpan = TimeSpan.Parse("0:0:3.9500000")
        Console.WriteLine("{0} ('ss\.FFFFFFF') --> {0:ss\.FFFFFFF}", ts2)
        Console.WriteLine()

        Console.WriteLine("Parsing:")
        Dim inputs() As String = {"0:0:03.", "0:0:03.12", "0:0:03.1279569"}
        Dim fmt As String = "h\:m\:ss\.FFFFFFF"
        Dim ts3 As TimeSpan

        For Each input As String In inputs
            If TimeSpan.TryParseExact(input, fmt, Nothing, ts3)
                Console.WriteLine("{0} ('{1}') --> {2}", input, fmt, ts3)
            Else
                Console.WriteLine("Cannot parse {0} with '{1}'.",
                                  input, fmt)
            End If
        Next
        ' The example displays the following output:
        '    Formatting:
        '    00:00:03.6974974 ('FFFFFFF') --> 6974974
        '    00:00:03.9500000 ('ss\.FFFFFFF') --> 03.95
        '    
        '    Parsing:
        '    0:0:03. ('h\:m\:ss\.FFFFFFF') --> 00:00:03
        '    0:0:03.12 ('h\:m\:ss\.FFFFFFF') --> 00:00:03.1200000
        '    0:0:03.1279569 ('h\:m\:ss\.FFFFFFF') --> 00:00:03.1279569     
        ' </Snippet27>
    End Sub
End Module

Back to table

The "FFFFFF" custom format specifier

The "FFFFFF" custom format specifier (with six F characters) outputs the millionths of a second in a time interval. In a formatting operation, any remaining fractional digits are truncated. If there are any trailing fractional zeros, they aren't included in the result string. In a parsing operation that calls the System.TimeSpan.ParseExact or System.TimeSpan.TryParseExact method, the presence of the tenths, hundredths, thousandths, ten-thousandths, hundred-thousandths, and millionths of a second digit is optional.

The following example uses the "FFFFFF" custom format specifier to display the millionths of a second in a System.TimeSpan value. It also uses this custom format specifier in a parsing operation.

sample · csharpsamples/snippets/csharp/VS_Snippets_CLR/conceptual.timespan.custom/cs/f_specifiers1.cs26

csharp
using System;

public class Example
{
   public static void Main()
   {
      FSpecifier();
      Console.WriteLine("-----");
      FFSpecifier();
      Console.WriteLine("-----");
      FFFSpecifier();
      Console.WriteLine("-----");
      FFFFSpecifier();
      Console.WriteLine("-----");
      FFFFFSpecifier();
      Console.WriteLine("-----");
      FFFFFFSpecifier();
      Console.WriteLine("-----");
      F7Specifier();
   }

   private static void FSpecifier()
   {
      // <Snippet21>
      Console.WriteLine("Formatting:");
      TimeSpan ts1 = TimeSpan.Parse("0:0:3.669");
      Console.WriteLine($"{ts1} ('%F') --> {ts1:%F}");

      TimeSpan ts2 = TimeSpan.Parse("0:0:3.091");
      Console.WriteLine($"{ts2} ('ss\\.F') --> {ts2:ss\\.F}");
      Console.WriteLine();

      Console.WriteLine("Parsing:");
      string[] inputs = { "0:0:03.", "0:0:03.1", "0:0:03.12" };
      string fmt = @"h\:m\:ss\.F";
      TimeSpan ts3;

      foreach (string input in inputs) {
         if (TimeSpan.TryParseExact(input, fmt, null, out ts3))
            Console.WriteLine($"{input} ('{fmt}') --> {ts3}");
         else
            Console.WriteLine($"Cannot parse {input} with '{fmt}'.");
      }
      // The example displays the following output:
      //       Formatting:
      //       00:00:03.6690000 ('%F') --> 6
      //       00:00:03.0910000 ('ss\.F') --> 03.
      //
      //       Parsing:
      //       0:0:03. ('h\:m\:ss\.F') --> 00:00:03
      //       0:0:03.1 ('h\:m\:ss\.F') --> 00:00:03.1000000
      //       Cannot parse 0:0:03.12 with 'h\:m\:ss\.F'.
      // </Snippet21>
   }

   private static void FFSpecifier()
   {
      // <Snippet22>
      Console.WriteLine("Formatting:");
      TimeSpan ts1 = TimeSpan.Parse("0:0:3.697");
      Console.WriteLine($"{ts1} ('FF') --> {ts1:FF}");

      TimeSpan ts2 = TimeSpan.Parse("0:0:3.809");
      Console.WriteLine($"{ts2} ('ss\\.FF') --> {ts2:ss\\.FF}");
      Console.WriteLine();

      Console.WriteLine("Parsing:");
      string[] inputs = { "0:0:03.", "0:0:03.1", "0:0:03.127" };
      string fmt = @"h\:m\:ss\.FF";
      TimeSpan ts3;

      foreach (string input in inputs) {
         if (TimeSpan.TryParseExact(input, fmt, null, out ts3))
            Console.WriteLine($"{input} ('{fmt}') --> {ts3}");
         else
            Console.WriteLine($"Cannot parse {input} with '{fmt}'.");
      }
      // The example displays the following output:
      //       Formatting:
      //       00:00:03.6970000 ('FF') --> 69
      //       00:00:03.8090000 ('ss\.FF') --> 03.8
      //
      //       Parsing:
      //       0:0:03. ('h\:m\:ss\.FF') --> 00:00:03
      //       0:0:03.1 ('h\:m\:ss\.FF') --> 00:00:03.1000000
      //       Cannot parse 0:0:03.127 with 'h\:m\:ss\.FF'.
      // </Snippet22>
   }

   private static void FFFSpecifier()
   {
      // <Snippet23>
      Console.WriteLine("Formatting:");
      TimeSpan ts1 = TimeSpan.Parse("0:0:3.6974");
      Console.WriteLine($"{ts1} ('FFF') --> {ts1:FFF}");

      TimeSpan ts2 = TimeSpan.Parse("0:0:3.8009");
      Console.WriteLine($"{ts2} ('ss\\.FFF') --> {ts2:ss\\.FFF}");
      Console.WriteLine();

      Console.WriteLine("Parsing:");
      string[] inputs = { "0:0:03.", "0:0:03.12", "0:0:03.1279" };
      string fmt = @"h\:m\:ss\.FFF";
      TimeSpan ts3;

      foreach (string input in inputs) {
         if (TimeSpan.TryParseExact(input, fmt, null, out ts3))
            Console.WriteLine($"{input} ('{fmt}') --> {ts3}");
         else
            Console.WriteLine($"Cannot parse {input} with '{fmt}'.");
      }
      // The example displays the following output:
      //       Formatting:
      //       00:00:03.6974000 ('FFF') --> 697
      //       00:00:03.8009000 ('ss\.FFF') --> 03.8
      //
      //       Parsing:
      //       0:0:03. ('h\:m\:ss\.FFF') --> 00:00:03
      //       0:0:03.12 ('h\:m\:ss\.FFF') --> 00:00:03.1200000
      //       Cannot parse 0:0:03.1279 with 'h\:m\:ss\.FFF'.
      // </Snippet23>
   }

   private static void FFFFSpecifier()
   {
      // <Snippet24>
      Console.WriteLine("Formatting:");
      TimeSpan ts1 = TimeSpan.Parse("0:0:3.69749");
      Console.WriteLine($"{ts1} ('FFFF') --> {ts1:FFFF}");

      TimeSpan ts2 = TimeSpan.Parse("0:0:3.80009");
      Console.WriteLine($"{ts2} ('ss\\.FFFF') --> {ts2:ss\\.FFFF}");
      Console.WriteLine();

      Console.WriteLine("Parsing:");
      string[] inputs = { "0:0:03.", "0:0:03.12", "0:0:03.12795" };
      string fmt = @"h\:m\:ss\.FFFF";
      TimeSpan ts3;

      foreach (string input in inputs) {
         if (TimeSpan.TryParseExact(input, fmt, null, out ts3))
            Console.WriteLine($"{input} ('{fmt}') --> {ts3}");
         else
            Console.WriteLine($"Cannot parse {input} with '{fmt}'.");
      }
      // The example displays the following output:
      //       Formatting:
      //       00:00:03.6974900 ('FFFF') --> 6974
      //       00:00:03.8000900 ('ss\.FFFF') --> 03.8
      //
      //       Parsing:
      //       0:0:03. ('h\:m\:ss\.FFFF') --> 00:00:03
      //       0:0:03.12 ('h\:m\:ss\.FFFF') --> 00:00:03.1200000
      //       Cannot parse 0:0:03.12795 with 'h\:m\:ss\.FFFF'.
      // </Snippet24>
   }

   private static void FFFFFSpecifier()
   {
      // <Snippet25>
      Console.WriteLine("Formatting:");
      TimeSpan ts1 = TimeSpan.Parse("0:0:3.697497");
      Console.WriteLine($"{ts1} ('FFFFF') --> {ts1:FFFFF}");

      TimeSpan ts2 = TimeSpan.Parse("0:0:3.800009");
      Console.WriteLine($"{ts2} ('ss\\.FFFFF') --> {ts2:ss\\.FFFFF}");
      Console.WriteLine();

      Console.WriteLine("Parsing:");
      string[] inputs = { "0:0:03.", "0:0:03.12", "0:0:03.127956" };
      string fmt = @"h\:m\:ss\.FFFFF";
      TimeSpan ts3;

      foreach (string input in inputs) {
         if (TimeSpan.TryParseExact(input, fmt, null, out ts3))
            Console.WriteLine($"{input} ('{fmt}') --> {ts3}");
         else
            Console.WriteLine($"Cannot parse {input} with '{fmt}'.");
      }
      // The example displays the following output:
      //       Formatting:
      //       00:00:03.6974970 ('FFFFF') --> 69749
      //       00:00:03.8000090 ('ss\.FFFFF') --> 03.8
      //
      //       Parsing:
      //       0:0:03. ('h\:m\:ss\.FFFF') --> 00:00:03
      //       0:0:03.12 ('h\:m\:ss\.FFFF') --> 00:00:03.1200000
      //       Cannot parse 0:0:03.127956 with 'h\:m\:ss\.FFFF'.
      // </Snippet25>
   }

   private static void FFFFFFSpecifier()
   {
      // <Snippet26>
      Console.WriteLine("Formatting:");
      TimeSpan ts1 = TimeSpan.Parse("0:0:3.6974974");
      Console.WriteLine($"{ts1} ('FFFFFF') --> {ts1:FFFFFF}");

      TimeSpan ts2 = TimeSpan.Parse("0:0:3.8000009");
      Console.WriteLine($"{ts2} ('ss\\.FFFFFF') --> {ts2:ss\\.FFFFFF}");
      Console.WriteLine();

      Console.WriteLine("Parsing:");
      string[] inputs = { "0:0:03.", "0:0:03.12", "0:0:03.1279569" };
      string fmt = @"h\:m\:ss\.FFFFFF";
      TimeSpan ts3;

      foreach (string input in inputs) {
         if (TimeSpan.TryParseExact(input, fmt, null, out ts3))
            Console.WriteLine($"{input} ('{fmt}') --> {ts3}");
         else
            Console.WriteLine($"Cannot parse {input} with '{fmt}'.");
      }
      // The example displays the following output:
      //       Formatting:
      //       00:00:03.6974974 ('FFFFFF') --> 697497
      //       00:00:03.8000009 ('ss\.FFFFFF') --> 03.8
      //
      //       Parsing:
      //       0:0:03. ('h\:m\:ss\.FFFFFF') --> 00:00:03
      //       0:0:03.12 ('h\:m\:ss\.FFFFFF') --> 00:00:03.1200000
      //       Cannot parse 0:0:03.1279569 with 'h\:m\:ss\.FFFFFF'.
      // </Snippet26>
   }

   private static void F7Specifier()
   {
      // <Snippet27>
      Console.WriteLine("Formatting:");
      TimeSpan ts1 = TimeSpan.Parse("0:0:3.6974974");
      Console.WriteLine($"{ts1} ('FFFFFFF') --> {ts1:FFFFFFF}");

      TimeSpan ts2 = TimeSpan.Parse("0:0:3.9500000");
      Console.WriteLine($"{ts2} ('ss\\.FFFFFFF') --> {ts2:ss\\.FFFFFFF}");
      Console.WriteLine();

      Console.WriteLine("Parsing:");
      string[] inputs = { "0:0:03.", "0:0:03.12", "0:0:03.1279569" };
      string fmt = @"h\:m\:ss\.FFFFFFF";
      TimeSpan ts3;

      foreach (string input in inputs) {
         if (TimeSpan.TryParseExact(input, fmt, null, out ts3))
            Console.WriteLine($"{input} ('{fmt}') --> {ts3}");
         else
            Console.WriteLine($"Cannot parse {input} with '{fmt}'.");
      }
      // The example displays the following output:
      //    Formatting:
      //    00:00:03.6974974 ('FFFFFFF') --> 6974974
      //    00:00:03.9500000 ('ss\.FFFFFFF') --> 03.95
      //
      //    Parsing:
      //    0:0:03. ('h\:m\:ss\.FFFFFFF') --> 00:00:03
      //    0:0:03.12 ('h\:m\:ss\.FFFFFFF') --> 00:00:03.1200000
      //    0:0:03.1279569 ('h\:m\:ss\.FFFFFFF') --> 00:00:03.1279569
      // </Snippet27>
   }
}

sample · vbsamples/snippets/visualbasic/VS_Snippets_CLR/conceptual.timespan.custom/vb/f_specifiers1.vb26

vb
' Visual Basic .NET Document
Option Strict On

Module Example
    Public Sub Main()
        FSpecifier()
        Console.WriteLine("-----")
        FFSpecifier()
        Console.WriteLine("-----")
        FFFSpecifier()
        Console.WriteLine("-----")
        FFFFSpecifier()
        Console.WriteLine("-----")
        FFFFFSpecifier()
        Console.WriteLine("-----")
        FFFFFFSpecifier()
        Console.WriteLine("-----")
        F7Specifier()
    End Sub

    Private Sub FSpecifier()
        ' <Snippet21>
        Console.WriteLine("Formatting:")
        Dim ts1 As TimeSpan = TimeSpan.Parse("0:0:3.669")
        Console.WriteLine("{0} ('%F') --> {0:%F}", ts1)

        Dim ts2 As TimeSpan = TimeSpan.Parse("0:0:3.091")
        Console.WriteLine("{0} ('ss\.F') --> {0:ss\.F}", ts2)
        Console.WriteLine()

        Console.WriteLine("Parsing:")
        Dim inputs() As String = {"0:0:03.", "0:0:03.1", "0:0:03.12"}
        Dim fmt As String = "h\:m\:ss\.F"
        Dim ts3 As TimeSpan

        For Each input As String In inputs
            If TimeSpan.TryParseExact(input, fmt, Nothing, ts3)
                Console.WriteLine("{0} ('{1}') --> {2}", input, fmt, ts3)
            Else
                Console.WriteLine("Cannot parse {0} with '{1}'.",
                                  input, fmt)
            End If
        Next
        ' The example displays the following output:
        '       Formatting:
        '       00:00:03.6690000 ('%F') --> 6
        '       00:00:03.0910000 ('ss\.F') --> 03.
        '       
        '       Parsing:
        '       0:0:03. ('h\:m\:ss\.F') --> 00:00:03
        '       0:0:03.1 ('h\:m\:ss\.F') --> 00:00:03.1000000
        '       Cannot parse 0:0:03.12 with 'h\:m\:ss\.F'.      
        ' </Snippet21>
    End Sub

    Private Sub FFSpecifier()
        ' <Snippet22>
        Console.WriteLine("Formatting:")
        Dim ts1 As TimeSpan = TimeSpan.Parse("0:0:3.697")
        Console.WriteLine("{0} ('FF') --> {0:FF}", ts1)

        Dim ts2 As TimeSpan = TimeSpan.Parse("0:0:3.809")
        Console.WriteLine("{0} ('ss\.FF') --> {0:ss\.FF}", ts2)
        Console.WriteLine()

        Console.WriteLine("Parsing:")
        Dim inputs() As String = {"0:0:03.", "0:0:03.1", "0:0:03.127"}
        Dim fmt As String = "h\:m\:ss\.FF"
        Dim ts3 As TimeSpan

        For Each input As String In inputs
            If TimeSpan.TryParseExact(input, fmt, Nothing, ts3)
                Console.WriteLine("{0} ('{1}') --> {2}", input, fmt, ts3)
            Else
                Console.WriteLine("Cannot parse {0} with '{1}'.",
                                  input, fmt)
            End If
        Next
        ' The example displays the following output:
        '       Formatting:
        '       00:00:03.6970000 ('FF') --> 69
        '       00:00:03.8090000 ('ss\.FF') --> 03.8
        '       
        '       Parsing:
        '       0:0:03. ('h\:m\:ss\.FF') --> 00:00:03
        '       0:0:03.1 ('h\:m\:ss\.FF') --> 00:00:03.1000000
        '       Cannot parse 0:0:03.127 with 'h\:m\:ss\.FF'.
        ' </Snippet22>
    End Sub

    Private Sub FFFSpecifier()
        ' <Snippet23>
        Console.WriteLine("Formatting:")
        Dim ts1 As TimeSpan = TimeSpan.Parse("0:0:3.6974")
        Console.WriteLine("{0} ('FFF') --> {0:FFF}", ts1)

        Dim ts2 As TimeSpan = TimeSpan.Parse("0:0:3.8009")
        Console.WriteLine("{0} ('ss\.FFF') --> {0:ss\.FFF}", ts2)
        Console.WriteLine()

        Console.WriteLine("Parsing:")
        Dim inputs() As String = {"0:0:03.", "0:0:03.12", "0:0:03.1279"}
        Dim fmt As String = "h\:m\:ss\.FFF"
        Dim ts3 As TimeSpan

        For Each input As String In inputs
            If TimeSpan.TryParseExact(input, fmt, Nothing, ts3)
                Console.WriteLine("{0} ('{1}') --> {2}", input, fmt, ts3)
            Else
                Console.WriteLine("Cannot parse {0} with '{1}'.",
                                  input, fmt)
            End If
        Next
        ' The example displays the following output:
        '       Formatting:
        '       00:00:03.6974000 ('FFF') --> 697
        '       00:00:03.8009000 ('ss\.FFF') --> 03.8
        '       
        '       Parsing:
        '       0:0:03. ('h\:m\:ss\.FFF') --> 00:00:03
        '       0:0:03.12 ('h\:m\:ss\.FFF') --> 00:00:03.1200000
        '       Cannot parse 0:0:03.1279 with 'h\:m\:ss\.FFF'.
        ' </Snippet23>
    End Sub

    Private Sub FFFFSpecifier()
        ' <Snippet24>
        Console.WriteLine("Formatting:")
        Dim ts1 As TimeSpan = TimeSpan.Parse("0:0:3.69749")
        Console.WriteLine("{0} ('FFFF') --> {0:FFFF}", ts1)

        Dim ts2 As TimeSpan = TimeSpan.Parse("0:0:3.80009")
        Console.WriteLine("{0} ('ss\.FFFF') --> {0:ss\.FFFF}", ts2)
        Console.WriteLine()

        Console.WriteLine("Parsing:")
        Dim inputs() As String = {"0:0:03.", "0:0:03.12", "0:0:03.12795"}
        Dim fmt As String = "h\:m\:ss\.FFFF"
        Dim ts3 As TimeSpan

        For Each input As String In inputs
            If TimeSpan.TryParseExact(input, fmt, Nothing, ts3)
                Console.WriteLine("{0} ('{1}') --> {2}", input, fmt, ts3)
            Else
                Console.WriteLine("Cannot parse {0} with '{1}'.",
                                  input, fmt)
            End If
        Next
        ' The example displays the following output:
        '       Formatting:
        '       00:00:03.6974900 ('FFFF') --> 6974
        '       00:00:03.8000900 ('ss\.FFFF') --> 03.8
        '       
        '       Parsing:
        '       0:0:03. ('h\:m\:ss\.FFFF') --> 00:00:03
        '       0:0:03.12 ('h\:m\:ss\.FFFF') --> 00:00:03.1200000
        '       Cannot parse 0:0:03.12795 with 'h\:m\:ss\.FFFF'.
        ' </Snippet24>
    End Sub

    Private Sub FFFFFSpecifier()
        ' <Snippet25>
        Console.WriteLine("Formatting:")
        Dim ts1 As TimeSpan = TimeSpan.Parse("0:0:3.697497")
        Console.WriteLine("{0} ('FFFFF') --> {0:FFFFF}", ts1)

        Dim ts2 As TimeSpan = TimeSpan.Parse("0:0:3.800009")
        Console.WriteLine("{0} ('ss\.FFFFF') --> {0:ss\.FFFFF}", ts2)
        Console.WriteLine()

        Console.WriteLine("Parsing:")
        Dim inputs() As String = {"0:0:03.", "0:0:03.12", "0:0:03.127956"}
        Dim fmt As String = "h\:m\:ss\.FFFFF"
        Dim ts3 As TimeSpan

        For Each input As String In inputs
            If TimeSpan.TryParseExact(input, fmt, Nothing, ts3)
                Console.WriteLine("{0} ('{1}') --> {2}", input, fmt, ts3)
            Else
                Console.WriteLine("Cannot parse {0} with '{1}'.",
                                  input, fmt)
            End If
        Next
        ' The example displays the following output:
        '       Formatting:
        '       00:00:03.6974970 ('FFFFF') --> 69749
        '       00:00:03.8000090 ('ss\.FFFFF') --> 03.8
        '       
        '       Parsing:
        '       0:0:03. ('h\:m\:ss\.FFFF') --> 00:00:03
        '       0:0:03.12 ('h\:m\:ss\.FFFF') --> 00:00:03.1200000
        '       Cannot parse 0:0:03.127956 with 'h\:m\:ss\.FFFF'.
        ' </Snippet25>
    End Sub

    Private Sub FFFFFFSpecifier()
        ' <Snippet26>
        Console.WriteLine("Formatting:")
        Dim ts1 As TimeSpan = TimeSpan.Parse("0:0:3.6974974")
        Console.WriteLine("{0} ('FFFFFF') --> {0:FFFFFF}", ts1)

        Dim ts2 As TimeSpan = TimeSpan.Parse("0:0:3.8000009")
        Console.WriteLine("{0} ('ss\.FFFFFF') --> {0:ss\.FFFFFF}", ts2)
        Console.WriteLine()

        Console.WriteLine("Parsing:")
        Dim inputs() As String = {"0:0:03.", "0:0:03.12", "0:0:03.1279569"}
        Dim fmt As String = "h\:m\:ss\.FFFFFF"
        Dim ts3 As TimeSpan

        For Each input As String In inputs
            If TimeSpan.TryParseExact(input, fmt, Nothing, ts3)
                Console.WriteLine("{0} ('{1}') --> {2}", input, fmt, ts3)
            Else
                Console.WriteLine("Cannot parse {0} with '{1}'.",
                                  input, fmt)
            End If
        Next
        ' The example displays the following output:
        '       Formatting:
        '       00:00:03.6974974 ('FFFFFF') --> 697497
        '       00:00:03.8000009 ('ss\.FFFFFF') --> 03.8
        '       
        '       Parsing:
        '       0:0:03. ('h\:m\:ss\.FFFFFF') --> 00:00:03
        '       0:0:03.12 ('h\:m\:ss\.FFFFFF') --> 00:00:03.1200000
        '       Cannot parse 0:0:03.1279569 with 'h\:m\:ss\.FFFFFF'.
        ' </Snippet26>
    End Sub

    Private Sub F7Specifier()
        ' <Snippet27>
        Console.WriteLine("Formatting:")
        Dim ts1 As TimeSpan = TimeSpan.Parse("0:0:3.6974974")
        Console.WriteLine("{0} ('FFFFFFF') --> {0:FFFFFFF}", ts1)

        Dim ts2 As TimeSpan = TimeSpan.Parse("0:0:3.9500000")
        Console.WriteLine("{0} ('ss\.FFFFFFF') --> {0:ss\.FFFFFFF}", ts2)
        Console.WriteLine()

        Console.WriteLine("Parsing:")
        Dim inputs() As String = {"0:0:03.", "0:0:03.12", "0:0:03.1279569"}
        Dim fmt As String = "h\:m\:ss\.FFFFFFF"
        Dim ts3 As TimeSpan

        For Each input As String In inputs
            If TimeSpan.TryParseExact(input, fmt, Nothing, ts3)
                Console.WriteLine("{0} ('{1}') --> {2}", input, fmt, ts3)
            Else
                Console.WriteLine("Cannot parse {0} with '{1}'.",
                                  input, fmt)
            End If
        Next
        ' The example displays the following output:
        '    Formatting:
        '    00:00:03.6974974 ('FFFFFFF') --> 6974974
        '    00:00:03.9500000 ('ss\.FFFFFFF') --> 03.95
        '    
        '    Parsing:
        '    0:0:03. ('h\:m\:ss\.FFFFFFF') --> 00:00:03
        '    0:0:03.12 ('h\:m\:ss\.FFFFFFF') --> 00:00:03.1200000
        '    0:0:03.1279569 ('h\:m\:ss\.FFFFFFF') --> 00:00:03.1279569     
        ' </Snippet27>
    End Sub
End Module

Back to table

The "FFFFFFF" custom format specifier

The "FFFFFFF" custom format specifier (with seven F characters) outputs the ten-millionths of a second (or the fractional number of ticks) in a time interval. If there are any trailing fractional zeros, they aren't included in the result string. In a parsing operation that calls the System.TimeSpan.ParseExact or System.TimeSpan.TryParseExact method, the presence of the seven fractional digits in the input string is optional.

The following example uses the "FFFFFFF" custom format specifier to display the fractional parts of a second in a System.TimeSpan value. It also uses this custom format specifier in a parsing operation.

sample · csharpsamples/snippets/csharp/VS_Snippets_CLR/conceptual.timespan.custom/cs/f_specifiers1.cs27

csharp
using System;

public class Example
{
   public static void Main()
   {
      FSpecifier();
      Console.WriteLine("-----");
      FFSpecifier();
      Console.WriteLine("-----");
      FFFSpecifier();
      Console.WriteLine("-----");
      FFFFSpecifier();
      Console.WriteLine("-----");
      FFFFFSpecifier();
      Console.WriteLine("-----");
      FFFFFFSpecifier();
      Console.WriteLine("-----");
      F7Specifier();
   }

   private static void FSpecifier()
   {
      // <Snippet21>
      Console.WriteLine("Formatting:");
      TimeSpan ts1 = TimeSpan.Parse("0:0:3.669");
      Console.WriteLine($"{ts1} ('%F') --> {ts1:%F}");

      TimeSpan ts2 = TimeSpan.Parse("0:0:3.091");
      Console.WriteLine($"{ts2} ('ss\\.F') --> {ts2:ss\\.F}");
      Console.WriteLine();

      Console.WriteLine("Parsing:");
      string[] inputs = { "0:0:03.", "0:0:03.1", "0:0:03.12" };
      string fmt = @"h\:m\:ss\.F";
      TimeSpan ts3;

      foreach (string input in inputs) {
         if (TimeSpan.TryParseExact(input, fmt, null, out ts3))
            Console.WriteLine($"{input} ('{fmt}') --> {ts3}");
         else
            Console.WriteLine($"Cannot parse {input} with '{fmt}'.");
      }
      // The example displays the following output:
      //       Formatting:
      //       00:00:03.6690000 ('%F') --> 6
      //       00:00:03.0910000 ('ss\.F') --> 03.
      //
      //       Parsing:
      //       0:0:03. ('h\:m\:ss\.F') --> 00:00:03
      //       0:0:03.1 ('h\:m\:ss\.F') --> 00:00:03.1000000
      //       Cannot parse 0:0:03.12 with 'h\:m\:ss\.F'.
      // </Snippet21>
   }

   private static void FFSpecifier()
   {
      // <Snippet22>
      Console.WriteLine("Formatting:");
      TimeSpan ts1 = TimeSpan.Parse("0:0:3.697");
      Console.WriteLine($"{ts1} ('FF') --> {ts1:FF}");

      TimeSpan ts2 = TimeSpan.Parse("0:0:3.809");
      Console.WriteLine($"{ts2} ('ss\\.FF') --> {ts2:ss\\.FF}");
      Console.WriteLine();

      Console.WriteLine("Parsing:");
      string[] inputs = { "0:0:03.", "0:0:03.1", "0:0:03.127" };
      string fmt = @"h\:m\:ss\.FF";
      TimeSpan ts3;

      foreach (string input in inputs) {
         if (TimeSpan.TryParseExact(input, fmt, null, out ts3))
            Console.WriteLine($"{input} ('{fmt}') --> {ts3}");
         else
            Console.WriteLine($"Cannot parse {input} with '{fmt}'.");
      }
      // The example displays the following output:
      //       Formatting:
      //       00:00:03.6970000 ('FF') --> 69
      //       00:00:03.8090000 ('ss\.FF') --> 03.8
      //
      //       Parsing:
      //       0:0:03. ('h\:m\:ss\.FF') --> 00:00:03
      //       0:0:03.1 ('h\:m\:ss\.FF') --> 00:00:03.1000000
      //       Cannot parse 0:0:03.127 with 'h\:m\:ss\.FF'.
      // </Snippet22>
   }

   private static void FFFSpecifier()
   {
      // <Snippet23>
      Console.WriteLine("Formatting:");
      TimeSpan ts1 = TimeSpan.Parse("0:0:3.6974");
      Console.WriteLine($"{ts1} ('FFF') --> {ts1:FFF}");

      TimeSpan ts2 = TimeSpan.Parse("0:0:3.8009");
      Console.WriteLine($"{ts2} ('ss\\.FFF') --> {ts2:ss\\.FFF}");
      Console.WriteLine();

      Console.WriteLine("Parsing:");
      string[] inputs = { "0:0:03.", "0:0:03.12", "0:0:03.1279" };
      string fmt = @"h\:m\:ss\.FFF";
      TimeSpan ts3;

      foreach (string input in inputs) {
         if (TimeSpan.TryParseExact(input, fmt, null, out ts3))
            Console.WriteLine($"{input} ('{fmt}') --> {ts3}");
         else
            Console.WriteLine($"Cannot parse {input} with '{fmt}'.");
      }
      // The example displays the following output:
      //       Formatting:
      //       00:00:03.6974000 ('FFF') --> 697
      //       00:00:03.8009000 ('ss\.FFF') --> 03.8
      //
      //       Parsing:
      //       0:0:03. ('h\:m\:ss\.FFF') --> 00:00:03
      //       0:0:03.12 ('h\:m\:ss\.FFF') --> 00:00:03.1200000
      //       Cannot parse 0:0:03.1279 with 'h\:m\:ss\.FFF'.
      // </Snippet23>
   }

   private static void FFFFSpecifier()
   {
      // <Snippet24>
      Console.WriteLine("Formatting:");
      TimeSpan ts1 = TimeSpan.Parse("0:0:3.69749");
      Console.WriteLine($"{ts1} ('FFFF') --> {ts1:FFFF}");

      TimeSpan ts2 = TimeSpan.Parse("0:0:3.80009");
      Console.WriteLine($"{ts2} ('ss\\.FFFF') --> {ts2:ss\\.FFFF}");
      Console.WriteLine();

      Console.WriteLine("Parsing:");
      string[] inputs = { "0:0:03.", "0:0:03.12", "0:0:03.12795" };
      string fmt = @"h\:m\:ss\.FFFF";
      TimeSpan ts3;

      foreach (string input in inputs) {
         if (TimeSpan.TryParseExact(input, fmt, null, out ts3))
            Console.WriteLine($"{input} ('{fmt}') --> {ts3}");
         else
            Console.WriteLine($"Cannot parse {input} with '{fmt}'.");
      }
      // The example displays the following output:
      //       Formatting:
      //       00:00:03.6974900 ('FFFF') --> 6974
      //       00:00:03.8000900 ('ss\.FFFF') --> 03.8
      //
      //       Parsing:
      //       0:0:03. ('h\:m\:ss\.FFFF') --> 00:00:03
      //       0:0:03.12 ('h\:m\:ss\.FFFF') --> 00:00:03.1200000
      //       Cannot parse 0:0:03.12795 with 'h\:m\:ss\.FFFF'.
      // </Snippet24>
   }

   private static void FFFFFSpecifier()
   {
      // <Snippet25>
      Console.WriteLine("Formatting:");
      TimeSpan ts1 = TimeSpan.Parse("0:0:3.697497");
      Console.WriteLine($"{ts1} ('FFFFF') --> {ts1:FFFFF}");

      TimeSpan ts2 = TimeSpan.Parse("0:0:3.800009");
      Console.WriteLine($"{ts2} ('ss\\.FFFFF') --> {ts2:ss\\.FFFFF}");
      Console.WriteLine();

      Console.WriteLine("Parsing:");
      string[] inputs = { "0:0:03.", "0:0:03.12", "0:0:03.127956" };
      string fmt = @"h\:m\:ss\.FFFFF";
      TimeSpan ts3;

      foreach (string input in inputs) {
         if (TimeSpan.TryParseExact(input, fmt, null, out ts3))
            Console.WriteLine($"{input} ('{fmt}') --> {ts3}");
         else
            Console.WriteLine($"Cannot parse {input} with '{fmt}'.");
      }
      // The example displays the following output:
      //       Formatting:
      //       00:00:03.6974970 ('FFFFF') --> 69749
      //       00:00:03.8000090 ('ss\.FFFFF') --> 03.8
      //
      //       Parsing:
      //       0:0:03. ('h\:m\:ss\.FFFF') --> 00:00:03
      //       0:0:03.12 ('h\:m\:ss\.FFFF') --> 00:00:03.1200000
      //       Cannot parse 0:0:03.127956 with 'h\:m\:ss\.FFFF'.
      // </Snippet25>
   }

   private static void FFFFFFSpecifier()
   {
      // <Snippet26>
      Console.WriteLine("Formatting:");
      TimeSpan ts1 = TimeSpan.Parse("0:0:3.6974974");
      Console.WriteLine($"{ts1} ('FFFFFF') --> {ts1:FFFFFF}");

      TimeSpan ts2 = TimeSpan.Parse("0:0:3.8000009");
      Console.WriteLine($"{ts2} ('ss\\.FFFFFF') --> {ts2:ss\\.FFFFFF}");
      Console.WriteLine();

      Console.WriteLine("Parsing:");
      string[] inputs = { "0:0:03.", "0:0:03.12", "0:0:03.1279569" };
      string fmt = @"h\:m\:ss\.FFFFFF";
      TimeSpan ts3;

      foreach (string input in inputs) {
         if (TimeSpan.TryParseExact(input, fmt, null, out ts3))
            Console.WriteLine($"{input} ('{fmt}') --> {ts3}");
         else
            Console.WriteLine($"Cannot parse {input} with '{fmt}'.");
      }
      // The example displays the following output:
      //       Formatting:
      //       00:00:03.6974974 ('FFFFFF') --> 697497
      //       00:00:03.8000009 ('ss\.FFFFFF') --> 03.8
      //
      //       Parsing:
      //       0:0:03. ('h\:m\:ss\.FFFFFF') --> 00:00:03
      //       0:0:03.12 ('h\:m\:ss\.FFFFFF') --> 00:00:03.1200000
      //       Cannot parse 0:0:03.1279569 with 'h\:m\:ss\.FFFFFF'.
      // </Snippet26>
   }

   private static void F7Specifier()
   {
      // <Snippet27>
      Console.WriteLine("Formatting:");
      TimeSpan ts1 = TimeSpan.Parse("0:0:3.6974974");
      Console.WriteLine($"{ts1} ('FFFFFFF') --> {ts1:FFFFFFF}");

      TimeSpan ts2 = TimeSpan.Parse("0:0:3.9500000");
      Console.WriteLine($"{ts2} ('ss\\.FFFFFFF') --> {ts2:ss\\.FFFFFFF}");
      Console.WriteLine();

      Console.WriteLine("Parsing:");
      string[] inputs = { "0:0:03.", "0:0:03.12", "0:0:03.1279569" };
      string fmt = @"h\:m\:ss\.FFFFFFF";
      TimeSpan ts3;

      foreach (string input in inputs) {
         if (TimeSpan.TryParseExact(input, fmt, null, out ts3))
            Console.WriteLine($"{input} ('{fmt}') --> {ts3}");
         else
            Console.WriteLine($"Cannot parse {input} with '{fmt}'.");
      }
      // The example displays the following output:
      //    Formatting:
      //    00:00:03.6974974 ('FFFFFFF') --> 6974974
      //    00:00:03.9500000 ('ss\.FFFFFFF') --> 03.95
      //
      //    Parsing:
      //    0:0:03. ('h\:m\:ss\.FFFFFFF') --> 00:00:03
      //    0:0:03.12 ('h\:m\:ss\.FFFFFFF') --> 00:00:03.1200000
      //    0:0:03.1279569 ('h\:m\:ss\.FFFFFFF') --> 00:00:03.1279569
      // </Snippet27>
   }
}

sample · vbsamples/snippets/visualbasic/VS_Snippets_CLR/conceptual.timespan.custom/vb/f_specifiers1.vb27

vb
' Visual Basic .NET Document
Option Strict On

Module Example
    Public Sub Main()
        FSpecifier()
        Console.WriteLine("-----")
        FFSpecifier()
        Console.WriteLine("-----")
        FFFSpecifier()
        Console.WriteLine("-----")
        FFFFSpecifier()
        Console.WriteLine("-----")
        FFFFFSpecifier()
        Console.WriteLine("-----")
        FFFFFFSpecifier()
        Console.WriteLine("-----")
        F7Specifier()
    End Sub

    Private Sub FSpecifier()
        ' <Snippet21>
        Console.WriteLine("Formatting:")
        Dim ts1 As TimeSpan = TimeSpan.Parse("0:0:3.669")
        Console.WriteLine("{0} ('%F') --> {0:%F}", ts1)

        Dim ts2 As TimeSpan = TimeSpan.Parse("0:0:3.091")
        Console.WriteLine("{0} ('ss\.F') --> {0:ss\.F}", ts2)
        Console.WriteLine()

        Console.WriteLine("Parsing:")
        Dim inputs() As String = {"0:0:03.", "0:0:03.1", "0:0:03.12"}
        Dim fmt As String = "h\:m\:ss\.F"
        Dim ts3 As TimeSpan

        For Each input As String In inputs
            If TimeSpan.TryParseExact(input, fmt, Nothing, ts3)
                Console.WriteLine("{0} ('{1}') --> {2}", input, fmt, ts3)
            Else
                Console.WriteLine("Cannot parse {0} with '{1}'.",
                                  input, fmt)
            End If
        Next
        ' The example displays the following output:
        '       Formatting:
        '       00:00:03.6690000 ('%F') --> 6
        '       00:00:03.0910000 ('ss\.F') --> 03.
        '       
        '       Parsing:
        '       0:0:03. ('h\:m\:ss\.F') --> 00:00:03
        '       0:0:03.1 ('h\:m\:ss\.F') --> 00:00:03.1000000
        '       Cannot parse 0:0:03.12 with 'h\:m\:ss\.F'.      
        ' </Snippet21>
    End Sub

    Private Sub FFSpecifier()
        ' <Snippet22>
        Console.WriteLine("Formatting:")
        Dim ts1 As TimeSpan = TimeSpan.Parse("0:0:3.697")
        Console.WriteLine("{0} ('FF') --> {0:FF}", ts1)

        Dim ts2 As TimeSpan = TimeSpan.Parse("0:0:3.809")
        Console.WriteLine("{0} ('ss\.FF') --> {0:ss\.FF}", ts2)
        Console.WriteLine()

        Console.WriteLine("Parsing:")
        Dim inputs() As String = {"0:0:03.", "0:0:03.1", "0:0:03.127"}
        Dim fmt As String = "h\:m\:ss\.FF"
        Dim ts3 As TimeSpan

        For Each input As String In inputs
            If TimeSpan.TryParseExact(input, fmt, Nothing, ts3)
                Console.WriteLine("{0} ('{1}') --> {2}", input, fmt, ts3)
            Else
                Console.WriteLine("Cannot parse {0} with '{1}'.",
                                  input, fmt)
            End If
        Next
        ' The example displays the following output:
        '       Formatting:
        '       00:00:03.6970000 ('FF') --> 69
        '       00:00:03.8090000 ('ss\.FF') --> 03.8
        '       
        '       Parsing:
        '       0:0:03. ('h\:m\:ss\.FF') --> 00:00:03
        '       0:0:03.1 ('h\:m\:ss\.FF') --> 00:00:03.1000000
        '       Cannot parse 0:0:03.127 with 'h\:m\:ss\.FF'.
        ' </Snippet22>
    End Sub

    Private Sub FFFSpecifier()
        ' <Snippet23>
        Console.WriteLine("Formatting:")
        Dim ts1 As TimeSpan = TimeSpan.Parse("0:0:3.6974")
        Console.WriteLine("{0} ('FFF') --> {0:FFF}", ts1)

        Dim ts2 As TimeSpan = TimeSpan.Parse("0:0:3.8009")
        Console.WriteLine("{0} ('ss\.FFF') --> {0:ss\.FFF}", ts2)
        Console.WriteLine()

        Console.WriteLine("Parsing:")
        Dim inputs() As String = {"0:0:03.", "0:0:03.12", "0:0:03.1279"}
        Dim fmt As String = "h\:m\:ss\.FFF"
        Dim ts3 As TimeSpan

        For Each input As String In inputs
            If TimeSpan.TryParseExact(input, fmt, Nothing, ts3)
                Console.WriteLine("{0} ('{1}') --> {2}", input, fmt, ts3)
            Else
                Console.WriteLine("Cannot parse {0} with '{1}'.",
                                  input, fmt)
            End If
        Next
        ' The example displays the following output:
        '       Formatting:
        '       00:00:03.6974000 ('FFF') --> 697
        '       00:00:03.8009000 ('ss\.FFF') --> 03.8
        '       
        '       Parsing:
        '       0:0:03. ('h\:m\:ss\.FFF') --> 00:00:03
        '       0:0:03.12 ('h\:m\:ss\.FFF') --> 00:00:03.1200000
        '       Cannot parse 0:0:03.1279 with 'h\:m\:ss\.FFF'.
        ' </Snippet23>
    End Sub

    Private Sub FFFFSpecifier()
        ' <Snippet24>
        Console.WriteLine("Formatting:")
        Dim ts1 As TimeSpan = TimeSpan.Parse("0:0:3.69749")
        Console.WriteLine("{0} ('FFFF') --> {0:FFFF}", ts1)

        Dim ts2 As TimeSpan = TimeSpan.Parse("0:0:3.80009")
        Console.WriteLine("{0} ('ss\.FFFF') --> {0:ss\.FFFF}", ts2)
        Console.WriteLine()

        Console.WriteLine("Parsing:")
        Dim inputs() As String = {"0:0:03.", "0:0:03.12", "0:0:03.12795"}
        Dim fmt As String = "h\:m\:ss\.FFFF"
        Dim ts3 As TimeSpan

        For Each input As String In inputs
            If TimeSpan.TryParseExact(input, fmt, Nothing, ts3)
                Console.WriteLine("{0} ('{1}') --> {2}", input, fmt, ts3)
            Else
                Console.WriteLine("Cannot parse {0} with '{1}'.",
                                  input, fmt)
            End If
        Next
        ' The example displays the following output:
        '       Formatting:
        '       00:00:03.6974900 ('FFFF') --> 6974
        '       00:00:03.8000900 ('ss\.FFFF') --> 03.8
        '       
        '       Parsing:
        '       0:0:03. ('h\:m\:ss\.FFFF') --> 00:00:03
        '       0:0:03.12 ('h\:m\:ss\.FFFF') --> 00:00:03.1200000
        '       Cannot parse 0:0:03.12795 with 'h\:m\:ss\.FFFF'.
        ' </Snippet24>
    End Sub

    Private Sub FFFFFSpecifier()
        ' <Snippet25>
        Console.WriteLine("Formatting:")
        Dim ts1 As TimeSpan = TimeSpan.Parse("0:0:3.697497")
        Console.WriteLine("{0} ('FFFFF') --> {0:FFFFF}", ts1)

        Dim ts2 As TimeSpan = TimeSpan.Parse("0:0:3.800009")
        Console.WriteLine("{0} ('ss\.FFFFF') --> {0:ss\.FFFFF}", ts2)
        Console.WriteLine()

        Console.WriteLine("Parsing:")
        Dim inputs() As String = {"0:0:03.", "0:0:03.12", "0:0:03.127956"}
        Dim fmt As String = "h\:m\:ss\.FFFFF"
        Dim ts3 As TimeSpan

        For Each input As String In inputs
            If TimeSpan.TryParseExact(input, fmt, Nothing, ts3)
                Console.WriteLine("{0} ('{1}') --> {2}", input, fmt, ts3)
            Else
                Console.WriteLine("Cannot parse {0} with '{1}'.",
                                  input, fmt)
            End If
        Next
        ' The example displays the following output:
        '       Formatting:
        '       00:00:03.6974970 ('FFFFF') --> 69749
        '       00:00:03.8000090 ('ss\.FFFFF') --> 03.8
        '       
        '       Parsing:
        '       0:0:03. ('h\:m\:ss\.FFFF') --> 00:00:03
        '       0:0:03.12 ('h\:m\:ss\.FFFF') --> 00:00:03.1200000
        '       Cannot parse 0:0:03.127956 with 'h\:m\:ss\.FFFF'.
        ' </Snippet25>
    End Sub

    Private Sub FFFFFFSpecifier()
        ' <Snippet26>
        Console.WriteLine("Formatting:")
        Dim ts1 As TimeSpan = TimeSpan.Parse("0:0:3.6974974")
        Console.WriteLine("{0} ('FFFFFF') --> {0:FFFFFF}", ts1)

        Dim ts2 As TimeSpan = TimeSpan.Parse("0:0:3.8000009")
        Console.WriteLine("{0} ('ss\.FFFFFF') --> {0:ss\.FFFFFF}", ts2)
        Console.WriteLine()

        Console.WriteLine("Parsing:")
        Dim inputs() As String = {"0:0:03.", "0:0:03.12", "0:0:03.1279569"}
        Dim fmt As String = "h\:m\:ss\.FFFFFF"
        Dim ts3 As TimeSpan

        For Each input As String In inputs
            If TimeSpan.TryParseExact(input, fmt, Nothing, ts3)
                Console.WriteLine("{0} ('{1}') --> {2}", input, fmt, ts3)
            Else
                Console.WriteLine("Cannot parse {0} with '{1}'.",
                                  input, fmt)
            End If
        Next
        ' The example displays the following output:
        '       Formatting:
        '       00:00:03.6974974 ('FFFFFF') --> 697497
        '       00:00:03.8000009 ('ss\.FFFFFF') --> 03.8
        '       
        '       Parsing:
        '       0:0:03. ('h\:m\:ss\.FFFFFF') --> 00:00:03
        '       0:0:03.12 ('h\:m\:ss\.FFFFFF') --> 00:00:03.1200000
        '       Cannot parse 0:0:03.1279569 with 'h\:m\:ss\.FFFFFF'.
        ' </Snippet26>
    End Sub

    Private Sub F7Specifier()
        ' <Snippet27>
        Console.WriteLine("Formatting:")
        Dim ts1 As TimeSpan = TimeSpan.Parse("0:0:3.6974974")
        Console.WriteLine("{0} ('FFFFFFF') --> {0:FFFFFFF}", ts1)

        Dim ts2 As TimeSpan = TimeSpan.Parse("0:0:3.9500000")
        Console.WriteLine("{0} ('ss\.FFFFFFF') --> {0:ss\.FFFFFFF}", ts2)
        Console.WriteLine()

        Console.WriteLine("Parsing:")
        Dim inputs() As String = {"0:0:03.", "0:0:03.12", "0:0:03.1279569"}
        Dim fmt As String = "h\:m\:ss\.FFFFFFF"
        Dim ts3 As TimeSpan

        For Each input As String In inputs
            If TimeSpan.TryParseExact(input, fmt, Nothing, ts3)
                Console.WriteLine("{0} ('{1}') --> {2}", input, fmt, ts3)
            Else
                Console.WriteLine("Cannot parse {0} with '{1}'.",
                                  input, fmt)
            End If
        Next
        ' The example displays the following output:
        '    Formatting:
        '    00:00:03.6974974 ('FFFFFFF') --> 6974974
        '    00:00:03.9500000 ('ss\.FFFFFFF') --> 03.95
        '    
        '    Parsing:
        '    0:0:03. ('h\:m\:ss\.FFFFFFF') --> 00:00:03
        '    0:0:03.12 ('h\:m\:ss\.FFFFFFF') --> 00:00:03.1200000
        '    0:0:03.1279569 ('h\:m\:ss\.FFFFFFF') --> 00:00:03.1279569     
        ' </Snippet27>
    End Sub
End Module

Back to table

Other characters

Any other unescaped character in a format string, including a white-space character, is interpreted as a custom format specifier. In most cases, the presence of any other unescaped character results in a System.FormatException.

There are two ways to include a literal character in a format string:

  • Enclose it in single quotation marks (the literal string delimiter).

  • Precede it with a backslash ("\"), which is interpreted as an escape character. This means that, in C#, the format string must either be @-quoted, or the literal character must be preceded by an additional backslash.

    In some cases, you may have to use conditional logic to include an escaped literal in a format string. The following example uses conditional logic to include a sign symbol for negative time intervals.

    sample · csharpsamples/snippets/csharp/VS_Snippets_CLR/conceptual.timespan.custom/cs/negativevalues1.cs29

    csharp
    // <Snippet29>
    using System;
    
    public class Example
    {
       public static void Main()
       {
          TimeSpan result = new DateTime(2010, 01, 01) - DateTime.Now;
          String fmt = (result < TimeSpan.Zero ?  "\\-" : "") + "dd\\.hh\\:mm";
    
          Console.WriteLine(result.ToString(fmt));
          Console.WriteLine($"Interval: {result.ToString(fmt)}");
       }
    }
    // The example displays output like the following:
    //       -5582.12:21
    //       Interval: -5582.12:21
    // </Snippet29>

    sample · vbsamples/snippets/visualbasic/VS_Snippets_CLR/conceptual.timespan.custom/vb/negativevalues1.vb29

    vb
    ' Visual Basic .NET Document
    Option Strict On
    
    ' <Snippet29>
    Module Example
        Public Sub Main()
            Dim result As TimeSpan = New DateTime(2010, 01, 01) - Date.Now
            Dim fmt As String = If(result < TimeSpan.Zero, "\-", "") + "dd\.hh\:mm"
    
            Console.WriteLine(result.ToString(fmt))
            Console.WriteLine("Interval: {0:" + fmt + "}", result)
        End Sub
    End Module
    ' The example displays output like the following:
    '       -1291.10:54
    '       Interval: -1291.10:54
    ' </Snippet29>

.NET doesn't define a grammar for separators in time intervals. This means that the separators between days and hours, hours and minutes, minutes and seconds, and seconds and fractions of a second must all be treated as character literals in a format string.

The following example uses both the escape character and the single quote to define a custom format string that includes the word "minutes" in the output string.

sample · csharpsamples/snippets/csharp/VS_Snippets_CLR/conceptual.timespan.custom/cs/literal1.cs28

csharp
using System;

public class Example
{
   public static void Main()
   {
      // <Snippet28>
      TimeSpan interval = new TimeSpan(0, 32, 45);
      // Escape literal characters in a format string.
      string fmt = @"mm\:ss\ \m\i\n\u\t\e\s";
      Console.WriteLine(interval.ToString(fmt));
      // Delimit literal characters in a format string with the ' symbol.
      fmt = "mm':'ss' minutes'";
      Console.WriteLine(interval.ToString(fmt));
      // The example displays the following output:
      //       32:45 minutes
      //       32:45 minutes
      // </Snippet28>
   }
}

sample · vbsamples/snippets/visualbasic/VS_Snippets_CLR/conceptual.timespan.custom/vb/literal1.vb28

vb
' Visual Basic .NET Document
Option Strict On

Module Example
    Public Sub Main()
        ' <Snippet28>
        Dim interval As New TimeSpan(0, 32, 45)
        ' Escape literal characters in a format string.
        Dim fmt As String = "mm\:ss\ \m\i\n\u\t\e\s"
        Console.WriteLine(interval.ToString(fmt))
        ' Delimit literal characters in a format string with the ' symbol.
        fmt = "mm':'ss' minutes'"
        Console.WriteLine(interval.ToString(fmt))
        ' The example displays the following output: 
        '       32:45 minutes      
        '       32:45 minutes      
        ' </Snippet28>
    End Sub
End Module

Back to table

See also