英語で読む

次の方法で共有


TimeSpan コンストラクター

定義

TimeSpan 構造体の新しいインスタンスを初期化します。

オーバーロード

TimeSpan(Int64)

TimeSpan 構造体の新しいインスタンスを、指定したタイマー刻み数に初期化します。

TimeSpan(Int32, Int32, Int32)

TimeSpan 構造体の新しいインスタンスを、指定した時間数、分数、秒数に初期化します。

TimeSpan(Int32, Int32, Int32, Int32)

TimeSpan 構造体の新しいインスタンスを指定された日数、時間数、分数、秒数に初期化します。

TimeSpan(Int32, Int32, Int32, Int32, Int32)

TimeSpan 構造体の新しいインスタンスを、指定した日数、時間数、分数、秒数、ミリ秒数に初期化します。

TimeSpan(Int32, Int32, Int32, Int32, Int32, Int32)

構造体の新しいインスタンスを TimeSpan 、指定した日数、時間、分、秒、ミリ秒、マイクロ秒に初期化します。

TimeSpan(Int64)

ソース:
TimeSpan.cs
ソース:
TimeSpan.cs
ソース:
TimeSpan.cs

TimeSpan 構造体の新しいインスタンスを、指定したタイマー刻み数に初期化します。

C#
public TimeSpan (long ticks);

パラメーター

ticks
Int64

100 ナノ秒単位で表される期間。

次の例では、 を指定された数のティックに初期化TimeSpanするコンストラクター オーバーロードを使用して、いくつかのTimeSpanオブジェクトを作成します。

C#
// Example of the TimeSpan( long ) constructor.
using System;

class TimeSpanCtorLDemo
{
    // Create a TimeSpan object and display its value.
    static void CreateTimeSpan( long ticks )
    {
        TimeSpan elapsedTime = new TimeSpan( ticks );

        // Format the constructor for display.
        string ctor = String.Format( "TimeSpan( {0} )", ticks );

        // Pad the end of a TimeSpan string with spaces if
        // it does not contain milliseconds.
        string  elapsedStr = elapsedTime.ToString( );
        int     pointIndex = elapsedStr.IndexOf( ':' );

        pointIndex = elapsedStr.IndexOf( '.', pointIndex );
        if( pointIndex < 0 ) elapsedStr += "        ";

        // Display the constructor and its value.
        Console.WriteLine( "{0,-33}{1,24}", ctor, elapsedStr );
    }
    
    static void Main( )
    {
        Console.WriteLine( 
            "This example of the TimeSpan( long ) constructor " +
            "\ngenerates the following output.\n" );
        Console.WriteLine( "{0,-33}{1,16}", "Constructor", "Value" );
        Console.WriteLine( "{0,-33}{1,16}", "-----------", "-----" );

        CreateTimeSpan( 1 );                
        CreateTimeSpan( 999999 );                
        CreateTimeSpan( -1000000000000 );        
        CreateTimeSpan( 18012202000000 );        
        CreateTimeSpan( 999999999999999999 );    
        CreateTimeSpan( 1000000000000000000 );   
    } 
} 

/*
This example of the TimeSpan( long ) constructor
generates the following output.

Constructor                                 Value
-----------                                 -----
TimeSpan( 1 )                            00:00:00.0000001
TimeSpan( 999999 )                       00:00:00.0999999
TimeSpan( -1000000000000 )            -1.03:46:40
TimeSpan( 18012202000000 )            20.20:20:20.2000000
TimeSpan( 999999999999999999 )   1157407.09:46:39.9999999
TimeSpan( 1000000000000000000 )  1157407.09:46:40
*/

注釈

1 ティックは、100 ナノ秒または 1,000 万分の 1 秒を表します。 ミリ秒単位で 10,000 ティックがあります。

こちらもご覧ください

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

TimeSpan(Int32, Int32, Int32)

ソース:
TimeSpan.cs
ソース:
TimeSpan.cs
ソース:
TimeSpan.cs

TimeSpan 構造体の新しいインスタンスを、指定した時間数、分数、秒数に初期化します。

C#
public TimeSpan (int hours, int minutes, int seconds);

パラメーター

hours
Int32

時間数。

minutes
Int32

分数。

seconds
Int32

秒数。

例外

パラメーターは、TimeSpanTimeSpan.MinValue より小さい値または TimeSpan.MaxValue より大きい値を指定します。

次の例では、 を指定した時間、分、秒に初期化TimeSpanするコンストラクター オーバーロードを使用して、いくつかのTimeSpanオブジェクトを作成します。

C#
// Example of the TimeSpan( int, int, int ) constructor.
using System;

