TimeSpan 結構
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
表示時間間隔。
public value class TimeSpan : IComparable, IComparable<TimeSpan>, IEquatable<TimeSpan>, IFormattable
public value class TimeSpan : IComparable, IComparable<TimeSpan>, IEquatable<TimeSpan>, IParsable<TimeSpan>, ISpanFormattable, ISpanParsable<TimeSpan>, IUtf8SpanFormattable
public value class TimeSpan : IComparable, IComparable<TimeSpan>, IEquatable<TimeSpan>, ISpanFormattable
public value class TimeSpan : IComparable, IComparable<TimeSpan>, IEquatable<TimeSpan>, IParsable<TimeSpan>, ISpanFormattable, ISpanParsable<TimeSpan>
public value class TimeSpan : IComparable
public value class TimeSpan : IComparable, IComparable<TimeSpan>, IEquatable<TimeSpan>
public struct TimeSpan : IComparable, IComparable<TimeSpan>, IEquatable<TimeSpan>, IFormattable
public readonly struct TimeSpan : IComparable, IComparable<TimeSpan>, IEquatable<TimeSpan>, IParsable<TimeSpan>, ISpanFormattable, ISpanParsable<TimeSpan>, IUtf8SpanFormattable
public readonly struct TimeSpan : IComparable, IComparable<TimeSpan>, IEquatable<TimeSpan>, IFormattable
public readonly struct TimeSpan : IComparable, IComparable<TimeSpan>, IEquatable<TimeSpan>, ISpanFormattable
public readonly struct TimeSpan : IComparable, IComparable<TimeSpan>, IEquatable<TimeSpan>, IParsable<TimeSpan>, ISpanFormattable, ISpanParsable<TimeSpan>
[System.Serializable]
public struct TimeSpan : IComparable
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public struct TimeSpan : IComparable, IComparable<TimeSpan>, IEquatable<TimeSpan>
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public struct TimeSpan : IComparable, IComparable<TimeSpan>, IEquatable<TimeSpan>, IFormattable
type TimeSpan = struct
interface IFormattable
type TimeSpan = struct
interface IFormattable
interface IParsable<TimeSpan>
interface ISpanFormattable
interface ISpanParsable<TimeSpan>
interface IUtf8SpanFormattable
type TimeSpan = struct
interface ISpanFormattable
interface IFormattable
type TimeSpan = struct
interface IFormattable
interface IParsable<TimeSpan>
interface ISpanFormattable
interface ISpanParsable<TimeSpan>
[<System.Serializable>]
type TimeSpan = struct
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type TimeSpan = struct
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type TimeSpan = struct
interface IFormattable
Public Structure TimeSpan
Implements IComparable, IComparable(Of TimeSpan), IEquatable(Of TimeSpan), IFormattable
Public Structure TimeSpan
Implements IComparable, IComparable(Of TimeSpan), IEquatable(Of TimeSpan), IParsable(Of TimeSpan), ISpanFormattable, ISpanParsable(Of TimeSpan), IUtf8SpanFormattable
Public Structure TimeSpan
Implements IComparable, IComparable(Of TimeSpan), IEquatable(Of TimeSpan), ISpanFormattable
Public Structure TimeSpan
Implements IComparable, IComparable(Of TimeSpan), IEquatable(Of TimeSpan), IParsable(Of TimeSpan), ISpanFormattable, ISpanParsable(Of TimeSpan)
Public Structure TimeSpan
Implements IComparable
Public Structure TimeSpan
Implements IComparable, IComparable(Of TimeSpan), IEquatable(Of TimeSpan)
- 繼承
- 屬性
- 實作
範例
以下範例實例化一個 TimeSpan 物件,代表兩個日期之間的差異。 接著它會顯示物件 TimeSpan 的屬性。
// Define two dates.
DateTime date1 = new DateTime(2010, 1, 1, 8, 0, 15);
DateTime date2 = new DateTime(2010, 8, 18, 13, 30, 30);
// Calculate the interval between the two dates.
TimeSpan interval = date2 - date1;
Console.WriteLine("{0} - {1} = {2}", date2, date1, interval.ToString());
// Display individual properties of the resulting TimeSpan object.
Console.WriteLine(" {0,-35} {1,20}", "Value of Days Component:", interval.Days);
Console.WriteLine(" {0,-35} {1,20}", "Total Number of Days:", interval.TotalDays);
Console.WriteLine(" {0,-35} {1,20}", "Value of Hours Component:", interval.Hours);
Console.WriteLine(" {0,-35} {1,20}", "Total Number of Hours:", interval.TotalHours);
Console.WriteLine(" {0,-35} {1,20}", "Value of Minutes Component:", interval.Minutes);
Console.WriteLine(" {0,-35} {1,20}", "Total Number of Minutes:", interval.TotalMinutes);
Console.WriteLine(" {0,-35} {1,20:N0}", "Value of Seconds Component:", interval.Seconds);
Console.WriteLine(" {0,-35} {1,20:N0}", "Total Number of Seconds:", interval.TotalSeconds);
Console.WriteLine(" {0,-35} {1,20:N0}", "Value of Milliseconds Component:", interval.Milliseconds);
Console.WriteLine(" {0,-35} {1,20:N0}", "Total Number of Milliseconds:", interval.TotalMilliseconds);
Console.WriteLine(" {0,-35} {1,20:N0}", "Ticks:", interval.Ticks);
// This example displays the following output:
// 8/18/2010 1:30:30 PM - 1/1/2010 8:00:15 AM = 229.05:30:15
// Value of Days Component: 229
// Total Number of Days: 229.229340277778
// Value of Hours Component: 5
// Total Number of Hours: 5501.50416666667
// Value of Minutes Component: 30
// Total Number of Minutes: 330090.25
// Value of Seconds Component: 15
// Total Number of Seconds: 19,805,415
// Value of Milliseconds Component: 0
// Total Number of Milliseconds: 19,805,415,000
// Ticks: 198,054,150,000,000
// Define two dates.
let date1 = DateTime(2010, 1, 1, 8, 0, 15)
let date2 = DateTime(2010, 8, 18, 13, 30, 30)
// Calculate the interval between the two dates.
let interval = date2 - date1
printfn $"{date2} - {date1} = {interval}"
// Display individual properties of the resulting TimeSpan object.
printfn $""" {"Value of Days Component:",-35} {interval.Days,20}"""
printfn $""" {"Total Number of Days:",-35} {interval.TotalDays,20}"""
printfn $""" {"Value of Hours Component:",-35} {interval.Hours,20}"""
printfn $""" {"Total Number of Hours:",-35} {interval.TotalHours,20}"""
printfn $""" {"Value of Minutes Component:",-35} {interval.Minutes,20}"""
printfn $""" {"Total Number of Minutes:",-35} {interval.TotalMinutes,20}"""
printfn $""" {"Value of Seconds Component:",-35} {interval.Seconds,20:N0}"""
printfn $""" {"Total Number of Seconds:",-35} {interval.TotalSeconds,20:N0}"""
printfn $""" {"Value of Milliseconds Component:",-35} {interval.Milliseconds,20:N0}"""
printfn $""" {"Total Number of Milliseconds:",-35} {interval.TotalMilliseconds,20:N0}"""
printfn $""" {"Ticks:",-35} {interval.Ticks,20:N0}"""
// This example displays the following output:
// 8/18/2010 1:30:30 PM - 1/1/2010 8:00:15 AM = 229.05:30:15
// Value of Days Component: 229
// Total Number of Days: 229.229340277778
// Value of Hours Component: 5
// Total Number of Hours: 5501.50416666667
// Value of Minutes Component: 30
// Total Number of Minutes: 330090.25
// Value of Seconds Component: 15
// Total Number of Seconds: 19,805,415
// Value of Milliseconds Component: 0
// Total Number of Milliseconds: 19,805,415,000
// Ticks: 198,054,150,000,000
' Define two dates.
Dim date1 As Date = #1/1/2010 8:00:15AM#
Dim date2 As Date = #8/18/2010 1:30:30PM#
' Calculate the interval between the two dates.
Dim interval As TimeSpan = date2 - date1
Console.WriteLine("{0} - {1} = {2}", date2, date1, interval.ToString())
' Display individual properties of the resulting TimeSpan object.
Console.WriteLine(" {0,-35} {1,20}", "Value of Days Component:", interval.Days)
Console.WriteLine(" {0,-35} {1,20}", "Total Number of Days:", interval.TotalDays)
Console.WriteLine(" {0,-35} {1,20}", "Value of Hours Component:", interval.Hours)
Console.WriteLine(" {0,-35} {1,20}", "Total Number of Hours:", interval.TotalHours)
Console.WriteLine(" {0,-35} {1,20}", "Value of Minutes Component:", interval.Minutes)
Console.WriteLine(" {0,-35} {1,20}", "Total Number of Minutes:", interval.TotalMinutes)
Console.WriteLine(" {0,-35} {1,20:N0}", "Value of Seconds Component:", interval.Seconds)
Console.WriteLine(" {0,-35} {1,20:N0}", "Total Number of Seconds:", interval.TotalSeconds)
Console.WriteLine(" {0,-35} {1,20:N0}", "Value of Milliseconds Component:", interval.Milliseconds)
Console.WriteLine(" {0,-35} {1,20:N0}", "Total Number of Milliseconds:", interval.TotalMilliseconds)
Console.WriteLine(" {0,-35} {1,20:N0}", "Ticks:", interval.Ticks)
' The example displays the following output:
' 8/18/2010 1:30:30 PM - 1/1/2010 8:00:15 AM = 229.05:30:15
' Value of Days Component: 229
' Total Number of Days: 229.229340277778
' Value of Hours Component: 5
' Total Number of Hours: 5501.50416666667
' Value of Minutes Component: 30
' Total Number of Minutes: 330090.25
' Value of Seconds Component: 15
' Total Number of Seconds: 19,805,415
' Value of Milliseconds Component: 0
' Total Number of Milliseconds: 19,805,415,000
' Ticks: 198,054,150,000,000
# Define two dates.
$Date2 = Get-Date -Date '2010/8/18' -Hour 13 -Minute 30 -Second 30
$Date1 = Get-Date -Date '2010/1/1' -Hour 8 -Minute 0 -Second 15
# Calculate the interval between the two dates.
$Interval = $Date2 - $Date1
"{0} - {1} = {2}" -f $Date2, $Date1, ($Interval.ToString())
# Display individual properties of the resulting TimeSpan object.
" {0,-35} {1,20}" -f "Value of Days Component:", $Interval.Days
" {0,-35} {1,20}" -f "Total Number of Days:", $Interval.TotalDays
" {0,-35} {1,20}" -f "Value of Hours Component:", $Interval.Hours
" {0,-35} {1,20}" -f "Total Number of Hours:", $Interval.TotalHours
" {0,-35} {1,20}" -f "Value of Minutes Component:", $Interval.Minutes
" {0,-35} {1,20}" -f "Total Number of Minutes:", $Interval.TotalMinutes
" {0,-35} {1,20:N0}" -f "Value of Seconds Component:", $Interval.Seconds
" {0,-35} {1,20:N0}" -f "Total Number of Seconds:", $Interval.TotalSeconds
" {0,-35} {1,20:N0}" -f "Value of Milliseconds Component:", $Interval.Milliseconds
" {0,-35} {1,20:N0}" -f "Total Number of Milliseconds:", $Interval.TotalMilliseconds
" {0,-35} {1,20:N0}" -f "Ticks:", $Interval.Ticks
<# This sample produces the following output:
18/08/2010 13:30:30 - 01/01/2010 08:00:15 = 229.05:30:15
Value of Days Component: 229
Total Number of Days: 229.229340277778
Value of Hours Component: 5
Total Number of Hours: 5501.50416666667
Value of Minutes Component: 30
Total Number of Minutes: 330090.25
Value of Seconds Component: 15
Total Number of Seconds: 19,805,415
Value of Milliseconds Component: 0
Total Number of Milliseconds: 19,805,415,000
Ticks: 198,054,150,000,000
#>
備註
欲了解更多關於此 API 的資訊,請參閱 TimeSpan 的補充 API 備註。
建構函式
| 名稱 | Description |
|---|---|
| TimeSpan(Int32, Int32, Int32, Int32, Int32, Int32) |
將結構的新實例 TimeSpan 初始化為指定的天數、小時、分鐘、秒、毫秒及微秒。 |
| TimeSpan(Int32, Int32, Int32, Int32, Int32) |
初始化結構的新實例 TimeSpan ,時間為指定的天、小時、分鐘、秒和毫秒。 |
| TimeSpan(Int32, Int32, Int32, Int32) |
初始化結構的新實例 TimeSpan ,設定在指定的天數、小時、分鐘和秒數。 |
| TimeSpan(Int32, Int32, Int32) |
初始化結構的新實例 TimeSpan ,設定為指定的小時、分鐘和秒數。 |
| TimeSpan(Int64) |
將結構的新實例 TimeSpan 初始化為指定的脈次數。 |
欄位
| 名稱 | Description |
|---|---|
| HoursPerDay |
代表一天的時數。 此欄位是常數。 |
| MaxValue |
代表最大 TimeSpan 值。 此欄位僅供讀取。 |
| MicrosecondsPerDay |
代表一天內的微秒數。 此欄位是常數。 |
| MicrosecondsPerHour |
代表一小時內的微秒數。 此欄位是常數。 |
| MicrosecondsPerMillisecond |
代表1毫秒的微秒數。 此欄位是常數。 |
| MicrosecondsPerMinute |
代表1分鐘內的微秒數。 此欄位是常數。 |
| MicrosecondsPerSecond |
代表1秒內的微秒數。 此欄位是常數。 |
| MillisecondsPerDay |
代表一天內的毫秒數。 此欄位是常數。 |
| MillisecondsPerHour |
代表一小時中的毫秒數。 此欄位是常數。 |
| MillisecondsPerMinute |
代表1分鐘中的毫秒數。 此欄位是常數。 |
| MillisecondsPerSecond |
代表1秒內的毫秒數。 此欄位是常數。 |
| MinutesPerDay |
代表一天內的分鐘數。 此欄位是常數。 |
| MinutesPerHour |
代表一小時的分鐘數。 此欄位是常數。 |
| MinValue |
代表最小 TimeSpan 值。 此欄位僅供讀取。 |
| NanosecondsPerTick |
代表每刻奈秒數。 此欄位是常數。 |
| SecondsPerDay |
代表一天中的秒數。 此欄位是常數。 |
| SecondsPerHour |
代表一小時中的秒數。 此欄位是常數。 |
| SecondsPerMinute |
代表每分鐘的秒數。 此欄位是常數。 |
| TicksPerDay |
代表一天內的蜱蟲數量。 此欄位是常數。 |
| TicksPerHour |
代表一小時內的刻數。 此欄位是常數。 |
| TicksPerMicrosecond |
代表1微秒內的tick數。 此欄位是常數。 |
| TicksPerMillisecond |
代表 1 毫秒內的刻數。 此欄位是常數。 |
| TicksPerMinute |
代表每分鐘的tick數。 此欄位是常數。 |
| TicksPerSecond |
代表每秒的刻數。 |
| Zero |
代表零 TimeSpan 值。 此欄位僅供讀取。 |
屬性
| 名稱 | Description |
|---|---|
| Days |
取得由目前 TimeSpan 結構所代表的時間區間中的天數成分。 |
| Hours |
取得目前 TimeSpan 結構所代表的時間區間中的工時成分。 |
| Microseconds |
取得由當前 TimeSpan 結構所代表的時間區間中微秒分量。 |
| Milliseconds |
得到由當前 TimeSpan 結構所代表的時間區間中的毫秒分量。 |
| Minutes |
取得由目前 TimeSpan 結構表示的時間區間中的分鐘成分。 |
| Nanoseconds |
得到由當前 TimeSpan 結構所代表的時間區間中的奈秒分量。 |
| Seconds |
取得由當前 TimeSpan 結構表示的時間區間中的秒分量。 |
| Ticks |
取得代表當前 TimeSpan 結構值的刻數。 |
| TotalDays |
取得當前 TimeSpan 結構的價值,以整日及分數日表示。 |
| TotalHours |
以全工時和分工時表示的現有 TimeSpan 結構價值。 |
| TotalMicroseconds |
可獲得以整微秒與分微秒表示的當前 TimeSpan 結構值。 |
| TotalMilliseconds |
取得當前 TimeSpan 結構的數值,以整數與分毫秒表示。 |
| TotalMinutes |
取得目前 TimeSpan 結構的數值,以整分鐘和分數分鐘表示。 |
| TotalNanoseconds |
得到以整奈秒和分數奈秒表示的當前 TimeSpan 結構值。 |
| TotalSeconds |
以整秒和分秒表示當前 TimeSpan 結構的值。 |
方法
操作員
| 名稱 | Description |
|---|---|
| Addition(TimeSpan, TimeSpan) |
新增兩個指定的 TimeSpan 實例。 |
| Division(TimeSpan, Double) |
回傳一個新 TimeSpan 物件,其值是將指定 |
| Division(TimeSpan, TimeSpan) |
回傳一個新值,該值是除Double以 |
| Equality(TimeSpan, TimeSpan) |
表示兩個 TimeSpan 實例是否相等。 |
| GreaterThan(TimeSpan, TimeSpan) | |
| GreaterThanOrEqual(TimeSpan, TimeSpan) | |
| Inequality(TimeSpan, TimeSpan) |
表示兩個 TimeSpan 實例是否不相等。 |
| LessThan(TimeSpan, TimeSpan) | |
| LessThanOrEqual(TimeSpan, TimeSpan) | |
| Multiply(Double, TimeSpan) |
回傳一個新 TimeSpan 物件,其值是將指定 |
| Multiply(TimeSpan, Double) |
回傳一個新 TimeSpan 物件,其值是將指定 |
| Subtraction(TimeSpan, TimeSpan) | |
| UnaryNegation(TimeSpan) |
回傳 a TimeSpan ,其值為指定實例的否定值。 |
| UnaryPlus(TimeSpan) |
回傳指定的 實例 TimeSpan。 |
明確介面實作
| 名稱 | Description |
|---|---|
| IComparable.CompareTo(Object) |
將此實例與指定物件比較,並回傳一個整數,表示該實例是短於指定物件、等於或長。 |