다음을 통해 공유


TimeSpan 구조체

시간 간격을 나타냅니다.

네임스페이스: System
어셈블리: mscorlib(mscorlib.dll)

구문

‘선언
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public Structure TimeSpan
    Implements IComparable, IComparable(Of TimeSpan), _
    IEquatable(Of TimeSpan)
‘사용 방법
Dim instance As TimeSpan
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public struct TimeSpan : IComparable, IComparable<TimeSpan>, 
    IEquatable<TimeSpan>
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public value class TimeSpan : IComparable, IComparable<TimeSpan>, 
    IEquatable<TimeSpan>
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
public final class TimeSpan extends ValueType implements IComparable, IComparable<TimeSpan>, 
    IEquatable<TimeSpan>
JScript에서는 구조체를 사용할 수 있지만 새로 선언할 수는 없습니다.

설명

TimeSpan 개체는 양수 또는 음수의 일, 시, 분, 초 및 초의 소수 부분 단위로 측정된 시간 간격 또는 기간을 나타냅니다. 기간을 측정하는 데 사용하는 최대 시간 단위는 일입니다. 월과 년과 같은 시간 단위에서는 일 수가 변하기 때문에 일관성을 위해 시간 간격은 일 단위로 측정됩니다.

TimeSpan 개체의 값은 표시된 시간 간격과 동일한 틱 수입니다. 틱 하나는 100나노초이며 TimeSpan 개체 값의 범위는 MinValue에서 MaxValue까지입니다.

TimeSpan 값은 [-]d.hh:mm:ss.ff로 나타낼 수 있습니다. 여기서 선택적 - 기호는 음의 시간을 나타내며, d 구성 요소는 일, hh는 24시간을 기준으로 측정한 시, mm은 분, ss는 초, ff는 초의 소수 부분입니다. 즉, 시간 간격은 양수나 음수의 시간이 없는 일 수, 시간이 있는 일 수, 또는 시간으로만 구성됩니다. 예를 들어, 1.0e+13틱으로 초기화된 TimeSpan 개체의 텍스트 표현은 "11.13:46:40"으로 나타내는데, 이것은 11일, 13시간, 46분, 40초를 의미합니다.

TimeSpan 형식은 System.IComparableSystem.IComparable 인터페이스를 구현합니다.

예제

다음 코드 예제에서는 몇 개의 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
// 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
*/                           
// Example of the TimeSpan class properties.
using namespace System;

// Display the properties of the TimeSpan parameter.
static void ShowTimeSpanProperties( TimeSpan interval )
{
   Object^ null = nullptr;
   String^ dataFmt = "{0,-12}{1,8}       {2,-18}{3,21}";
   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 );
}

int main()
{
   String^ headerFmt = "\n{0,-45}";
   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( TimeSpan(1) );
   
   // Create a TimeSpan value with a large number of ticks.
   Console::Write( headerFmt, "TimeSpan( 111222333444555 )" );
   ShowTimeSpanProperties( TimeSpan(111222333444555) );
   
   // This TimeSpan has all fields specified.
   Console::Write( headerFmt, "TimeSpan( 10, 20, 30, 40, 50 )" );
   ShowTimeSpanProperties( TimeSpan(10,20,30,40,50) );
   
   // This TimeSpan has all fields overflowing.
   Console::Write( headerFmt, "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, "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
*/
// Example of the TimeSpan class properties.
import System.*;

class TimeSpanPropertiesDemo
{
    private static String headerFmt = "\n{0,-45}";
    private static 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, new Object[] { "Days",
            String.valueOf(interval.get_Days()), "TotalDays",
            String.valueOf(interval.get_TotalDays()) });
        Console.WriteLine(dataFmt, new Object[] { "Hours", 
            String.valueOf(interval.get_Hours()), "TotalHours",
            String.valueOf(interval.get_TotalHours()) });
        Console.WriteLine(dataFmt, new Object[] { "Minutes",
            String.valueOf(interval.get_Minutes()), "TotalMinutes",
            String.valueOf(interval.get_TotalMinutes()) });
        Console.WriteLine(dataFmt, new Object[] { "Seconds",
            String.valueOf(interval.get_Seconds()), "TotalSeconds",
            (System.Double)interval.get_TotalSeconds() });
        Console.WriteLine(dataFmt, new Object[] { "Milliseconds", 
            String.valueOf(interval.get_Milliseconds()), "TotalMilliseconds",
            (System.Double)interval.get_TotalMilliseconds() });
        Console.WriteLine(dataFmt, new Object[] { null, null, "Ticks", 
            String.valueOf(interval.get_Ticks()) });
    } //ShowTimeSpanProperties

    public static void main(String[] args)
    {
        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(111222333444555L));

        // 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));
    } //main
} //TimeSpanPropertiesDemo

/*
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.1574074074074074E-12
Hours              0       TotalHours        2.7777777777777777E-11
Minutes            0       TotalMinutes      1.6666666666666667E-9
Seconds            0       TotalSeconds                      1E-07
Milliseconds       0       TotalMilliseconds                0.0001
                           Ticks                                 1

TimeSpan( 111222333444555 )                   128.17:30:33.3444555
Days             128       TotalDays             128.7295525978646
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.854630208333333
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.9494161458333
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.847456018518518
Hours             20       TotalHours           500.33894444444445
Minutes           20       TotalMinutes         30020.336666666666
Seconds           20       TotalSeconds                  1801220.2
Milliseconds     200       TotalMilliseconds            1801220200
                           Ticks                    18012202000000
*/

스레드로부터의 안전성

이 형식의 모든 public static(Visual Basic의 경우 Shared) 멤버는 스레드로부터 안전합니다. 인터페이스 멤버는 스레드로부터 안전하지 않습니다.

플랫폼

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework에서 모든 플래폼의 모든 버전을 지원하지는 않습니다. 지원되는 버전의 목록은 시스템 요구 사항을 참조하십시오.

버전 정보

.NET Framework

2.0, 1.1, 1.0에서 지원

.NET Compact Framework

2.0, 1.0에서 지원

참고 항목

참조

TimeSpan 멤버
System 네임스페이스
DateTime 구조체
Calendar