TimeSpan Struktura
Definice
Představuje časový interval.Represents a time interval.
public value class TimeSpan : IComparable, IComparable<TimeSpan>, IEquatable<TimeSpan>, IFormattable
public value class TimeSpan : IComparable
public value class TimeSpan : IComparable, IComparable<TimeSpan>, IEquatable<TimeSpan>
public struct TimeSpan : IComparable, IComparable<TimeSpan>, IEquatable<TimeSpan>, IFormattable
[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
[<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
Public Structure TimeSpan
Implements IComparable, IComparable(Of TimeSpan), IEquatable(Of TimeSpan)
- Dědičnost
- Atributy
- Implementuje
Příklady
Následující příklad vytvoří instanci TimeSpan objektu, který představuje rozdíl mezi dvěma kalendářními daty.The following example instantiates a TimeSpan object that represents the difference between two dates. Pak zobrazí TimeSpan vlastnosti objektu.It then displays the TimeSpan object's properties.
// 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.
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
#>
Poznámky
TimeSpanObjekt představuje časový interval (dobu trvání nebo uplynulý čas), který je měřen jako kladný nebo záporný počet dnů, hodin, minut, sekund a zlomků sekund.A TimeSpan object represents a time interval (duration of time or elapsed time) that is measured as a positive or negative number of days, hours, minutes, seconds, and fractions of a second. TimeSpanStrukturu lze také použít k vyjádření denního času, ale pouze v případě, že čas nesouvisí s konkrétním datem.The TimeSpan structure can also be used to represent the time of day, but only if the time is unrelated to a particular date. V opačném DateTime případě DateTimeOffset by měla být místo toho použita struktura nebo.Otherwise, the DateTime or DateTimeOffset structure should be used instead. (Další informace o použití TimeSpan struktury k odrážející denní dobu najdete v tématu Volba mezi DateTime, DateTimeOffset, TimeSpan a TimeZoneInfo.)(For more information about using the TimeSpan structure to reflect the time of day, see Choosing Between DateTime, DateTimeOffset, TimeSpan, and TimeZoneInfo.)
Poznámka
TimeSpanHodnota představuje časový interval a může být vyjádřena jako určitý počet dnů, hodin, minut, sekund a milisekund.A TimeSpan value represents a time interval and can be expressed as a particular number of days, hours, minutes, seconds, and milliseconds. Vzhledem k tomu, že představuje obecný interval bez odkazů na konkrétní počáteční nebo koncový bod, nemůže být vyjádřena v letech a měsících, přičemž obě mají proměnný počet dnů.Because it represents a general interval without reference to a particular start or end point, it cannot be expressed in terms of years and months, both of which have a variable number of days. Liší se od DateTime hodnoty, která představuje datum a čas bez odkazování na konkrétní časové pásmo nebo DateTimeOffset hodnotu, která představuje konkrétní časový okamžik.It differs from a DateTime value, which represents a date and time without reference to a particular time zone, or a DateTimeOffset value, which represents a specific moment of time.
Největší Časová jednotka, kterou TimeSpan struktura používá k měření trvání, je den.The largest unit of time that the TimeSpan structure uses to measure duration is a day. Časové intervaly se měří v počtu dnů konzistence, protože počet dní ve větších jednotkách času, například měsíce a roky, se liší.Time intervals are measured in days for consistency, because the number of days in larger units of time, such as months and years, varies.
Hodnota TimeSpan objektu je počet taktů, které se rovnají reprezentovanému časovému intervalu.The value of a TimeSpan object is the number of ticks that equal the represented time interval. Značka je rovna 100 nanosekundám nebo 1 10-millionth sekundy.A tick is equal to 100 nanoseconds, or one ten-millionth of a second. Hodnota TimeSpan objektu může být v rozsahu od TimeSpan.MinValue do TimeSpan.MaxValue .The value of a TimeSpan object can range from TimeSpan.MinValue to TimeSpan.MaxValue.
Vytvoření instance hodnoty TimeSpanInstantiating a TimeSpan Value
Můžete vytvořit instanci TimeSpan hodnoty několika způsoby:You can instantiate a TimeSpan value in a number of ways:
Voláním jeho implicitního konstruktoru bez parametrů.By calling its implicit parameterless constructor. Tím se vytvoří objekt, jehož hodnota je TimeSpan.Zero , jak ukazuje následující příklad.This creates an object whose value is TimeSpan.Zero, as the following example shows.
TimeSpan interval = new TimeSpan(); Console.WriteLine(interval.Equals(TimeSpan.Zero)); // Displays "True".Dim interval As New TimeSpan() Console.WriteLine(interval.Equals(TimeSpan.Zero)) ' Displays "True".Voláním jednoho z jeho explicitních konstruktorů.By calling one of its explicit constructors. Následující příklad inicializuje TimeSpan hodnotu na zadaný počet hodin, minut a sekund.The following example initializes a TimeSpan value to a specified number of hours, minutes, and seconds.
TimeSpan interval = new TimeSpan(2, 14, 18); Console.WriteLine(interval.ToString()); // Displays "02:14:18".Dim interval As New TimeSpan(2, 14, 18) Console.WriteLine(interval.ToString()) ' Displays "02:14:18".Voláním metody nebo prováděním operace, která vrací TimeSpan hodnotu.By calling a method or performing an operation that returns a TimeSpan value. Například lze vytvořit instanci TimeSpan hodnoty, která představuje interval mezi dvěma hodnotami data a času, jak ukazuje následující příklad.For example, you can instantiate a TimeSpan value that represents the interval between two date and time values, as the following example shows.
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:00Dim 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:00Tímto způsobem lze také inicializovat TimeSpan objekt na hodnotu nulového času, jak ukazuje následující příklad.You can also initialize a TimeSpan object to a zero time value in this way, as the following example shows.
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:00Module 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:00TimeSpan hodnoty jsou vráceny aritmetickými operátory a metodami DateTime , DateTimeOffset a TimeSpan .TimeSpan values are returned by arithmetic operators and methods of the DateTime, DateTimeOffset, and TimeSpan structures.
Pomocí analýzy řetězcové reprezentace TimeSpan hodnoty.By parsing the string representation of a TimeSpan value. Můžete použít Parse TryParse metody a k převodu řetězců, které obsahují časové intervaly na TimeSpan hodnoty.You can use the Parse and TryParse methods to convert strings that contain time intervals to TimeSpan values. Následující příklad používá Parse metodu pro převod pole řetězců na TimeSpan hodnoty.The following example uses the Parse method to convert an array of strings to TimeSpan values.
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'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'Kromě toho můžete definovat přesný formát vstupního řetězce, který má být analyzován a převeden na TimeSpan hodnotu voláním ParseExact TryParseExact metody nebo.In addition, you can define the precise format of the input string to be parsed and converted to a TimeSpan value by calling the ParseExact or TryParseExact method.
Provádění operací na hodnotách TimeSpanPerforming Operations on TimeSpan Values
Můžete přidat a odečíst dobu trvání buď pomocí Addition Subtraction operátorů a, nebo voláním Add Subtract metod a.You can add and subtract time durations either by using the Addition and Subtraction operators, or by calling the Add and Subtract methods. Můžete také porovnat dvě časové doby trvání voláním Compare CompareTo metod, a Equals .You can also compare two time durations by calling the Compare, CompareTo, and Equals methods. TimeSpanStruktura také obsahuje Duration Negate metody a, které převádějí časové intervaly na kladné a záporné hodnoty,The TimeSpan structure also includes the Duration and Negate methods, which convert time intervals to positive and negative values,
Rozsah TimeSpan hodnot je MinValue MaxValue .The range of TimeSpan values is MinValue to MaxValue.
Formátování hodnoty TimeSpanFormatting a TimeSpan Value
TimeSpanHodnota může být reprezentována jako [ - ]d.HH:mm:SS. FF, kde volitelné znaménko minus indikuje záporný časový interval, komponenta d je dny, HH je hodiny, jak se měří na 24hodinové hodiny, mm je minuty, SS sekund a FF je zlomky sekundy.A TimeSpan value can be represented as [-]d.hh:mm:ss.ff, where the optional minus sign indicates a negative time interval, the d component is days, hh is hours as measured on a 24-hour clock, mm is minutes, ss is seconds, and ff is fractions of a second. To znamená, že časový interval se skládá z kladného nebo záporného počtu dnů bez denního času nebo z určitého počtu dní v denním čase nebo pouze v denní době.That is, a time interval consists of a positive or negative number of days without a time of day, or a number of days with a time of day, or only a time of day.
Počínaje .NET Framework 4 TimeSpan Struktura podporuje formátování zohledňující jazykovou verzi prostřednictvím přetížení ToString metody, která převede TimeSpan hodnotu na její řetězcovou reprezentaci.Beginning with the .NET Framework 4, the TimeSpan structure supports culture-sensitive formatting through the overloads of its ToString method, which converts a TimeSpan value to its string representation. Výchozí TimeSpan.ToString() metoda vrací časový interval pomocí neutrálního formátu, který je totožný s jeho návratovou hodnotou v předchozích verzích .NET Framework.The default TimeSpan.ToString() method returns a time interval by using an invariant format that is identical to its return value in previous versions of the .NET Framework. TimeSpan.ToString(String)Přetížení umožňuje zadat formátovací řetězec, který definuje řetězcové vyjádření časového intervalu.The TimeSpan.ToString(String) overload lets you specify a format string that defines the string representation of the time interval. TimeSpan.ToString(String, IFormatProvider)Přetížení umožňuje zadat formátovací řetězec a jazykovou verzi, jejíž konvence formátování slouží k vytvoření řetězcové reprezentace časového intervalu.The TimeSpan.ToString(String, IFormatProvider) overload lets you specify a format string and the culture whose formatting conventions are used to create the string representation of the time interval. TimeSpan podporuje standardní i vlastní řetězce formátu.TimeSpan supports both standard and custom format strings. (Další informace naleznete v tématu Standardní řetězce formátu TimeSpan a Vlastní řetězce formátu TimeSpan.) Pouze standardní formátovací řetězce jsou však závislé na jazykové verzi.(For more information, see Standard TimeSpan Format Strings and Custom TimeSpan Format Strings.) However, only standard format strings are culture-sensitive.
Obnovení staršího formátování TimeSpanRestoring Legacy TimeSpan Formatting
V některých případech kód, který úspěšně formátuje TimeSpan hodnoty v .NET Framework 3,5 a starších verzích, se v .NET Framework 4 nezdaří.In some cases, code that successfully formats TimeSpan values in .NET Framework 3.5 and earlier versions fails in .NET Framework 4. Toto je nejběžnější v kódu, který volá <TimeSpan_LegacyFormatMode> metoda pro naformátování TimeSpan hodnoty pomocí formátovacího řetězce.This is most common in code that calls a <TimeSpan_LegacyFormatMode> element method to format a TimeSpan value with a format string. V následujícím příkladu je úspěšně naformátována TimeSpan hodnota v .NET Framework 3,5 a dřívějších verzích, ale vyvolá výjimku v .NET Framework 4 a novějších verzích.The following example successfully formats a TimeSpan value in .NET Framework 3.5 and earlier versions, but throws an exception in .NET Framework 4 and later versions. Všimněte si, že se pokusí naformátovat TimeSpan hodnotu pomocí nepodporovaného specifikátoru formátu, který se ignoruje v .NET Framework 3,5 a dřívějších verzích.Note that it attempts to format a TimeSpan value by using an unsupported format specifier, which is ignored in .NET Framework 3.5 and earlier versions.
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);
}
}
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
Pokud kód nemůžete upravovat, můžete obnovit starší formátování TimeSpan hodnot jedním z následujících způsobů:If you cannot modify the code, you can restore the legacy formatting of TimeSpan values in one of the following ways:
Vytvořením konfiguračního souboru, který obsahuje prvek<TimeSpan_LegacyFormatMode>.By creating a configuration file that contains the <TimeSpan_LegacyFormatMode> element. Nastavením atributu tohoto elementu
enabledtrueobnovíte starší TimeSpan formátování na základě jednotlivých aplikací.Setting this element'senabledattribute totruerestores legacy TimeSpan formatting on a per-application basis.Nastavením přepínače kompatibility "NetFx40_TimeSpanLegacyFormatMode" při vytváření domény aplikace.By setting the "NetFx40_TimeSpanLegacyFormatMode" compatibility switch when you create an application domain. To umožňuje použití starších verzí TimeSpan formátování na jednotlivých aplikačních doménách.This enables legacy TimeSpan formatting on a per-application-domain basis. Následující příklad vytvoří doménu aplikace, která používá formátování starší verze TimeSpan .The following example creates an application domain that uses legacy TimeSpan formatting.
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"); } }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 ModuleKdyž se v nové doméně aplikace spustí následující kód, vrátí se do chování při formátování starší verze TimeSpan .When the following code executes in the new application domain, it reverts to legacy TimeSpan formatting behavior.
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.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.
Konstruktory
| TimeSpan(Int32, Int32, Int32) |
Inicializuje novou instanci TimeSpan struktury na zadaný počet hodin, minut a sekund.Initializes a new instance of the TimeSpan structure to a specified number of hours, minutes, and seconds. |
| TimeSpan(Int32, Int32, Int32, Int32) |
Inicializuje novou instanci TimeSpan struktury na zadaný počet dnů, hodin, minut a sekund.Initializes a new instance of the TimeSpan structure to a specified number of days, hours, minutes, and seconds. |
| TimeSpan(Int32, Int32, Int32, Int32, Int32) |
Inicializuje novou instanci TimeSpan struktury na zadaný počet dnů, hodin, minut, sekund a milisekund.Initializes a new instance of the TimeSpan structure to a specified number of days, hours, minutes, seconds, and milliseconds. |
| TimeSpan(Int64) |
Inicializuje novou instanci TimeSpan struktury na zadaný počet tiků.Initializes a new instance of the TimeSpan structure to the specified number of ticks. |
Pole
| MaxValue |
Představuje maximální TimeSpan hodnotu.Represents the maximum TimeSpan value. Toto pole je jen ke čtení.This field is read-only. |
| MinValue |
Představuje minimální TimeSpan hodnotu.Represents the minimum TimeSpan value. Toto pole je jen ke čtení.This field is read-only. |
| TicksPerDay |
Představuje počet taktů za 1 den.Represents the number of ticks in 1 day. Toto pole je konstantní.This field is constant. |
| TicksPerHour |
Představuje počet taktů v průběhu 1 hodiny.Represents the number of ticks in 1 hour. Toto pole je konstantní.This field is constant. |
| TicksPerMillisecond |
Představuje počet taktů za 1 milisekundu.Represents the number of ticks in 1 millisecond. Toto pole je konstantní.This field is constant. |
| TicksPerMinute |
Představuje počet taktů za 1 minutu.Represents the number of ticks in 1 minute. Toto pole je konstantní.This field is constant. |
| TicksPerSecond |
Představuje počet taktů v 1 sekundách.Represents the number of ticks in 1 second. |
| Zero |
Představuje nulovou TimeSpan hodnotu.Represents the zero TimeSpan value. Toto pole je jen ke čtení.This field is read-only. |
Vlastnosti
| Days |
Načte složku dní časového intervalu reprezentovaného aktuální TimeSpan strukturou.Gets the days component of the time interval represented by the current TimeSpan structure. |
| Hours |
Načte komponentu hodin časového intervalu reprezentovaného aktuální TimeSpan strukturou.Gets the hours component of the time interval represented by the current TimeSpan structure. |
| Milliseconds |
Načte komponentu milisekund časového intervalu reprezentovaného aktuální TimeSpan strukturou.Gets the milliseconds component of the time interval represented by the current TimeSpan structure. |
| Minutes |
Načte komponentu minut časového intervalu reprezentovaného aktuální TimeSpan strukturou.Gets the minutes component of the time interval represented by the current TimeSpan structure. |
| Seconds |
Získá komponentu sekund časového intervalu reprezentovaného aktuální TimeSpan strukturou.Gets the seconds component of the time interval represented by the current TimeSpan structure. |
| Ticks |
Získá počet tiků, které reprezentují hodnotu aktuální TimeSpan struktury.Gets the number of ticks that represent the value of the current TimeSpan structure. |
| TotalDays |
Získá hodnotu aktuální TimeSpan struktury vyjádřenou v celých a zlomcích dnů.Gets the value of the current TimeSpan structure expressed in whole and fractional days. |
| TotalHours |
Získá hodnotu aktuální TimeSpan struktury vyjádřenou v celých a zlomcích hodin.Gets the value of the current TimeSpan structure expressed in whole and fractional hours. |
| TotalMilliseconds |
Získá hodnotu aktuální TimeSpan struktury vyjádřenou v celých a zlomkových milisekundách.Gets the value of the current TimeSpan structure expressed in whole and fractional milliseconds. |
| TotalMinutes |
Získá hodnotu aktuální TimeSpan struktury vyjádřenou v celých a zlomkových minutách.Gets the value of the current TimeSpan structure expressed in whole and fractional minutes. |
| TotalSeconds |
Získá hodnotu aktuální TimeSpan struktury vyjádřenou celými a zlomky sekund.Gets the value of the current TimeSpan structure expressed in whole and fractional seconds. |
Metody
| Add(TimeSpan) |
Vrátí nový TimeSpan objekt, jehož hodnota je součet zadaného TimeSpan objektu a této instance.Returns a new TimeSpan object whose value is the sum of the specified TimeSpan object and this instance. |
| Compare(TimeSpan, TimeSpan) |
Porovná dvě TimeSpan hodnoty a vrátí celé číslo, které označuje, zda je první hodnota kratší než, rovna nebo větší než druhá hodnota.Compares two TimeSpan values and returns an integer that indicates whether the first value is shorter than, equal to, or longer than the second value. |
| CompareTo(Object) |
Porovná tuto instanci se zadaným objektem a vrátí celé číslo, které označuje, zda je tato instance kratší než, rovna nebo větší než zadaný objekt.Compares this instance to a specified object and returns an integer that indicates whether this instance is shorter than, equal to, or longer than the specified object. |
| CompareTo(TimeSpan) |
Porovná tuto instanci se zadaným TimeSpan objektem a vrátí celé číslo, které označuje, zda je tato instance kratší než, rovna nebo větší než TimeSpan objekt.Compares this instance to a specified TimeSpan object and returns an integer that indicates whether this instance is shorter than, equal to, or longer than the TimeSpan object. |
| Divide(Double) |
Vrátí nový TimeSpan objekt, jehož hodnota je výsledkem dělení této instance a zadaného |
| Divide(TimeSpan) |
Vrátí novou Double hodnotu, která je výsledkem dělení této instance a zadaného |
| Duration() |
Vrátí nový TimeSpan objekt, jehož hodnota je absolutní hodnota aktuálního TimeSpan objektu.Returns a new TimeSpan object whose value is the absolute value of the current TimeSpan object. |
| Equals(Object) |
Vrací hodnotu, která určuje, zda je tato instance rovna zadanému objektu.Returns a value indicating whether this instance is equal to a specified object. |
| Equals(TimeSpan) |
Vrátí hodnotu, která označuje, zda je tato instance rovna zadanému TimeSpan objektu.Returns a value indicating whether this instance is equal to a specified TimeSpan object. |
| Equals(TimeSpan, TimeSpan) |
Vrátí hodnotu, která označuje, zda jsou dvě zadané instance TimeSpan stejné.Returns a value that indicates whether two specified instances of TimeSpan are equal. |
| FromDays(Double) |
Vrátí TimeSpan , který představuje zadaný počet dní, kde je specifikace přesnost na nejbližší milisekundu.Returns a TimeSpan that represents a specified number of days, where the specification is accurate to the nearest millisecond. |
| FromHours(Double) |
Vrátí TimeSpan , který představuje zadaný počet hodin, kde je specifikace přesně na nejbližší milisekundu.Returns a TimeSpan that represents a specified number of hours, where the specification is accurate to the nearest millisecond. |
| FromMilliseconds(Double) |
Vrátí TimeSpan , který představuje zadaný počet milisekund.Returns a TimeSpan that represents a specified number of milliseconds. |
| FromMinutes(Double) |
Vrátí TimeSpan , který představuje zadaný počet minut, kde je specifikace přesně na nejbližší milisekundu.Returns a TimeSpan that represents a specified number of minutes, where the specification is accurate to the nearest millisecond. |
| FromSeconds(Double) |
Vrátí TimeSpan , který představuje zadaný počet sekund, kde je specifikace přesně na nejbližší milisekundu.Returns a TimeSpan that represents a specified number of seconds, where the specification is accurate to the nearest millisecond. |
| FromTicks(Int64) |
Vrátí TimeSpan , který představuje určený čas, kde je specifikace v jednotkách taktů.Returns a TimeSpan that represents a specified time, where the specification is in units of ticks. |
| GetHashCode() |
Vrátí kód hash této instance.Returns a hash code for this instance. |
| Multiply(Double) |
Vrátí nový TimeSpan objekt, jehož hodnota je výsledkem násobení této instance a zadaného |
| Negate() |
Vrátí nový TimeSpan objekt, jehož hodnota je hodnota negace této instance.Returns a new TimeSpan object whose value is the negated value of this instance. |
| Parse(ReadOnlySpan<Char>, IFormatProvider) |
Převede reprezentace rozsahu časového intervalu na jeho TimeSpan ekvivalent pomocí zadaných informací o formátu specifické jazykové verze.Converts the span representation of a time interval to its TimeSpan equivalent by using the specified culture-specific format information. |
| Parse(String) |
Převede řetězcové vyjádření časového intervalu na jeho TimeSpan ekvivalent.Converts the string representation of a time interval to its TimeSpan equivalent. |
| Parse(String, IFormatProvider) |
Převede řetězcové vyjádření časového intervalu na jeho TimeSpan ekvivalent pomocí zadaných informací o formátu specifické jazykové verze.Converts the string representation of a time interval to its TimeSpan equivalent by using the specified culture-specific format information. |
| ParseExact(ReadOnlySpan<Char>, ReadOnlySpan<Char>, IFormatProvider, TimeSpanStyles) |
Převede rozsah znaků časového intervalu na jeho TimeSpan ekvivalent pomocí zadaného formátu a informací o formátu specifické jazykové verze.Converts the char span of a time interval to its TimeSpan equivalent by using the specified format and culture-specific format information. Formát řetězcového vyjádření musí přesně odpovídat určenému formátu.The format of the string representation must match the specified format exactly. |
| ParseExact(ReadOnlySpan<Char>, String[], IFormatProvider, TimeSpanStyles) |
Převede řetězcové vyjádření časového intervalu na jeho TimeSpan ekvivalent pomocí zadaných formátů, informací o formátu specifických pro jazykovou verzi a stylů.Converts the string representation of a time interval to its TimeSpan equivalent by using the specified formats, culture-specific format information, and styles. Formát řetězcového vyjádření musí přesně odpovídat jednomu z určených formátů.The format of the string representation must match one of the specified formats exactly. |
| ParseExact(String, String, IFormatProvider) |
Převede řetězcové vyjádření časového intervalu na jeho TimeSpan ekvivalent pomocí zadaného formátu a informací o formátu určených pro jazykovou verzi.Converts the string representation of a time interval to its TimeSpan equivalent by using the specified format and culture-specific format information. Formát řetězcového vyjádření musí přesně odpovídat určenému formátu.The format of the string representation must match the specified format exactly. |
| ParseExact(String, String, IFormatProvider, TimeSpanStyles) |
Převede řetězcové vyjádření časového intervalu na jeho TimeSpan ekvivalent pomocí zadaného formátu, informací o formátu specifických pro jazykovou verzi a stylů.Converts the string representation of a time interval to its TimeSpan equivalent by using the specified format, culture-specific format information, and styles. Formát řetězcového vyjádření musí přesně odpovídat určenému formátu.The format of the string representation must match the specified format exactly. |
| ParseExact(String, String[], IFormatProvider) |
Převede řetězcové vyjádření časového intervalu na jeho TimeSpan ekvivalent pomocí zadaného pole formátovacích řetězců a informací o formátu specifických pro jazykovou verzi.Converts the string representation of a time interval to its TimeSpan equivalent by using the specified array of format strings and culture-specific format information. Formát řetězcového vyjádření musí přesně odpovídat jednomu z určených formátů.The format of the string representation must match one of the specified formats exactly. |
| ParseExact(String, String[], IFormatProvider, TimeSpanStyles) |
Převede řetězcové vyjádření časového intervalu na jeho TimeSpan ekvivalent pomocí zadaných formátů, informací o formátu specifických pro jazykovou verzi a stylů.Converts the string representation of a time interval to its TimeSpan equivalent by using the specified formats, culture-specific format information, and styles. Formát řetězcového vyjádření musí přesně odpovídat jednomu z určených formátů.The format of the string representation must match one of the specified formats exactly. |
| Subtract(TimeSpan) |
Vrátí nový TimeSpan objekt, jehož hodnota je rozdíl mezi zadaným TimeSpan objektem a touto instancí.Returns a new TimeSpan object whose value is the difference between the specified TimeSpan object and this instance. |
| ToString() |
Převede hodnotu aktuálního TimeSpan objektu na odpovídající řetězcovou reprezentaci.Converts the value of the current TimeSpan object to its equivalent string representation. |
| ToString(String) |
Převede hodnotu aktuálního TimeSpan objektu na odpovídající řetězcovou reprezentaci pomocí zadaného formátu.Converts the value of the current TimeSpan object to its equivalent string representation by using the specified format. |
| ToString(String, IFormatProvider) |
Převede hodnotu aktuálního TimeSpan objektu na odpovídající řetězcovou reprezentaci pomocí zadaného formátu a informací o formátování specifických pro jazykovou verzi.Converts the value of the current TimeSpan object to its equivalent string representation by using the specified format and culture-specific formatting information. |
| TryFormat(Span<Char>, Int32, ReadOnlySpan<Char>, IFormatProvider) |
Pokusí se naformátovat hodnotu aktuální instance čísla TimeSpan do zadaného rozsahu znaků.Tries to format the value of the current timespan number instance into the provided span of characters. |
| TryParse(ReadOnlySpan<Char>, IFormatProvider, TimeSpan) |
Převede reprezentace rozsahu časového intervalu na jeho TimeSpan ekvivalent pomocí zadaných informací o formátování specifických pro jazykovou verzi a vrátí hodnotu, která označuje, zda byl převod úspěšný.Converts the span representation of a time interval to its TimeSpan equivalent by using the specified culture-specific formatting information, and returns a value that indicates whether the conversion succeeded. |
| TryParse(ReadOnlySpan<Char>, TimeSpan) |
Převede reprezentace rozsahu časového intervalu na jeho TimeSpan ekvivalent a vrátí hodnotu, která označuje, zda převod proběhl úspěšně.Converts the span representation of a time interval to its TimeSpan equivalent and returns a value that indicates whether the conversion succeeded. |
| TryParse(String, IFormatProvider, TimeSpan) |
Převede řetězcové vyjádření časového intervalu na jeho TimeSpan ekvivalent pomocí zadaných informací o formátování specifických pro jazykovou verzi a vrátí hodnotu, která označuje, zda převod proběhl úspěšně.Converts the string representation of a time interval to its TimeSpan equivalent by using the specified culture-specific formatting information, and returns a value that indicates whether the conversion succeeded. |
| TryParse(String, TimeSpan) |
Převede řetězcové vyjádření časového intervalu na jeho TimeSpan ekvivalent a vrátí hodnotu, která označuje, zda převod proběhl úspěšně.Converts the string representation of a time interval to its TimeSpan equivalent and returns a value that indicates whether the conversion succeeded. |
| TryParseExact(ReadOnlySpan<Char>, ReadOnlySpan<Char>, IFormatProvider, TimeSpan) |
Převede určený rozsah reprezentace časového intervalu na jeho TimeSpan ekvivalent pomocí zadaného formátu a informací o formátu specifické jazykové verze.Converts the specified span representation of a time interval to its TimeSpan equivalent by using the specified format and culture-specific format information. Formát řetězcového vyjádření musí přesně odpovídat určenému formátu.The format of the string representation must match the specified format exactly. |
| TryParseExact(ReadOnlySpan<Char>, ReadOnlySpan<Char>, IFormatProvider, TimeSpanStyles, TimeSpan) |
Převede určený rozsah reprezentace časového intervalu na jeho TimeSpan ekvivalent pomocí zadaného formátu, jazykové informace specifické pro jazykovou verzi a stylů a vrátí hodnotu, která označuje, zda byl převod úspěšný.Converts the specified span representation of a time interval to its TimeSpan equivalent by using the specified format, culture-specific format information, and styles, and returns a value that indicates whether the conversion succeeded. Formát řetězcového vyjádření musí přesně odpovídat určenému formátu.The format of the string representation must match the specified format exactly. |
| TryParseExact(ReadOnlySpan<Char>, String[], IFormatProvider, TimeSpan) |
Převede určený rozsah reprezentace časového intervalu na jeho TimeSpan ekvivalent pomocí zadaných formátů a informací o formátu specifické jazykové verze.Converts the specified span representation of a time interval to its TimeSpan equivalent by using the specified formats and culture-specific format information. Formát řetězcového vyjádření musí přesně odpovídat jednomu z určených formátů.The format of the string representation must match one of the specified formats exactly. |
| TryParseExact(ReadOnlySpan<Char>, String[], IFormatProvider, TimeSpanStyles, TimeSpan) |
Převede určený rozsah reprezentace časového intervalu na jeho TimeSpan ekvivalent pomocí zadaných formátů, informací o formátu a stylů specifických pro jazykovou verzi.Converts the specified span representation of a time interval to its TimeSpan equivalent by using the specified formats, culture-specific format information and styles. Formát řetězcového vyjádření musí přesně odpovídat jednomu z určených formátů.The format of the string representation must match one of the specified formats exactly. |
| TryParseExact(String, String, IFormatProvider, TimeSpan) |
Převede řetězcové vyjádření časového intervalu na jeho TimeSpan ekvivalent pomocí zadaného formátu a informací o formátu určených pro jazykovou verzi.Converts the string representation of a time interval to its TimeSpan equivalent by using the specified format and culture-specific format information. Formát řetězcového vyjádření musí přesně odpovídat určenému formátu.The format of the string representation must match the specified format exactly. |
| TryParseExact(String, String, IFormatProvider, TimeSpanStyles, TimeSpan) |
Převede řetězcové vyjádření časového intervalu na jeho TimeSpan ekvivalent pomocí zadaného formátu, informací o formátu a stylů specifických pro jazykovou verzi.Converts the string representation of a time interval to its TimeSpan equivalent by using the specified format, culture-specific format information and styles. Formát řetězcového vyjádření musí přesně odpovídat určenému formátu.The format of the string representation must match the specified format exactly. |
| TryParseExact(String, String[], IFormatProvider, TimeSpan) |
Převede zadané řetězcové vyjádření časového intervalu na jeho TimeSpan ekvivalent pomocí zadaných formátů a informací o formátu specifické jazykové verze.Converts the specified string representation of a time interval to its TimeSpan equivalent by using the specified formats and culture-specific format information. Formát řetězcového vyjádření musí přesně odpovídat jednomu z určených formátů.The format of the string representation must match one of the specified formats exactly. |
| TryParseExact(String, String[], IFormatProvider, TimeSpanStyles, TimeSpan) |
Převede zadané řetězcové vyjádření časového intervalu na jeho TimeSpan ekvivalent pomocí zadaných formátů, informací o formátu a stylů specifických pro jazykovou verzi.Converts the specified string representation of a time interval to its TimeSpan equivalent by using the specified formats, culture-specific format information and styles. Formát řetězcového vyjádření musí přesně odpovídat jednomu z určených formátů.The format of the string representation must match one of the specified formats exactly. |
Operátory
| Addition(TimeSpan, TimeSpan) |
Přidá dvě zadané TimeSpan instance.Adds two specified TimeSpan instances. |
| Division(TimeSpan, Double) |
Vrátí nový TimeSpan objekt, jehož hodnota je výsledkem dělení |
| Division(TimeSpan, TimeSpan) |
Vrací novou Double hodnotu, která je výsledkem dělení |
| Equality(TimeSpan, TimeSpan) |
Určuje, zda TimeSpan jsou dvě instance stejné.Indicates whether two TimeSpan instances are equal. |
| GreaterThan(TimeSpan, TimeSpan) |
Uvádí, zda TimeSpan je zadaný je větší než jiný zadaný TimeSpan .Indicates whether a specified TimeSpan is greater than another specified TimeSpan. |
| GreaterThanOrEqual(TimeSpan, TimeSpan) |
Označuje, zda TimeSpan je zadaný je větší nebo roven jiné zadané hodnotě TimeSpan .Indicates whether a specified TimeSpan is greater than or equal to another specified TimeSpan. |
| Inequality(TimeSpan, TimeSpan) |
Určuje, zda TimeSpan se neshodují dvě instance.Indicates whether two TimeSpan instances are not equal. |
| LessThan(TimeSpan, TimeSpan) |
Označuje, zda TimeSpan je zadaný méně než jiný zadaný TimeSpan .Indicates whether a specified TimeSpan is less than another specified TimeSpan. |
| LessThanOrEqual(TimeSpan, TimeSpan) |
Označuje, zda TimeSpan je zadaný menší nebo roven jiné zadané hodnotě TimeSpan .Indicates whether a specified TimeSpan is less than or equal to another specified TimeSpan. |
| Multiply(Double, TimeSpan) |
Vrátí nový TimeSpan objekt, jehož hodnota je výsledkem vynásobení zadaného |
| Multiply(TimeSpan, Double) |
Vrátí nový TimeSpan objekt, jehož hodnota je výsledkem vynásobení zadané |
| Subtraction(TimeSpan, TimeSpan) |
Odečte zadaný TimeSpan od jiného zadaného TimeSpan .Subtracts a specified TimeSpan from another specified TimeSpan. |
| UnaryNegation(TimeSpan) |
Vrátí, TimeSpan jehož hodnota je hodnota negace zadané instance.Returns a TimeSpan whose value is the negated value of the specified instance. |
| UnaryPlus(TimeSpan) |
Vrátí zadanou instanci TimeSpan .Returns the specified instance of TimeSpan. |
Explicitní implementace rozhraní
| IComparable.CompareTo(Object) |
Porovná tuto instanci se zadaným objektem a vrátí celé číslo, které označuje, zda je tato instance kratší než, rovna nebo větší než zadaný objekt.Compares this instance to a specified object and returns an integer that indicates whether this instance is shorter than, equal to, or longer than the specified object. |