class TimeSpanCtorIIIDemo
{
    // Create a TimeSpan object and display its value.
    static void CreateTimeSpan( int hours, int minutes, 
        int seconds )
    {
        TimeSpan elapsedTime = 
            new TimeSpan( hours, minutes, seconds );

        // Format the constructor for display.
        string ctor = String.Format( "TimeSpan( {0}, {1}, {2} )", 
            hours, minutes, seconds);

        // Display the constructor and its value.
        Console.WriteLine( "{0,-37}{1,16}", 
            ctor, elapsedTime.ToString( ) );
    }
    
    static void Main( )
    {
        Console.WriteLine(
            "This example of the TimeSpan( int, int, int ) " +
            "\nconstructor generates the following output.\n" );
        Console.WriteLine( "{0,-37}{1,16}", "Constructor", "Value" );
        Console.WriteLine( "{0,-37}{1,16}", "-----------", "-----" );

        CreateTimeSpan( 10, 20, 30 );
        CreateTimeSpan( -10, 20, 30 );
        CreateTimeSpan( 0, 0, 37230 );
        CreateTimeSpan( 1000, 2000, 3000 );
        CreateTimeSpan( 1000, -2000, -3000 );
        CreateTimeSpan( 999999, 999999, 999999 );
    } 
} 

/*
This example of the TimeSpan( int, int, int )
constructor generates the following output.

Constructor                                     Value
-----------                                     -----
TimeSpan( 10, 20, 30 )                       10:20:30
TimeSpan( -10, 20, 30 )                     -09:39:30
TimeSpan( 0, 0, 37230 )                      10:20:30
TimeSpan( 1000, 2000, 3000 )              43.02:10:00
TimeSpan( 1000, -2000, -3000 )            40.05:50:00
TimeSpan( 999999, 999999, 999999 )     42372.15:25:39
*/

注釈

指定した hoursminutesseconds はティックに変換され、その値はこのインスタンスを初期化します。

こちらもご覧ください

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

TimeSpan(Int32, Int32, Int32, Int32)

ソース:
TimeSpan.cs
ソース:
TimeSpan.cs
ソース:
TimeSpan.cs

TimeSpan 構造体の新しいインスタンスを指定された日数、時間数、分数、秒数に初期化します。

C#
public TimeSpan (int days, int hours, int minutes, int seconds);

パラメーター

days
Int32

日数。

hours
Int32

時間数。

minutes
Int32

分数。

seconds
Int32

秒数。

例外

パラメーターは、TimeSpanTimeSpan.MinValue より小さい値または TimeSpan.MaxValue より大きい値を指定します。

次の例では、 を指定した日数、時間、分、秒に初期化TimeSpanするコンストラクター オーバーロードを使用して、いくつかのTimeSpanオブジェクトを作成します。

C#
using System;

class Example
{
    // Create a TimeSpan object and display its value.
    static void CreateTimeSpan( int days, int hours, 
        int minutes, int seconds )
    {
        TimeSpan elapsedTime = 
            new TimeSpan( days, hours, minutes, seconds );

        // Format the constructor for display.
        string ctor = 
            String.Format( "TimeSpan( {0}, {1}, {2}, {3} )", 
                days, hours, minutes, seconds);

        // Display the constructor and its value.
        Console.WriteLine( "{0,-44}{1,16}", 
            ctor, elapsedTime.ToString( ) );
    }
    
    static void Main( )
    {
        Console.WriteLine( "{0,-44}{1,16}", "Constructor", "Value" );
        Console.WriteLine( "{0,-44}{1,16}", "-----------", "-----" );

        CreateTimeSpan( 10, 20, 30, 40 );
        CreateTimeSpan( -10, 20, 30, 40 );
        CreateTimeSpan( 0, 0, 0, 937840 );
        CreateTimeSpan( 1000, 2000, 3000, 4000 );
        CreateTimeSpan( 1000, -2000, -3000, -4000 );
        CreateTimeSpan( 999999, 999999, 999999, 999999 );
    } 
} 
// The example displays the following output:
//       Constructor                                            Value
//       -----------                                            -----
//       TimeSpan( 10, 20, 30, 40 )                       10.20:30:40
//       TimeSpan( -10, 20, 30, 40 )                      -9.03:29:20
//       TimeSpan( 0, 0, 0, 937840 )                      10.20:30:40
//       TimeSpan( 1000, 2000, 3000, 4000 )             1085.11:06:40
//       TimeSpan( 1000, -2000, -3000, -4000 )           914.12:53:20
//       TimeSpan( 999999, 999999, 999999, 999999 )  1042371.15:25:39

