|
Este artículo proviene de un motor de traducción automática. Mueva el puntero sobre las frases del artículo para ver el texto original. Más información.
|
Traducción
Original
|
TimeSpan (Estructura)
Espacio de nombres: System
Ensamblado: mscorlib (en mscorlib.dll)
El tipo TimeSpan expone los siguientes miembros.
| Nombre | Descripción | |
|---|---|---|
![]() ![]() ![]() ![]() | TimeSpan(Int64) | |
![]() ![]() ![]() ![]() | TimeSpan(Int32, Int32, Int32) | |
![]() ![]() ![]() ![]() | TimeSpan(Int32, Int32, Int32, Int32) | |
![]() ![]() ![]() ![]() | TimeSpan(Int32, Int32, Int32, Int32, Int32) |
| Nombre | Descripción | |
|---|---|---|
![]() ![]() ![]() ![]() | Days | |
![]() ![]() ![]() ![]() | Hours | |
![]() ![]() ![]() ![]() | Milliseconds | |
![]() ![]() ![]() ![]() | Minutes | |
![]() ![]() ![]() ![]() | Seconds | |
![]() ![]() ![]() ![]() | Ticks | |
![]() ![]() ![]() ![]() | TotalDays | |
![]() ![]() ![]() ![]() | TotalHours | |
![]() ![]() ![]() ![]() | TotalMilliseconds | |
![]() ![]() ![]() ![]() | TotalMinutes | |
![]() ![]() ![]() ![]() | TotalSeconds |
| Nombre | Descripción | |
|---|---|---|
![]() ![]() ![]() ![]() | 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) |
| Nombre | Descripción | |
|---|---|---|
![]() ![]() ![]() ![]() ![]() | Addition | |
![]() ![]() ![]() ![]() ![]() | Equality | |
![]() ![]() ![]() ![]() ![]() | GreaterThan | |
![]() ![]() ![]() ![]() ![]() | GreaterThanOrEqual | |
![]() ![]() ![]() ![]() ![]() | Inequality | |
![]() ![]() ![]() ![]() ![]() | LessThan | |
![]() ![]() ![]() ![]() ![]() | LessThanOrEqual | |
![]() ![]() ![]() ![]() ![]() | Subtraction | |
![]() ![]() ![]() ![]() ![]() | UnaryNegation | |
![]() ![]() ![]() ![]() | UnaryPlus |
| Nombre | Descripción | |
|---|---|---|
![]() ![]() ![]() ![]() ![]() | MaxValue | |
![]() ![]() ![]() ![]() ![]() | MinValue | |
![]() ![]() ![]() ![]() ![]() | TicksPerDay | |
![]() ![]() ![]() ![]() ![]() | TicksPerHour | |
![]() ![]() ![]() ![]() ![]() | TicksPerMillisecond | |
![]() ![]() ![]() ![]() ![]() | TicksPerMinute | |
![]() ![]() ![]() ![]() ![]() | TicksPerSecond | |
![]() ![]() ![]() ![]() ![]() | Zero |
| Nombre | Descripción | |
|---|---|---|
![]() ![]() ![]() ![]() | IComparable.CompareTo |
Crear instancias de un valor TimeSpan
Mediante una llamada a su constructor predeterminado implícito. Esto crea un objeto cuyo valor es TimeSpan.Zero, como se muestra en el ejemplo siguiente. Llamando a uno de sus constructores explícitos. En el siguiente ejemplo se inicializa un valor TimeSpan para un número de horas, minutos y segundos especificado. Llamando a un método o realizando una operación que devuelve un valor TimeSpan. Por ejemplo, puede crear instancias de un valor TimeSpan que representa el intervalo entre dos valores de fecha y hora, como las siguientes presentaciones del ejemplo. 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
También puede inicializar un objeto de TimeSpan a un valor de hora cero de esta manera, como las presentaciones de ejemplo siguientes. 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
Los valores TimeSpan se devuelven mediante operadores aritméticos y métodos de las estructuras DateTime, DateTimeOffset y TimeSpan. Mediante el análisis de la representación de cadena de un valor TimeSpan. Puede usar los métodos TryParse y Parse para convertir cadenas que contienen intervalos de tiempo para los valores TimeSpan. En el siguiente ejemplo, se utiliza el método Parse para convertir una matriz de cadenas en valores 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'
Además, puede definir el formato preciso de la cadena de entrada que se va a analizar y convertir a un valor TimeSpan llamando al método TryParseExact u ParseExact.
Realizar operaciones en valores TimeSpan
Dar formato a un valor TimeSpan
Restaurar el formato de TimeSpan heredado
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 archivo de configuración que contiene el elemento <TimeSpan_LegacyFormatMode>. Al establecer el atributo enabled de este elemento en true, se restaura el formato TimeSpan heredado según la aplicación. Estableciendo el modificador de compatibilidad de "NetFx40_TimeSpanLegacyFormatMode" al crear un dominio de aplicación. Esto habilita el formato TimeSpan heredado según dominio de aplicación. En el ejemplo siguiente se crea un dominio de aplicación que usa el formato TimeSpan heredado. 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"); } }
Cuando el siguiente código se ejecuta en el nuevo dominio de aplicación, revierte al comportamiento de formato TimeSpan heredado. 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 (no se admite el rol Server Core), Windows Server 2008 R2 (se admite el rol Server Core con SP1 o versiones posteriores; no se admite Itanium)
.NET Framework no admite todas las versiones de todas las plataformas. Para obtener una lista de las versiones compatibles, vea Requisitos de sistema de .NET Framework.


