方法: 日付および時刻の値のミリ秒部分を表示する

DateTime.ToString() などの既定の日付および時刻書式指定メソッドは時刻値の時間、分、秒を含めますが、ミリ秒の部分は含めません。 この記事では、書式設定された日付および時刻文字列の中にミリ秒コンポーネントを含める方法について説明します。

DateTime 値のミリ秒部分を表示するには

  1. 文字列形式の日付を処理している場合には、静的 DateTime.Parse(String) または DateTimeOffset.Parse(String) メソッドを使用して、その日付を DateTime 値または DateTimeOffset 値に変換します。

  2. 時刻のミリ秒部分の文字列表現を抽出するには、日付および時刻の値の 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";

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"));

    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"

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

            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 を挿入します。 この例では、1 つのメソッド呼び出しでこの操作を実行するために正規表現が使用されています。

また、カスタム書式指定子を使用して、ミリ秒以外の秒の端数を表示することもできます。 たとえば、カスタム書式指定子 f または F は 1/10 秒を表示し、カスタム書式指定子 ff または FF は 1/100 秒、カスタム書式指定子 ffff または FFFF は 1/10000 秒をそれぞれ表示します。 返される文字列内では、ミリ秒の端数は丸められるのではなく、切り捨てられます。 次の例では、これらの書式指定子が使用されています。

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

Note

1/10000 秒、1/100000 秒などの非常に小さな端数単位を表示することが可能です。 ただし、このような値を表示してもあまり意味がない可能性があります。 日付および時刻の値の精度は、オペレーティング システム クロックの分解能に依存します。 詳細については、オペレーティング システムで使用される API を参照してください。

関連項目