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

顯示 DateTime 值的毫秒部分

  • 如果您要處理日期的字串表示法,請使用靜態 DateTime.Parse(String) DateTimeOffset.Parse(String) 方法,將其轉換成 DateTime DateTimeOffset 值。

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

    屬性 System.Globalization.NumberFormatInfo.NumberDecimalSeparator 會指定毫秒分隔符號。

    此範例會對主控台單獨顯示 DateTime DateTimeOffset 值的毫秒元件,以及包含在較長的日期和時間字串中一併顯示。

    using System.Globalization; using System.Text.RegularExpressions; string dateString = "7/16/2008 8:32:45.126 AM"; 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")); string fullPattern = DateTimeFormatInfo.CurrentInfo.FullDateTimePattern; // Create a format similar to .fff but based on the current culture. string millisecondFormat = $"{NumberFormatInfo.CurrentInfo.NumberDecimalSeparator}fff"; // Append millisecond pattern to current culture's full date time pattern. fullPattern = Regex.Replace(fullPattern, "(:ss|:s)", $"$1{millisecondFormat}"); // 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 Imports System.Globalization Imports System.Text.REgularExpressions Module MillisecondDisplay Public Sub Main() Dim dateString As String = "7/16/2008 8:32:45.126 AM" 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")) Dim fullPattern As String = DateTimeFormatInfo.CurrentInfo.FullDateTimePattern ' Create a format similar to .fff but based on the current culture. Dim millisecondFormat as String = $"{NumberFormatInfo.CurrentInfo.NumberDecimalSeparator}fff" ' Append millisecond pattern to current culture's full date time pattern. fullPattern = Regex.Replace(fullPattern, "(:ss|:s)", $"$1{millisecondFormat}") ' 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

    fff 格式模式會包含毫秒值尾端的任何零。 FFF 格式模式則會加以隱藏。 下列範例說明其間的差異:

    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 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

    定義如果包含日期和時間毫秒部分的完整自訂格式規範,問題在於會定義硬式編碼格式,而該格式可能不會對應至應用程式目前文化特性中時間元素的排列。 建議改為擷取目前文化特性之 DateTimeFormatInfo 物件所定義的其中一個日期和時間顯示模式,然後修改該模式以加入毫秒。 此範例也會說明這個方法。 它會從 DateTimeFormatInfo.FullDateTimePattern 屬性擷取目前文化特性的完整日期和時間模式,然後插入自訂模式 fff 以及目前文化特性的毫秒分隔符號。 請注意,此範例使用規則運算式,以單一方法呼叫來執行這項操作。

    除了毫秒之外,您也可以使用自訂格式規範顯示秒的其他小數部分。 例如, f F 自訂格式規範會顯示十分之一秒, ff FF 自訂格式規範會顯示百分之一秒,而 ffff FFFF 自訂格式規範會顯示萬分之一秒。 毫秒的小數部分會在傳回的字串中被截斷,而不是四捨五入。 下列範例使用這些格式規範:

    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 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

    您可以使用非常小的小數單位來顯示秒,例如萬分之一秒或十萬分之一秒。 不過,這些值可能沒有太大的意義。 日期和時間值的精確度會根據作業系統時鐘的解析度而定。 如需詳細資訊,請參閱您的作業系統使用的 API:

  • Windows 7: GetSystemTimeAsFileTime
  • Windows 8 和更新版本: GetSystemTimePreciseAsFileTime
  • Linux 和 macOS: clock_gettime
  • DateTimeFormatInfo
  • 自訂日期和時間格式字串
  •