共用方式為


HOW TO:顯示日期和時間值的毫秒元件

更新:2007 年 11 月

預設的日期和時間格式化方法 (例如 DateTime.ToString()) 包括時間值的時、分和秒,但不包括毫秒元件。本主題將說明如何將日期和時間的毫秒元件加入格式化的日期和時間字串中。

顯示 DateTime 值的毫秒元件

  1. 如果您使用的是日期的字串表示,請使用靜態 DateTime.Parse(String)DateTimeOffset.Parse(String) 方法,將它轉換成 DateTimeDateTimeOffset 值。

  2. 若要擷取時間毫秒元件的字串表示,請呼叫日期和時間值的 DateTime.ToString(String) 或是 ToString 方法,並且單獨傳遞 fff 或 FFF 自訂格式模式,或是連同其他自訂格式規範一併傳遞做為 format 參數。

範例

這個範例會對主控台單獨顯示 DateTime 的毫秒元件和 DateTimeOffset 值,且兩者都會包含在較長的日期和時間字串中。

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