TimeSpan 結構
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
表示時間間隔。
public value class TimeSpan : IComparable, IComparable<TimeSpan>, IEquatable<TimeSpan>, IFormattable
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>, 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 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), 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
#>
備註
TimeSpan物件代表時間間隔 (時間間隔或經過的時間) ,以正數或負數天數、小時、分鐘、秒數和秒數來測量。 結構 TimeSpan 也可以用來表示一天的時間,但只有在時間與特定日期無關時才使用。 否則, DateTime 應該改用 或 DateTimeOffset 結構。 (如需使用 TimeSpan 結構來反映當日時間的詳細資訊,請參閱 選擇 DateTime、DateTimeOffset、TimeSpan 和 TimeZoneInfo.)
注意
值 TimeSpan 代表時間間隔,而且可以表示為特定天數、小時、分鐘、秒和毫秒。 因為它代表一般間隔,而不參考特定起點或結束點,因此不能以年份和月份表示,這兩者都有可變天數。 它與值不同 DateTime ,其代表日期和時間,而不參考特定時區,或 DateTimeOffset 代表特定時間點的值。
結構用來測量持續時間的最大時間 TimeSpan 單位是一天。 時間間隔是以天數為單位來測量一致性,因為以較大的時間單位為單位的天數,例如月份和年份,會有所不同。
物件的值 TimeSpan 是等於所表示時間間隔的刻度數目。 刻度等於 100 奈秒,或每秒 100 萬分之一。 物件的值 TimeSpan 可以範圍從 到 TimeSpan.MinValueTimeSpan.MaxValue 。
具現化 TimeSpan 值
您可以透過數種方式具現化 TimeSpan 值:
藉由呼叫其隱含無參數建構函式。 這會建立值為 TimeSpan.Zero 的物件,如下列範例所示。
TimeSpan interval = new TimeSpan(); Console.WriteLine(interval.Equals(TimeSpan.Zero)); // Displays "True".
let interval = TimeSpan() printfn $"{interval.Equals TimeSpan.Zero}" // Displays "True".
Dim interval As New TimeSpan() Console.WriteLine(interval.Equals(TimeSpan.Zero)) ' Displays "True".
藉由呼叫其中一個明確的建構函式。 下列範例會將 TimeSpan 值初始化為指定的時數、分鐘和秒數。
TimeSpan interval = new TimeSpan(2, 14, 18); Console.WriteLine(interval.ToString()); // Displays "02:14:18".
let interval = TimeSpan(2, 14, 18) printfn $"{interval}" // Displays "02:14:18".
Dim interval As New TimeSpan(2, 14, 18) Console.WriteLine(interval.ToString()) ' Displays "02:14:18".
呼叫 方法或執行傳回 TimeSpan 值的作業。 例如,您可以具現化代表兩個 TimeSpan 日期和時間值之間的間隔的值,如下列範例所示。
DateTime departure = new DateTime(2010, 6, 12, 18, 32, 0); DateTime arrival = new DateTime(2010, 6, 13, 22, 47, 0); TimeSpan travelTime = arrival - departure; Console.WriteLine("{0} - {1} = {2}", arrival, departure, travelTime); // The example displays the following output: // 6/13/2010 10:47:00 PM - 6/12/2010 6:32:00 PM = 1.04:15:00
let departure = DateTime(2010, 6, 12, 18, 32, 0) let arrival = DateTime(2010, 6, 13, 22, 47, 0) let travelTime = arrival - departure printfn $"{arrival} - {departure} = {travelTime}" // The example displays the following output: // 6/13/2010 10:47:00 PM - 6/12/2010 6:32:00 PM = 1.04:15:00
Dim departure As DateTime = #06/12/2010 6:32PM# Dim arrival As DateTime = #06/13/2010 10:47PM# Dim travelTime As TimeSpan = arrival - departure Console.WriteLine("{0} - {1} = {2}", arrival, departure, travelTime) ' The example displays the following output: ' 6/13/2010 10:47:00 PM - 6/12/2010 6:32:00 PM = 1.04:15:00
您也可以以這種方式將 物件初始化 TimeSpan 為零時間值,如下列範例所示。
Random rnd = new Random(); TimeSpan timeSpent = TimeSpan.Zero; timeSpent += GetTimeBeforeLunch(); timeSpent += GetTimeAfterLunch(); Console.WriteLine("Total time: {0}", timeSpent); TimeSpan GetTimeBeforeLunch() { return new TimeSpan(rnd.Next(3, 6), 0, 0); } TimeSpan GetTimeAfterLunch() { return new TimeSpan(rnd.Next(3, 6), 0, 0); } // The example displays output like the following: // Total time: 08:00:00
open System let rnd = Random() let getTimeBeforeLunch () = TimeSpan(rnd.Next(3, 6), 0, 0) let getTimeAfterLunch() = TimeSpan(rnd.Next(3, 6), 0, 0) do let timeSpent = TimeSpan.Zero let timeSpent = timeSpent + getTimeBeforeLunch () let timeSpent = timeSpent + getTimeAfterLunch () printfn $"Total time: {timeSpent}" // The example displays output like the following: // Total time: 08:00:00
Module Example Dim rnd As New Random() Public Sub Main() Dim timeSpent As TimeSpan = TimeSpan.Zero timeSpent += GetTimeBeforeLunch() timeSpent += GetTimeAfterLunch() Console.WriteLine("Total time: {0}", timeSpent) End Sub Private Function GetTimeBeforeLunch() As TimeSpan Return New TimeSpan(rnd.Next(3, 6), 0, 0) End Function Private Function GetTimeAfterLunch() As TimeSpan Return New TimeSpan(rnd.Next(3, 6), 0, 0) End Function End Module ' The example displays output like the following: ' Total time: 08:00:00
TimeSpan值是由 、 DateTimeOffset 和 結構的算術運算子和 TimeSpan 方法 DateTime 傳回。
藉由剖析值的字串表示 TimeSpan 。 您可以使用 Parse 和 TryParse 方法來將包含時間間隔的 TimeSpan 字串轉換成值。 下列範例會使用 方法, Parse 將字串 TimeSpan 陣列轉換成值。
string[] values = { "12", "31.", "5.8:32:16", "12:12:15.95", ".12"}; foreach (string value in values) { try { TimeSpan ts = TimeSpan.Parse(value); Console.WriteLine("'{0}' --> {1}", value, ts); } catch (FormatException) { Console.WriteLine("Unable to parse '{0}'", value); } catch (OverflowException) { Console.WriteLine("'{0}' is outside the range of a TimeSpan.", value); } } // The example displays the following output: // '12' --> 12.00:00:00 // Unable to parse '31.' // '5.8:32:16' --> 5.08:32:16 // '12:12:15.95' --> 12:12:15.9500000 // Unable to parse '.12'
let values = [| "12"; "31."; "5.8:32:16"; "12:12:15.95"; ".12" |] for value in values do try let ts = TimeSpan.Parse value printfn $"'{value}' --> {ts}" with | :? FormatException -> printfn $"Unable to parse '{value}'" | :? OverflowException -> printfn $"'{value}' is outside the range of a TimeSpan." // The example displays the following output: // '12' --> 12.00:00:00 // Unable to parse '31.' // '5.8:32:16' --> 5.08:32:16 // '12:12:15.95' --> 12:12:15.9500000 // Unable to parse '.12'
Dim values() As String = { "12", "31.", "5.8:32:16", "12:12:15.95", ".12"} For Each value As String In values Try Dim ts As TimeSpan = TimeSpan.Parse(value) Console.WriteLine("'{0}' --> {1}", value, ts) Catch e As FormatException Console.WriteLine("Unable to parse '{0}'", value) Catch e As OverflowException Console.WriteLine("'{0}' is outside the range of a TimeSpan.", value) End Try Next ' The example displays the following output: ' '12' --> 12.00:00:00 ' Unable to parse '31.' ' '5.8:32:16' --> 5.08:32:16 ' '12:12:15.95' --> 12:12:15.9500000 ' Unable to parse '.12'
此外,您可以呼叫 ParseExact 或 TryParseExact 方法來定義要剖析並轉換成 TimeSpan 值的輸入字串精確格式。
在 TimeSpan 值上執行作業
您可以使用 和 Subtraction 運算子,或呼叫 Add 和 Subtract 方法來加入和減去時間持續時間 Addition 。 您也可以藉由呼叫 Compare 、 CompareTo 和 Equals 方法來比較兩個時間持續時間。 結構 TimeSpan 也包含 Duration 和 Negate 方法,可將時間間隔轉換為正值和負值。
值的範圍 TimeSpan 是 MinValueMaxValue 。
格式化 TimeSpan 值
TimeSpan值可以表示為 [ - ]d。hh:mm:ss。ff,其中選擇性減號表示負數時間間隔,d元件為天,hh是以 24 小時制測量的小時,mm為分鐘數,ss為秒數,ff為秒數。 也就是說,時間間隔是由沒有一天時間的正數或負數天所組成,或是包含一天的時間,或只包含一天的時間。
從 .NET Framework 4 開始, TimeSpan 結構透過方法 ToString 的多載支援區分文化特性的格式,以將值轉換成 TimeSpan 其字串表示。 預設 TimeSpan.ToString() 方法會使用與舊版.NET Framework中傳回值相同的不變異格式,傳回時間間隔。 多 TimeSpan.ToString(String) 載可讓您指定格式字串,以定義時間間隔的字串表示。 多 TimeSpan.ToString(String, IFormatProvider) 載可讓您指定格式字串,以及其格式慣例用來建立時間間隔字串表示的文化特性。 TimeSpan 支援標準和自訂格式字串。 (如需詳細資訊,請參閱 標準 TimeSpan 格式字串 和 自訂 TimeSpan 格式字串。) 不過,只有標準格式字串會區分文化特性。
還原舊版 TimeSpan 格式設定
在某些情況下,.NET Framework 3.5 和舊版中成功格式化 TimeSpan 值的程式碼會在 .NET Framework 4 中失敗。 這在程式碼中最常呼叫< TimeSpan_LegacyFormatMode > 元素方法,以格式化 TimeSpan 格式字串的值。 下列範例成功格式化 TimeSpan .NET Framework 3.5 和舊版中的值,但在 .NET Framework 4 和更新版本中擲回例外狀況。 請注意,它會嘗試使用不支援的格式規範來格式化 TimeSpan 值,這會在 .NET Framework 3.5 和舊版中忽略。
ShowFormattingCode();
// Output from .NET Framework 3.5 and earlier versions:
// 12:30:45
// Output from .NET Framework 4:
// Invalid Format
Console.WriteLine("---");
ShowParsingCode();
// Output:
// 000000006 --> 6.00:00:00
void ShowFormattingCode()
{
TimeSpan interval = new TimeSpan(12, 30, 45);
string output;
try {
output = String.Format("{0:r}", interval);
}
catch (FormatException) {
output = "Invalid Format";
}
Console.WriteLine(output);
}
void ShowParsingCode()
{
string value = "000000006";
try {
TimeSpan interval = TimeSpan.Parse(value);
Console.WriteLine("{0} --> {1}", value, interval);
}
catch (FormatException) {
Console.WriteLine("{0}: Bad Format", value);
}
catch (OverflowException) {
Console.WriteLine("{0}: Overflow", value);
}
}
let showFormattingCode () =
let interval = TimeSpan(12, 30, 45)
try
$"{interval:r}"
with :? FormatException ->
"Invalid Format"
|> printfn "%s"
let showParsingCode () =
let value = "000000006"
try
let interval = TimeSpan.Parse value
printfn $"{value} --> {interval}"
with
| :? FormatException ->
printfn $"{value}: Bad Format"
| :? OverflowException ->
printfn $"{value}: Overflow"
showFormattingCode ()
// Output from .NET Framework 3.5 and earlier versions:
// 12:30:45
// Output from .NET Framework 4:
// Invalid Format
printfn "---"
showParsingCode ()
// Output:
// 000000006 --> 6.00:00:00
Dim interval As New TimeSpan(12, 30, 45)
Dim output As String
Try
output = String.Format("{0:r}", interval)
Catch e As FormatException
output = "Invalid Format"
End Try
Console.WriteLine(output)
' Output from .NET Framework 3.5 and earlier versions:
' 12:30:45
' Output from .NET Framework 4:
' Invalid Format
如果您無法修改程式碼,您可以透過下列其中一種方式還原值的舊版格式 TimeSpan 設定:
建立包含< TimeSpan_LegacyFormatMode > 專案的組態檔。 將這個專案的
enabled
屬性設定為true
以個別應用程式為基礎還原舊版 TimeSpan 格式設定。當您建立應用程式域時,藉由設定 「NetFx40_TimeSpanLegacyFormatMode」相容性參數。 這會根據每個應用程式域啟用舊版 TimeSpan 格式設定。 下列範例會建立使用舊版 TimeSpan 格式的應用程式域。
using System; public class Example { public static void Main() { AppDomainSetup appSetup = new AppDomainSetup(); appSetup.SetCompatibilitySwitches( new string[] { "NetFx40_TimeSpanLegacyFormatMode" } ); AppDomain legacyDomain = AppDomain.CreateDomain("legacyDomain", null, appSetup); legacyDomain.ExecuteAssembly("ShowTimeSpan.exe"); } }
open System let appSetup = AppDomainSetup() appSetup.SetCompatibilitySwitches [| "NetFx40_TimeSpanLegacyFormatMode" |] let legacyDomain = AppDomain.CreateDomain("legacyDomain", null, appSetup) legacyDomain.ExecuteAssembly "ShowTimeSpan.exe" |> ignore
Module Example Public Sub Main() Dim appSetup As New AppDomainSetup() appSetup.SetCompatibilitySwitches( { "NetFx40_TimeSpanLegacyFormatMode" } ) Dim legacyDomain As AppDomain = AppDomain.CreateDomain("legacyDomain", Nothing, appSetup) legacyDomain.ExecuteAssembly("ShowTimeSpan.exe") End Sub End Module
當下列程式碼在新的應用程式域中執行時,它會還原為舊版 TimeSpan 格式設定行為。
using System; public class Example { public static void Main() { TimeSpan interval = DateTime.Now - DateTime.Now.Date; string msg = String.Format("Elapsed Time Today: {0:d} hours.", interval); Console.WriteLine(msg); } } // The example displays the following output: // Elapsed Time Today: 01:40:52.2524662 hours.
open System let interval = DateTime.Now - DateTime.Now.Date printfn $"Elapsed Time Today: {interval:d} hours." // The example displays the following output: // Elapsed Time Today: 01:40:52.2524662 hours.
Module Example Public Sub Main() Dim interval As TimeSpan = Date.Now - Date.Now.Date Dim msg As String = String.Format("Elapsed Time Today: {0:d} hours.", interval) Console.WriteLine(msg) End Sub End Module ' The example displays output like the following: ' Elapsed Time Today: 01:40:52.2524662 hours.
建構函式
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 結構的新執行個體初始化為刻度的指定數目。 |
欄位
MaxValue |
代表最大 TimeSpan 值。 此欄位為唯讀。 |
MinValue |
代表最小 TimeSpan 值。 此欄位為唯讀。 |
NanosecondsPerTick |
表示每個刻度數的奈秒數。 這個欄位為常數。 |
TicksPerDay |
表示 1 天中的刻度數。 這個欄位為常數。 |
TicksPerHour |
表示 1 小時中的刻度數。 這個欄位為常數。 |
TicksPerMicrosecond |
表示以 1 毫秒為單位的刻度數。 這個欄位為常數。 |
TicksPerMillisecond |
表示 1 毫秒中的刻度數。 這個欄位為常數。 |
TicksPerMinute |
表示 1 分鐘中的刻度數。 這個欄位為常數。 |
TicksPerSecond |
表示 1 秒中的刻度數。 |
Zero |
代表零 TimeSpan 值。 此欄位為唯讀。 |
屬性
Days |
取得目前 TimeSpan 結構所表示之時間間隔的天數元件。 |
Hours |
取得目前 TimeSpan 結構所表示之時間間隔的時數元件。 |
Microseconds |
取得目前 TimeSpan 結構所表示之時間間隔的微秒元件。 |
Milliseconds |
取得目前 TimeSpan 結構所表示之時間間隔的毫秒數元件。 |
Minutes |
取得目前 TimeSpan 結構所表示之時間間隔的分鐘數元件。 |
Nanoseconds |
取得目前 TimeSpan 結構所表示之時間間隔的 nanoseconds 元件。 |
Seconds |
取得目前 TimeSpan 結構所表示之時間間隔的秒數元件。 |
Ticks |
取得刻度數,表示目前 TimeSpan 結構的值。 |
TotalDays |
取得目前 TimeSpan 結構的值,這個值以整數和小數的天數表示。 |
TotalHours |
取得目前 TimeSpan 結構的值,這個值以整數和小數的時數表示。 |
TotalMicroseconds |
取得以整數和小數微碼錶示的目前 TimeSpan 結構值。 |
TotalMilliseconds |
取得目前 TimeSpan 結構的值,這個值以整數和小數的毫秒數表示。 |
TotalMinutes |
取得目前 TimeSpan 結構的值,這個值以整數和小數的分鐘數表示。 |
TotalNanoseconds |
取得以整數和小數奈碼錶示的目前 TimeSpan 結構值。 |
TotalSeconds |
取得目前 TimeSpan 結構的值,這個值以整數和小數的秒數表示。 |
方法
運算子
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) |
傳回 TimeSpan,其值是指定執行個體的相反值。 |
UnaryPlus(TimeSpan) |
傳回 TimeSpan 的指定執行個體。 |
明確介面實作
IComparable.CompareTo(Object) |
比較這個執行個體和指定的物件,並傳回一個整數,表示這個執行個體短於、等於或長於指定的物件。 |