注釈

指定した days、、 hoursminutesおよび seconds はティックに変換され、その値によってこのインスタンスが初期化されます。

こちらもご覧ください

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

TimeSpan(Int32, Int32, Int32, Int32, Int32)

ソース:
TimeSpan.cs
ソース:
TimeSpan.cs
ソース:
TimeSpan.cs

TimeSpan 構造体の新しいインスタンスを、指定した日数、時間数、分数、秒数、ミリ秒数に初期化します。

C#
public TimeSpan (int days, int hours, int minutes, int seconds, int milliseconds);

パラメーター

days
Int32

日数。

hours
Int32

時間数。

minutes
Int32

分数。

seconds
Int32

秒数。

milliseconds
Int32

ミリ秒数。

例外

パラメーターは、TimeSpanTimeSpan.MinValue より小さい値または TimeSpan.MaxValue より大きい値を指定します。

次の例では、 を指定した日数、時間、分、秒、ミリ秒に初期化TimeSpanするコンストラクター オーバーロードを使用して、いくつかのTimeSpanオブジェクトを作成します。

C#
// Example of the TimeSpan( int, int, int, int, int ) constructor. 
using System;

class TimeSpanCtorIIIIIDemo
{
    // Create a TimeSpan object and display its value.
    static void CreateTimeSpan( int days, int hours, 
        int minutes, int seconds, int millisec )
    {
        TimeSpan elapsedTime = new TimeSpan( 
            days, hours, minutes, seconds, millisec );

        // Format the constructor for display.
        string ctor = 
            String.Format( "TimeSpan( {0}, {1}, {2}, {3}, {4} )", 
                days, hours, minutes, seconds, millisec);

        // Display the constructor and its value.
        Console.WriteLine( "{0,-48}{1,24}", 
            ctor, elapsedTime.ToString( ) );
    }

    static void Main( )
    {
        Console.WriteLine( 
            "This example of the " +
            "TimeSpan( int, int, int, int, int ) " +
            "\nconstructor generates the following output.\n" );
        Console.WriteLine( "{0,-48}{1,16}", "Constructor", "Value" );
        Console.WriteLine( "{0,-48}{1,16}", "-----------", "-----" );

        CreateTimeSpan( 10, 20, 30, 40, 50 );
        CreateTimeSpan( -10, 20, 30, 40, 50 );
        CreateTimeSpan( 0, 0, 0, 0, 937840050 );
        CreateTimeSpan( 1111, 2222, 3333, 4444, 5555 );
        CreateTimeSpan( 1111, -2222, -3333, -4444, -5555 );
        CreateTimeSpan( 99999, 99999, 99999, 99999, 99999 );
    } 
} 

/*
This example of the TimeSpan( int, int, int, int, int )
constructor generates the following output.

Constructor                                                Value
-----------                                                -----
TimeSpan( 10, 20, 30, 40, 50 )                       10.20:30:40.0500000
TimeSpan( -10, 20, 30, 40, 50 )                      -9.03:29:19.9500000
TimeSpan( 0, 0, 0, 0, 937840050 )                    10.20:30:40.0500000
TimeSpan( 1111, 2222, 3333, 4444, 5555 )           1205.22:47:09.5550000
TimeSpan( 1111, -2222, -3333, -4444, -5555 )       1016.01:12:50.4450000
TimeSpan( 99999, 99999, 99999, 99999, 99999 )    104236.05:27:18.9990000
*/

注釈

指定した days、、 hoursminutessecondsおよび milliseconds はティックに変換され、その値によってこのインスタンスが初期化されます。

こちらもご覧ください

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

TimeSpan(Int32, Int32, Int32, Int32, Int32, Int32)

ソース:
TimeSpan.cs
ソース:
TimeSpan.cs
ソース:
TimeSpan.cs

構造体の新しいインスタンスを TimeSpan 、指定した日数、時間、分、秒、ミリ秒、マイクロ秒に初期化します。

C#
public TimeSpan (int days, int hours, int minutes, int seconds, int milliseconds, int microseconds);

パラメーター

days
Int32

日数。

hours
Int32

時間数。

minutes
Int32

分数。

seconds
Int32

秒数。

milliseconds
Int32

ミリ秒数。

microseconds
Int32

マイクロ秒の数。

例外

パラメーターは、 TimeSpan より小さい MinValue 値またはより大きい値を指定します MaxValue

注釈

指定した days、、hourssecondsminutesmillisecondsmicroseconds はティックに変換され、その値によってこのインスタンスが初期化されます。

適用対象

.NET 9 およびその他のバージョン
製品 バージョン
.NET 7, 8, 9