如何:显示日期和时间值的毫秒部分

默认的日期和时间格式化方法(例如 DateTime.ToString())包括时间值的小时、分钟和秒钟,但不包括它的毫秒部分。 本主题演示如何在格式化的日期和时间字符串中包含日期和时间的毫秒部分。

显示 DateTime 值的毫秒部分

  1. 如果要使用日期的字符串表示形式,请使用静态 DateTime.Parse(String)DateTimeOffset.Parse(String) 方法将其转换为 DateTimeDateTimeOffset 值。

  2. 若要从时间中提取毫秒部分的字符串表示形式,请调用日期和时间值的 DateTime.ToString(String)ToString 方法,并以 format 参数形式传递 fff 或 FFF 自定义格式模式(单独传递或与其他自定义格式说明符一起传递)。

示例

该示例将在控制台中显示 DateTimeDateTimeOffset 值的毫秒部分(单独显示及包括在更长的日期和时间字符串中)。

Imports System.Globalization
Imports System.Text.REgularExpressions

Module MillisecondDisplay
   Public Sub Main()

      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.
         Console.WriteLine("Millisecond component only: {0}", _
                           dateValue.ToString("fff"))
         Console.WriteLine("Millisecond component only: {0}", _
                           dateOffsetValue.ToString("fff"))

         ' Display Millisecond component with full date and time.
         Console.WriteLine("Date and Time with Milliseconds: {0}", _
                           dateValue.ToString("MM/dd/yyyy hh:mm:ss.fff tt"))                        
         Console.WriteLine("Date and Time with Milliseconds: {0}", _
                           dateOffsetValue.ToString("MM/dd/yyyy hh:mm:ss.fff tt"))

         ' 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.
         Console.WriteLine("Modified full date time pattern: {0}", _
                           dateValue.ToString(fullPattern))                        
         Console.WriteLine("Modified full date time pattern: {0}", _
                           dateOffsetValue.ToString(fullPattern))
      Catch e As FormatException
         Console.WriteLine("Unable to convert {0} to a date.", dateString)      
      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 MillisecondDisplay
{
   public static void Main()
   {
      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.
         Console.WriteLine("Millisecond component only: {0}", 
                           dateValue.ToString("fff"));
         Console.WriteLine("Millisecond component only: {0}", 
                           dateOffsetValue.ToString("fff"));

         // Display Millisecond component with full date and time.
         Console.WriteLine("Date and Time with Milliseconds: {0}", 
                           dateValue.ToString("MM/dd/yyyy hh:mm:ss.fff tt"));                        
         Console.WriteLine("Date and Time with Milliseconds: {0}", 
                           dateOffsetValue.ToString("MM/dd/yyyy hh:mm:ss.fff tt"));

         // 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.
         Console.WriteLine("Modified full date time pattern: {0}", 
                           dateValue.ToString(fullPattern));
         Console.WriteLine("Modified full date time pattern: {0}",
                           dateOffsetValue.ToString(fullPattern));
      }
      catch (FormatException)
      {
         Console.WriteLine("Unable to convert {0} to a date.", dateString);
      }
   }
}
// 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

fff 格式模式包括毫秒值中的任何尾随零。 FFF 格式模式则禁止显示它们。 下面的示例中阐释了这种差异。

Dim dateValue As New Date(2008, 7, 16, 8, 32, 45, 180) 
Console.WriteLIne(dateValue.ToString("fff"))    
Console.WriteLine(dateValue.ToString("FFF"))
' The example displays the following output to the console:
'    180
'    18      
DateTime dateValue = new DateTime(2008, 7, 16, 8, 32, 45, 180); 
Console.WriteLine(dateValue.ToString("fff"));    
Console.WriteLine(dateValue.ToString("FFF"));
// The example displays the following output to the console:
//    180
//    18      

在定义包括日期和时间的毫秒部分的完整自定义格式说明符时,会产生以下问题:定义的硬编码格式可能无法与应用程序当前区域性中的时间元素排列方式相对应。 更好的替代方法是检索由当前区域性的 DateTimeFormatInfo 对象定义的某个日期和时间显示模式,并将其修改为包括毫秒部分。 该示例也阐释了这种方法。 它从 DateTimeFormatInfo.FullDateTimePattern 属性中检索当前区域性的完整日期和时间模式,然后在其秒钟模式后面插入自定义模式 .ffff。 请注意,该示例使用正则表达式在单个方法调用中执行此操作。

另外,还可以使用自定义格式说明符显示秒钟的小数(而非毫秒)部分。 例如,f 或 F 自定义格式说明符显示十分之一秒,ff 或 FF 自定义格式说明符显示百分之一秒,ffff 或 FFFF 自定义格式说明符显示万分之一秒。 在返回的字符串中,毫秒的小数部分将被截断,而不是进行舍入。 下面的示例中使用了这些格式说明符。

Dim dateValue As New DateTime(2008, 7, 16, 8, 32, 45, 180) 
Console.WriteLine("{0} seconds", dateValue.ToString("s.f"))
Console.WriteLine("{0} seconds", dateValue.ToString("s.ff"))      
Console.WriteLine("{0} seconds", dateValue.ToString("s.ffff"))
' The example displays the following output to the console:
'    45.1 seconds
'    45.18 seconds
'    45.1800 seconds
DateTime dateValue = new DateTime(2008, 7, 16, 8, 32, 45, 180); 
Console.WriteLine("{0} seconds", dateValue.ToString("s.f"));
Console.WriteLine("{0} seconds", dateValue.ToString("s.ff"));      
Console.WriteLine("{0} seconds", dateValue.ToString("s.ffff"));
// The example displays the following output to the console:
//    45.1 seconds
//    45.18 seconds
//    45.1800 seconds
注意注意

可以为秒钟显示非常微小的部分,例如万分之一秒或十万分之一秒。但是,这些值可能并没有意义。日期和时间值的精度取决于系统时钟的分辨率。在 Windows NT 3.5 和更高版本以及 Windows Vista 操作系统中,时钟的分辨率大约为 10-15 毫秒。

编译代码

在命令行处使用 csc.exe 或 vb.exe 编译代码。 若要在 Visual Studio 中编译代码,请将代码置于控制台应用程序项目模板中。

请参见

参考

DateTimeFormatInfo

概念

自定义日期和时间格式字符串