|
Il presente articolo è stato tradotto automaticamente. Passare il puntatore sulle frasi nell'articolo per visualizzare il testo originale. Ulteriori informazioni.
|
Traduzione
Originale
|
Struttura TimeSpan
Spazio dei nomi: System
Assembly: mscorlib (in mscorlib.dll)
Il tipo TimeSpan espone i seguenti membri.
| Nome | Descrizione | |
|---|---|---|
![]() ![]() ![]() ![]() | TimeSpan(Int64) | |
![]() ![]() ![]() ![]() | TimeSpan(Int32, Int32, Int32) | |
![]() ![]() ![]() ![]() | TimeSpan(Int32, Int32, Int32, Int32) | |
![]() ![]() ![]() ![]() | TimeSpan(Int32, Int32, Int32, Int32, Int32) |
| Nome | Descrizione | |
|---|---|---|
![]() ![]() ![]() ![]() | Days | |
![]() ![]() ![]() ![]() | Hours | |
![]() ![]() ![]() ![]() | Milliseconds | |
![]() ![]() ![]() ![]() | Minutes | |
![]() ![]() ![]() ![]() | Seconds | |
![]() ![]() ![]() ![]() | Ticks | |
![]() ![]() ![]() ![]() | TotalDays | |
![]() ![]() ![]() ![]() | TotalHours | |
![]() ![]() ![]() ![]() | TotalMilliseconds | |
![]() ![]() ![]() ![]() | TotalMinutes | |
![]() ![]() ![]() ![]() | TotalSeconds |
| Nome | Descrizione | |
|---|---|---|
![]() ![]() ![]() ![]() | Add | |
![]() ![]() ![]() ![]() ![]() | Compare | |
![]() ![]() | CompareTo(Object) | |
![]() ![]() ![]() ![]() | CompareTo(TimeSpan) | |
![]() ![]() ![]() ![]() | Duration | |
![]() ![]() ![]() ![]() | Equals(Object) | |
![]() ![]() ![]() ![]() | Equals(TimeSpan) | |
![]() ![]() ![]() ![]() ![]() | Equals(TimeSpan, TimeSpan) | |
![]() ![]() ![]() ![]() ![]() | FromDays | |
![]() ![]() ![]() ![]() ![]() | FromHours | |
![]() ![]() ![]() ![]() ![]() | FromMilliseconds | |
![]() ![]() ![]() ![]() ![]() | FromMinutes | |
![]() ![]() ![]() ![]() ![]() | FromSeconds | |
![]() ![]() ![]() ![]() ![]() | FromTicks | |
![]() ![]() ![]() ![]() | GetHashCode | |
![]() ![]() ![]() ![]() | GetType | |
![]() ![]() ![]() ![]() | Negate | |
![]() ![]() ![]() ![]() ![]() | Parse(String) | |
![]() ![]() ![]() ![]() | Parse(String, IFormatProvider) | |
![]() ![]() ![]() ![]() | ParseExact(String, String, IFormatProvider) | |
![]() ![]() ![]() ![]() | ParseExact(String, String[], IFormatProvider) | |
![]() ![]() ![]() ![]() | ParseExact(String, String, IFormatProvider, TimeSpanStyles) | |
![]() ![]() ![]() ![]() | ParseExact(String, String[], IFormatProvider, TimeSpanStyles) | |
![]() ![]() ![]() ![]() | Subtract | |
![]() ![]() ![]() ![]() | ToString() | |
![]() ![]() ![]() | ToString(String) | |
![]() ![]() ![]() | ToString(String, IFormatProvider) | |
![]() ![]() ![]() ![]() | TryParse(String, TimeSpan) | |
![]() ![]() ![]() ![]() | TryParse(String, IFormatProvider, TimeSpan) | |
![]() ![]() ![]() ![]() | TryParseExact(String, String, IFormatProvider, TimeSpan) | |
![]() ![]() ![]() ![]() | TryParseExact(String, String[], IFormatProvider, TimeSpan) | |
![]() ![]() ![]() ![]() | TryParseExact(String, String, IFormatProvider, TimeSpanStyles, TimeSpan) | |
![]() ![]() ![]() ![]() | TryParseExact(String, String[], IFormatProvider, TimeSpanStyles, TimeSpan) |
| Nome | Descrizione | |
|---|---|---|
![]() ![]() ![]() ![]() ![]() | Addition | |
![]() ![]() ![]() ![]() ![]() | Equality | |
![]() ![]() ![]() ![]() ![]() | GreaterThan | |
![]() ![]() ![]() ![]() ![]() | GreaterThanOrEqual | |
![]() ![]() ![]() ![]() ![]() | Inequality | |
![]() ![]() ![]() ![]() ![]() | LessThan | |
![]() ![]() ![]() ![]() ![]() | LessThanOrEqual | |
![]() ![]() ![]() ![]() ![]() | Subtraction | |
![]() ![]() ![]() ![]() ![]() | UnaryNegation | |
![]() ![]() ![]() ![]() | UnaryPlus |
| Nome | Descrizione | |
|---|---|---|
![]() ![]() ![]() ![]() ![]() | MaxValue | |
![]() ![]() ![]() ![]() ![]() | MinValue | |
![]() ![]() ![]() ![]() ![]() | TicksPerDay | |
![]() ![]() ![]() ![]() ![]() | TicksPerHour | |
![]() ![]() ![]() ![]() ![]() | TicksPerMillisecond | |
![]() ![]() ![]() ![]() ![]() | TicksPerMinute | |
![]() ![]() ![]() ![]() ![]() | TicksPerSecond | |
![]() ![]() ![]() ![]() ![]() | Zero |
| Nome | Descrizione | |
|---|---|---|
![]() ![]() ![]() ![]() | IComparable.CompareTo |
Creazione un'istanza di un valore TimeSpan
Tramite la chiamata al relativo costruttore predefinito. Questo crea un oggetto il cui valore è TimeSpan.Zero, come mostra l'esempio seguente. Chiamando uno dei costruttori espliciti. Nell'esempio riportato di seguito viene inizializzato un valore TimeSpan su un numero di ore, minuti e secondi specificato. Tramite la chiamata a un metodo o l'esecuzione di un'operazione che restituisce un valore TimeSpan. Ad esempio, è possibile creare un'istanza di un valore TimeSpan che rappresenta l'intervallo tra due valori di data e ora, come mostra l'esempio seguente. 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
È inoltre possibile inizializzare un oggetto TimeSpan a un valore zero in questo modo, come illustrato di seguito. using System; public class Example { static Random rnd = new Random(); public static void Main() { TimeSpan timeSpent = TimeSpan.Zero; timeSpent += GetTimeBeforeLunch(); timeSpent += GetTimeAfterLunch(); Console.WriteLine("Total time: {0}", timeSpent); } private static TimeSpan GetTimeBeforeLunch() { return new TimeSpan(rnd.Next(3, 6), 0, 0); } private static TimeSpan GetTimeAfterLunch() { return new TimeSpan(rnd.Next(3, 6), 0, 0); } } // The example displays output like the following: // Total time: 08:00:00
I valori TimeSpan vengono restituiti da operatori aritmetici e dai metodi delle strutture DateTime, DateTimeOffset e TimeSpan. Analizzando la rappresentazione di stringa di valore TimeSpan. È possibile utilizzare i metodi Parse e TryParse per convertire stringhe che contengono intervalli di tempo in valori TimeSpan. Nell'esempio che segue viene utilizzato il metodo Parse per convertire una matrice di stringhe in valori 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'
Inoltre, è possibile definire il formato preciso della stringa di input da analizzare e convertire in un valore TimeSpan chiamando il metodo ParseExact o TryParseExact.
Esecuzione di operazioni sui valori TimeSpan
Formattazione di un valore TimeSpan
Ripristino della formattazione legacy in TimeSpan
TimeSpan interval = new TimeSpan(12, 30, 45); string output; try { output = String.Format("{0:r}", interval); } catch (FormatException) { output = "Invalid Format"; } Console.WriteLine(output); // Output from .NET Framework 3.5 and earlier versions: // 12:30:45 // Output from .NET Framework 4: // Invalid Format
Creando un file di configurazione che contiene l'elemento <TimeSpan_LegacyFormatMode>. L'impostazione dell'attributo enabled di questo elemento su true ripristina la formattazione legacy TimeSpan per ogni singola applicazione. Impostando il commutatore di compatibilità "NetFx40_TimeSpanLegacyFormatMode" quando si crea un dominio di applicazione. Questa operazione consente la formattazione di TimeSpan legacy per ogni dominio di applicazione. Nell'esempio riportato di seguito viene creato un dominio dell'applicazione che una formattazione TimeSpan legacy. 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"); } }
Quando il codice seguente viene eseguito nel nuovo dominio dell'applicazione, viene ripristinato il comportamento di formattazione TimeSpan legacy. 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.
// 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); // 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
Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (ruoli di base del server non supportati), Windows Server 2008 R2 (ruoli di base del server supportati con SP1 o versione successiva, Itanium non supportato)
.NET Framework non supporta tutte le versioni di ciascuna piattaforma. Per un elenco delle versioni supportate, vedere Requisiti di sistema di .NET Framework.


