TimeSpan 構造体
時間間隔を表します。
この型のすべてのメンバの一覧については、TimeSpan メンバ を参照してください。
System.Object
System.ValueType
System.TimeSpan
<Serializable>
Public Structure TimeSpan Implements IComparable
[C#]
[Serializable]
public struct TimeSpan : IComparable
[C++]
[Serializable]
public __value struct TimeSpan : public IComparable
[JScript] JScript では、.NET Framework の構造体を利用することができます。ただし、独自に定義することはできません。
スレッドセーフ
この型の public static (Visual Basicでは Shared) のすべてのメンバは、マルチスレッド操作で安全に使用できます。インスタンスのメンバの場合は、スレッドセーフであるとは限りません。
解説
TimeSpan のインスタンスの値は、時間間隔を表します。この値は、このインスタンスに格納されているタイマ刻みの数になり、その範囲は Int64.MinValue から Int64.MaxValue までです。タイマ刻みは、指定できる時間の最小単位で、100 ナノ秒に相当します。タイマ刻みの数および TimeSpan の値は、正または負のどちらの場合もあります。
TimeSpan は "[-]d.hh:mm:ss.ff" という書式の文字列で表すことができます。ここで、"-" は負の TimeSpan 値の省略可能な符号、"d" は日、"hh" は時間、"mm" は分、"ss" は秒、"ff" は 1 秒の端数を表します。たとえば、タイマ刻みの数が 1.0e+13 に初期化された TimeSpan は、"11.13:46:40"、つまり、11 日 13 時間 46 分 40 秒を表します。
各月および年の日数が異なるため、TimeSpan で使用される時間の最も長い単位は日となります。
この値型は、 IComparable インターフェイスを実装します。
使用例
[Visual Basic, C#, C++] 複数の TimeSpan オブジェクトを作成し、各オブジェクトのプロパティを表示するコード例を次に示します。
' Example of the TimeSpan class properties.
Imports System
Imports Microsoft.VisualBasic
Module TimeSpanPropertiesDemo
Const headerFmt As String = vbCrLf & "{0,-45}"
Const dataFmt As String = "{0,-12}{1,8} {2,-18}{3,21}"
' Display the properties of the TimeSpan parameter.
Sub ShowTimeSpanProperties( interval as TimeSpan )
Console.WriteLine( "{0,21}", interval )
Console.WriteLine( dataFmt, _
"Days", interval.Days, "TotalDays", interval.TotalDays )
Console.WriteLine( dataFmt, "Hours", interval.Hours, _
"TotalHours", interval.TotalHours )
Console.WriteLine( dataFmt, "Minutes", interval.Minutes, _
"TotalMinutes", interval.TotalMinutes )
Console.WriteLine( dataFmt, "Seconds", interval.Seconds, _
"TotalSeconds", interval.TotalSeconds )
Console.WriteLine( dataFmt, _
"Milliseconds", interval.Milliseconds, _
"TotalMilliseconds", interval.TotalMilliseconds )
Console.WriteLine( dataFmt, _
Nothing, Nothing, "Ticks", interval.Ticks )
End Sub
Sub Main( )
Console.WriteLine( _
"This example of the TimeSpan class properties " & _
"generates the " & vbCrLf & "following output. It " & _
"creates several TimeSpan objects and " & vbCrLf & _
"displays the values of the TimeSpan properties for " & _
"each." )
' Create and display a TimeSpan value of 1 tick.
Console.Write( headerFmt, "TimeSpan( 1 )" )
ShowTimeSpanProperties( new TimeSpan( 1 ) )
' Create a TimeSpan value with a large number of ticks.
Console.Write( headerFmt, "TimeSpan( 111222333444555 )" )
ShowTimeSpanProperties( new TimeSpan( 111222333444555 ) )
' This TimeSpan has all fields specified.
Console.Write( headerFmt, "TimeSpan( 10, 20, 30, 40, 50 )" )
ShowTimeSpanProperties( new TimeSpan( 10, 20, 30, 40, 50 ) )
' This TimeSpan has all fields overflowing.
Console.Write( headerFmt, _
"TimeSpan( 1111, 2222, 3333, 4444, 5555 )" )
ShowTimeSpanProperties( _
new TimeSpan( 1111, 2222, 3333, 4444, 5555 ) )
' This TimeSpan is based on a number of days.
Console.Write( headerFmt, "FromDays( 20.84745602 )" )
ShowTimeSpanProperties( TimeSpan.FromDays( 20.84745602 ) )
End Sub
End Module
' This example of the TimeSpan class properties generates the
' following output. It creates several TimeSpan objects and
' displays the values of the TimeSpan properties for each.
'
' TimeSpan( 1 ) 00:00:00.0000001
' Days 0 TotalDays 1.15740740740741E-12
' Hours 0 TotalHours 2.77777777777778E-11
' Minutes 0 TotalMinutes 1.66666666666667E-09
' Seconds 0 TotalSeconds 1E-07
' Milliseconds 0 TotalMilliseconds 0.0001
' Ticks 1
'
' TimeSpan( 111222333444555 ) 128.17:30:33.3444555
' Days 128 TotalDays 128.729552597865
' Hours 17 TotalHours 3089.50926234875
' Minutes 30 TotalMinutes 185370.555740925
' Seconds 33 TotalSeconds 11122233.3444555
' Milliseconds 344 TotalMilliseconds 11122233344.4555
' Ticks 111222333444555
'
' TimeSpan( 10, 20, 30, 40, 50 ) 10.20:30:40.0500000
' Days 10 TotalDays 10.8546302083333
' Hours 20 TotalHours 260.511125
' Minutes 30 TotalMinutes 15630.6675
' Seconds 40 TotalSeconds 937840.05
' Milliseconds 50 TotalMilliseconds 937840050
' Ticks 9378400500000
'
' TimeSpan( 1111, 2222, 3333, 4444, 5555 ) 1205.22:47:09.5550000
' Days 1205 TotalDays 1205.94941614583
' Hours 22 TotalHours 28942.7859875
' Minutes 47 TotalMinutes 1736567.15925
' Seconds 9 TotalSeconds 104194029.555
' Milliseconds 555 TotalMilliseconds 104194029555
' Ticks 1041940295550000
'
' FromDays( 20.84745602 ) 20.20:20:20.2000000
' Days 20 TotalDays 20.8474560185185
' Hours 20 TotalHours 500.338944444444
' Minutes 20 TotalMinutes 30020.3366666667
' Seconds 20 TotalSeconds 1801220.2
' Milliseconds 200 TotalMilliseconds 1801220200
' Ticks 18012202000000
[C#]
// Example of the TimeSpan class properties.
using System;
class TimeSpanPropertiesDemo
{
const string headerFmt = "\n{0,-45}";
const string dataFmt = "{0,-12}{1,8} {2,-18}{3,21}" ;
// Display the properties of the TimeSpan parameter.
static void ShowTimeSpanProperties( TimeSpan interval )
{
Console.WriteLine( "{0,21}", interval );
Console.WriteLine( dataFmt, "Days", interval.Days,
"TotalDays", interval.TotalDays );
Console.WriteLine( dataFmt, "Hours", interval.Hours,
"TotalHours", interval.TotalHours );
Console.WriteLine( dataFmt, "Minutes", interval.Minutes,
"TotalMinutes", interval.TotalMinutes );
Console.WriteLine( dataFmt, "Seconds", interval.Seconds,
"TotalSeconds", interval.TotalSeconds );
Console.WriteLine( dataFmt, "Milliseconds",
interval.Milliseconds, "TotalMilliseconds",
interval.TotalMilliseconds );
Console.WriteLine( dataFmt, null, null,
"Ticks", interval.Ticks );
}
static void Main( )
{
Console.WriteLine(
"This example of the TimeSpan class properties " +
"generates the \nfollowing output. It " +
"creates several TimeSpan objects and \ndisplays " +
"the values of the TimeSpan properties for each." );
// Create and display a TimeSpan value of 1 tick.
Console.Write( headerFmt, "TimeSpan( 1 )" );
ShowTimeSpanProperties( new TimeSpan( 1 ) );
// Create a TimeSpan value with a large number of ticks.
Console.Write( headerFmt, "TimeSpan( 111222333444555 )" );
ShowTimeSpanProperties( new TimeSpan( 111222333444555 ) );
// This TimeSpan has all fields specified.
Console.Write( headerFmt, "TimeSpan( 10, 20, 30, 40, 50 )" );
ShowTimeSpanProperties( new TimeSpan( 10, 20, 30, 40, 50 ) );
// This TimeSpan has all fields overflowing.
Console.Write( headerFmt,
"TimeSpan( 1111, 2222, 3333, 4444, 5555 )" );
ShowTimeSpanProperties(
new TimeSpan( 1111, 2222, 3333, 4444, 5555 ) );
// This TimeSpan is based on a number of days.
Console.Write( headerFmt, "FromDays( 20.84745602 )" );
ShowTimeSpanProperties( TimeSpan.FromDays( 20.84745602 ) );
}
}
/*
This example of the TimeSpan class properties generates the
following output. It creates several TimeSpan objects and
displays the values of the TimeSpan properties for each.
TimeSpan( 1 ) 00:00:00.0000001
Days 0 TotalDays 1.15740740740741E-12
Hours 0 TotalHours 2.77777777777778E-11
Minutes 0 TotalMinutes 1.66666666666667E-09
Seconds 0 TotalSeconds 1E-07
Milliseconds 0 TotalMilliseconds 0.0001
Ticks 1
TimeSpan( 111222333444555 ) 128.17:30:33.3444555
Days 128 TotalDays 128.729552597865
Hours 17 TotalHours 3089.50926234875
Minutes 30 TotalMinutes 185370.555740925
Seconds 33 TotalSeconds 11122233.3444555
Milliseconds 344 TotalMilliseconds 11122233344.4555
Ticks 111222333444555
TimeSpan( 10, 20, 30, 40, 50 ) 10.20:30:40.0500000
Days 10 TotalDays 10.8546302083333
Hours 20 TotalHours 260.511125
Minutes 30 TotalMinutes 15630.6675
Seconds 40 TotalSeconds 937840.05
Milliseconds 50 TotalMilliseconds 937840050
Ticks 9378400500000
TimeSpan( 1111, 2222, 3333, 4444, 5555 ) 1205.22:47:09.5550000
Days 1205 TotalDays 1205.94941614583
Hours 22 TotalHours 28942.7859875
Minutes 47 TotalMinutes 1736567.15925
Seconds 9 TotalSeconds 104194029.555
Milliseconds 555 TotalMilliseconds 104194029555
Ticks 1041940295550000
FromDays( 20.84745602 ) 20.20:20:20.2000000
Days 20 TotalDays 20.8474560185185
Hours 20 TotalHours 500.338944444444
Minutes 20 TotalMinutes 30020.3366666667
Seconds 20 TotalSeconds 1801220.2
Milliseconds 200 TotalMilliseconds 1801220200
Ticks 18012202000000
*/
[C++]
// Example of the TimeSpan class properties.
#using <mscorlib.dll>
using namespace System;
// Display the properties of the TimeSpan parameter.
static void ShowTimeSpanProperties( TimeSpan interval )
{
Object* null = 0;
String* dataFmt = S"{0,-12}{1,8} {2,-18}{3,21}" ;
Console::WriteLine( S"{0,21}", __box( interval ) );
Console::WriteLine( dataFmt, S"Days", __box( interval.Days ),
S"TotalDays", __box( interval.TotalDays ) );
Console::WriteLine( dataFmt, S"Hours", __box( interval.Hours ),
S"TotalHours", __box( interval.TotalHours ) );
Console::WriteLine( dataFmt,
S"Minutes", __box( interval.Minutes ),
S"TotalMinutes", __box( interval.TotalMinutes ) );
Console::WriteLine( dataFmt,
S"Seconds", __box( interval.Seconds ),
S"TotalSeconds", __box( interval.TotalSeconds ) );
Console::WriteLine( dataFmt,
S"Milliseconds", __box( interval.Milliseconds ),
S"TotalMilliseconds", __box( interval.TotalMilliseconds ) );
Console::WriteLine( dataFmt, null, null,
S"Ticks", __box( interval.Ticks ) );
}
void main( )
{
String* headerFmt = S"\n{0,-45}";
Console::WriteLine(
S"This example of the TimeSpan class properties "
S"generates the \nfollowing output. It "
S"creates several TimeSpan objects and \ndisplays "
S"the values of the TimeSpan properties for each." );
// Create and display a TimeSpan value of 1 tick.
Console::Write( headerFmt, S"TimeSpan( 1 )" );
ShowTimeSpanProperties( TimeSpan( 1 ) );
// Create a TimeSpan value with a large number of ticks.
Console::Write( headerFmt, S"TimeSpan( 111222333444555 )" );
ShowTimeSpanProperties( TimeSpan( 111222333444555 ) );
// This TimeSpan has all fields specified.
Console::Write( headerFmt, S"TimeSpan( 10, 20, 30, 40, 50 )" );
ShowTimeSpanProperties( TimeSpan( 10, 20, 30, 40, 50 ) );
// This TimeSpan has all fields overflowing.
Console::Write( headerFmt,
S"TimeSpan( 1111, 2222, 3333, 4444, 5555 )" );
ShowTimeSpanProperties(
TimeSpan( 1111, 2222, 3333, 4444, 5555 ) );
// This TimeSpan is based on a number of days.
Console::Write( headerFmt, S"FromDays( 20.84745602 )" );
ShowTimeSpanProperties( TimeSpan::FromDays( 20.84745602 ) );
}
/*
This example of the TimeSpan class properties generates the
following output. It creates several TimeSpan objects and
displays the values of the TimeSpan properties for each.
TimeSpan( 1 ) 00:00:00.0000001
Days 0 TotalDays 1.15740740740741E-12
Hours 0 TotalHours 2.77777777777778E-11
Minutes 0 TotalMinutes 1.66666666666667E-09
Seconds 0 TotalSeconds 1E-07
Milliseconds 0 TotalMilliseconds 0.0001
Ticks 1
TimeSpan( 111222333444555 ) 128.17:30:33.3444555
Days 128 TotalDays 128.729552597865
Hours 17 TotalHours 3089.50926234875
Minutes 30 TotalMinutes 185370.555740925
Seconds 33 TotalSeconds 11122233.3444555
Milliseconds 344 TotalMilliseconds 11122233344.4555
Ticks 111222333444555
TimeSpan( 10, 20, 30, 40, 50 ) 10.20:30:40.0500000
Days 10 TotalDays 10.8546302083333
Hours 20 TotalHours 260.511125
Minutes 30 TotalMinutes 15630.6675
Seconds 40 TotalSeconds 937840.05
Milliseconds 50 TotalMilliseconds 937840050
Ticks 9378400500000
TimeSpan( 1111, 2222, 3333, 4444, 5555 ) 1205.22:47:09.5550000
Days 1205 TotalDays 1205.94941614583
Hours 22 TotalHours 28942.7859875
Minutes 47 TotalMinutes 1736567.15925
Seconds 9 TotalSeconds 104194029.555
Milliseconds 555 TotalMilliseconds 104194029555
Ticks 1041940295550000
FromDays( 20.84745602 ) 20.20:20:20.2000000
Days 20 TotalDays 20.8474560185185
Hours 20 TotalHours 500.338944444444
Minutes 20 TotalMinutes 30020.3366666667
Seconds 20 TotalSeconds 1801220.2
Milliseconds 200 TotalMilliseconds 1801220200
Ticks 18012202000000
*/
[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン をクリックします。
必要条件
名前空間: System
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET
アセンブリ: Mscorlib (Mscorlib.dll 内)