方法: 日付および時刻の値のミリ秒部分を表示するHow to: Display Milliseconds in Date and Time Values
DateTime.ToString() などの既定の日付および時刻書式指定メソッドは時刻値の時間、分、秒を含めますが、ミリ秒の部分は含めません。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.
DateTime 値のミリ秒部分を表示するにはTo display the millisecond component of a DateTime value
文字列形式の日付を処理している場合には、静的 DateTime または DateTimeOffset メソッドを使用して、その日付をDateTime.Parse(String) 値または DateTimeOffset.Parse(String) 値に変換します。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.
時刻のミリ秒部分の文字列表現を抽出するには、日付および時刻の値の DateTime.ToString(String) メソッドまたは ToString メソッドを呼び出して、カスタム書式パターン
fff
またはFFF
を単独で、あるいは他のカスタム書式指定子と共にformat
パラメーターとして渡します。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 thefff
orFFF
custom format pattern either alone or with other custom format specifiers as theformat
parameter.
例Example
この例では、DateTime および DateTimeOffset 値のミリ秒の部分をコンソールに表示します。単独で表示する場合と、より長い日付および時刻文字列に含める場合の両方を示します。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.
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
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
fff
書式パターンを使うと、ミリ秒値に後続のゼロがあればこれは含められます。The fff
format pattern includes any trailing zeros in the millisecond value. FFF
書式パターンでは、これが除外されます。The FFF
format pattern suppresses them. この違いを次の例に示します。The difference is illustrated in the following example.
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
日付および時刻のミリ秒部分を含む完全なカスタム書式指定子を定義する場合、アプリケーションの現在のカルチャの時間要素の取り決めに対応しない可能性のあるハードコーディングされた書式を定義するという問題が生じます。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. これに代わるより良い方法は、現在のカルチャの DateTimeFormatInfo オブジェクトで定義されているいずれかの日付および時刻表示パターンを取得して、ミリ秒を含むようにそれを修正することです。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. 現在のカルチャの完全な日付および時刻パターンを DateTimeFormatInfo.FullDateTimePattern プロパティから取得した後、その秒パターンの後にカスタム パターン .ffff
を挿入します。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. この例では、1 つのメソッド呼び出しでこの操作を実行するために正規表現が使われていることに注意してください。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. たとえば、カスタム書式指定子 f
または F
は 1/10 秒を表示し、カスタム書式指定子 ff
または FF
は 1/100 秒、カスタム書式指定子 ffff
または FFFF
は 1/10000 秒をそれぞれ表示します。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.
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
注意
1/10000 秒、1/100000 秒などの非常に小さな端数単位を表示することが可能です。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. Windows NT 3.5 以降および Windows Vista オペレーティング システムでは、クロックの解像力は約 10-15 ミリ秒です。On Windows NT 3.5 and later, and Windows Vista operating systems, the clock's resolution is approximately 10-15 milliseconds.