Freigeben über


How to: Display the Milliseconds Component of Date and Time Values

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

The default date and time formatting methods, such as DateTime.ToString(), include the hours, minutes, and seconds of a time value but exclude its milliseconds component. This topic shows how to include a date and time's millisecond component in formatted date and time strings.

To display the millisecond component of a DateTime value

  1. If you are working with the string representation of a date, convert it to a DateTime or a DateTimeOffset value by using the static DateTime.Parse(String) or DateTimeOffset.Parse(String) method.

  2. To extract the string representation of a time's millisecond component, call the date and time value's DateTime.ToString(String) or ToString method, and pass the fff or FFF custom format pattern either alone or with other custom format specifiers as the format parameter.

Example

The example displays the millisecond component of a DateTime and a DateTimeOffset value to the console, both alone and included in a longer date and time string.

Imports System.Globalization
Imports System.Text.RegularExpressions

Module Example
   Private outputBlock As System.Windows.Controls.TextBlock
   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

      Example.outputBlock = outputBlock

      Dim dateString As String = "7/16/2008 8:32:45.126 AM"

      Try
         Dim dateValue As Date = Date.Parse(dateString)
         Dim dateOffsetValue As DateTimeOffset = DateTimeOffset.Parse(dateString)

         ' Display Millisecond component alone.
         outputBlock.Text += String.Format("Millisecond component only: {0}", _
                           dateValue.ToString("fff")) & vbCrLf
         outputBlock.Text += String.Format("Millisecond component only: {0}", _
                           dateOffsetValue.ToString("fff")) & vbCrLf

         ' Display Millisecond component with full date and time.
         outputBlock.Text += String.Format("Date and Time with Milliseconds: {0}", _
                           dateValue.ToString("MM/dd/yyyy hh:mm:ss.fff tt")) & vbCrLf
         outputBlock.Text += String.Format("Date and Time with Milliseconds: {0}", _
                           dateOffsetValue.ToString("MM/dd/yyyy hh:mm:ss.fff tt")) & vbCrLf

         ' Append millisecond pattern to current culture's full date time pattern
         Dim fullPattern As String = DateTimeFormatInfo.CurrentInfo.FullDateTimePattern
         fullPattern = Regex.Replace(fullPattern, "(:ss|:s)", "$1.fff")

         ' Display Millisecond component with modified full date and time pattern.
         outputBlock.Text += String.Format("Modified full date time pattern: {0}", _
                           dateValue.ToString(fullPattern)) & vbCrLf
         outputBlock.Text += String.Format("Modified full date time pattern: {0}", _
                           dateOffsetValue.ToString(fullPattern)) & vbCrLf
      Catch e As FormatException
         outputBlock.Text += String.Format("Unable to convert {0} to a date.", dateString) & vbCrLf
      End Try
   End Sub
End Module
' The example displays the following output if the current culture is en-US:
'    Millisecond component only: 126
'    Millisecond component only: 126
'    Date and Time with Milliseconds: 07/16/2008 08:32:45.126 AM
'    Date and Time with Milliseconds: 07/16/2008 08:32:45.126 AM
'    Modified full date time pattern: Wednesday, July 16, 2008 8:32:45.126 AM
'    Modified full date time pattern: Wednesday, July 16, 2008 8:32:45.126 AM
using System;
using System.Globalization;
using System.Text.RegularExpressions;

public class Example
{
   private static System.Windows.Controls.TextBlock outputBlock;

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {

      Example.outputBlock = outputBlock;
      string dateString = "7/16/2008 8:32:45.126 AM";

      try
      {
         DateTime dateValue = DateTime.Parse(dateString);
         DateTimeOffset dateOffsetValue = DateTimeOffset.Parse(dateString);

         // Display Millisecond component alone.
         outputBlock.Text += String.Format("Millisecond component only: {0}",
                           dateValue.ToString("fff")) + "\n";
         outputBlock.Text += String.Format("Millisecond component only: {0}",
                           dateOffsetValue.ToString("fff")) + "\n";

         // Display Millisecond component with full date and time.
         outputBlock.Text += String.Format("Date and Time with Milliseconds: {0}",
                           dateValue.ToString("MM/dd/yyyy hh:mm:ss.fff tt")) + "\n";
         outputBlock.Text += String.Format("Date and Time with Milliseconds: {0}",
                           dateOffsetValue.ToString("MM/dd/yyyy hh:mm:ss.fff tt")) + "\n";

         // Append millisecond pattern to current culture's full date time pattern
         string fullPattern = DateTimeFormatInfo.CurrentInfo.FullDateTimePattern;
         fullPattern = Regex.Replace(fullPattern, "(:ss|:s)", "$1.fff");

         // Display Millisecond component with modified full date and time pattern.
         outputBlock.Text += String.Format("Modified full date time pattern: {0}",
                           dateValue.ToString(fullPattern)) + "\n";
         outputBlock.Text += String.Format("Modified full date time pattern: {0}",
                           dateOffsetValue.ToString(fullPattern)) + "\n";
      }
      catch (FormatException)
      {
         outputBlock.Text += String.Format("Unable to convert {0} to a date.", dateString) + "\n";
      }
   }
}
// The example displays the following output if the current culture is en-US:
//    Millisecond component only: 126
//    Millisecond component only: 126
//    Date and Time with Milliseconds: 07/16/2008 08:32:45.126 AM
//    Date and Time with Milliseconds: 07/16/2008 08:32:45.126 AM
//    Modified full date time pattern: Wednesday, July 16, 2008 8:32:45.126 AM
//    Modified full date time pattern: Wednesday, July 16, 2008 8:32:45.126 AM

The fff format pattern includes any trailing zeros in the millisecond value. The FFF format pattern suppresses them. The difference is illustrated in the following example.

Dim dateValue As New Date(2008, 7, 16, 8, 32, 45, 180)
outputBlock.Text &= dateValue.ToString("fff") & vbCrLf
outputBlock.Text &= dateValue.ToString("FFF") & vbCrLf
' The example displays the following output:
'    180
'    18      
DateTime dateValue = new DateTime(2008, 7, 16, 8, 32, 45, 180);
outputBlock.Text += dateValue.ToString("fff") + "\n";
outputBlock.Text += dateValue.ToString("FFF") + "\n";
// The example displays the following output:
//    180
//    18      

A problem with defining a complete custom format specifier that includes the millisecond component of a date and time is that it defines a hard-coded format that may not correspond to the arrangement of time elements in the application's current culture. A better alternative is to retrieve one of the date and time display patterns defined by the current culture's DateTimeFormatInfo object and modify it to include milliseconds. The example also illustrates this approach. It retrieves the current culture's full date and time pattern from the DateTimeFormatInfo.FullDateTimePattern property, and then inserts the custom pattern .ffff after its seconds pattern. Note that the example uses a regular expression to perform this operation in a single method call.

You can also use a custom format specifier to display a fractional part of seconds other than milliseconds. For example, the f or F custom format specifier displays tenths of a second, the ff or FF custom format specifier displays hundredths of a second, and the ffff or FFFF custom format specifier displays ten thousandths of a second. Fractional parts of a millisecond are truncated instead of rounded in the returned string. These format specifiers are used in the following example.

Dim dateValue As New DateTime(2008, 7, 16, 8, 32, 45, 180)
outputBlock.Text += String.Format("{0} seconds", dateValue.ToString("s.f")) & vbCrLf
outputBlock.Text += String.Format("{0} seconds", dateValue.ToString("s.ff")) & vbCrLf
outputBlock.Text += String.Format("{0} seconds", dateValue.ToString("s.ffff")) & vbCrLf
' The example displays the following output:
'    45.1 seconds
'    45.18 seconds
'    45.1800 seconds
DateTime dateValue = new DateTime(2008, 7, 16, 8, 32, 45, 180);
outputBlock.Text += String.Format("{0} seconds", dateValue.ToString("s.f")) + "\n";
outputBlock.Text += String.Format("{0} seconds", dateValue.ToString("s.ff")) + "\n";
outputBlock.Text += String.Format("{0} seconds", dateValue.ToString("s.ffff")) + "\n";
// The example displays the following output:
//    45.1 seconds
//    45.18 seconds
//    45.1800 seconds
NoteNote:

It is possible to display very small fractional units of a second, such as ten thousandths of a second or hundred-thousandths of a second. However, these values may not be meaningful. The precision of date and time values depends on the resolution of the system clock. On Windows NT 3.5 and later, and Windows Vista operating systems, the clock's resolution is approximately 10-15 milliseconds.