|
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
|
Cadenas de formato TimeSpan personalizado
Importante
|
|---|
|
|
using System; public class Example { public static void Main() { TimeSpan duration = new TimeSpan(1, 12, 23, 62); string output = null; output = "Time of Travel: " + duration.ToString("%d") + " days"; Console.WriteLine(output); output = "Time of Travel: " + duration.ToString(@"dd\.hh\:mm\:ss"); Console.WriteLine(output); Console.WriteLine("Time of Travel: {0:%d} day(s)", duration); Console.WriteLine("Time of Travel: {0:dd\\.hh\\:mm\\:ss} days", duration); } } // The example displays the following output: // Time of Travel: 1 days // Time of Travel: 01.12:24:02 // Time of Travel: 1 day(s) // Time of Travel: 01.12:24:02 days
using System; public class Example { public static void Main() { string value = null; TimeSpan interval; value = "6"; if (TimeSpan.TryParseExact(value, "%d", null, out interval)) Console.WriteLine("{0} --> {1}", value, interval.ToString("c")); else Console.WriteLine("Unable to parse '{0}'", value); value = "16:32.05"; if (TimeSpan.TryParseExact(value, @"mm\:ss\.ff", null, out interval)) Console.WriteLine("{0} --> {1}", value, interval.ToString("c")); else Console.WriteLine("Unable to parse '{0}'", value); value= "12.035"; if (TimeSpan.TryParseExact(value, "ss\\.fff", null, out interval)) Console.WriteLine("{0} --> {1}", value, interval.ToString("c")); else Console.WriteLine("Unable to parse '{0}'", value); } } // The example displays the following output: // 6 --> 6.00:00:00 // 16:32.05 --> 00:16:32.0500000 // 12.035 --> 00:00:12.0350000
|
|
|
|
|---|---|---|
|
|
|
new TimeSpan(6, 14, 32, 17, 685):
|
|
|
|
new TimeSpan(6, 14, 32, 17, 685):
|
|
|
|
new TimeSpan(6, 14, 32, 17, 685):
|
|
|
|
new TimeSpan(6, 14, 32, 17, 685): new TimeSpan(6, 8, 32, 17, 685): |
|
|
|
new TimeSpan(6, 14, 8, 17, 685): |
|
|
|
new TimeSpan(6, 14, 8, 17, 685): new TimeSpan(6, 8, 5, 17, 685): |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
new TimeSpan(14, 32, 17):
|
|
|
|
new TimeSpan(14, 32, 17):
|
|
|
|
new TimeSpan(14, 32, 17):
|
TimeSpan ts1 = new TimeSpan(0, 23, 17, 47); TimeSpan ts2 = new TimeSpan(365, 21, 19, 45); for (int ctr = 2; ctr <= 8; ctr++) { string fmt = new String('d', ctr) + @"\.hh\:mm\:ss"; Console.WriteLine("{0} --> {1:" + fmt + "}", fmt, ts1); Console.WriteLine("{0} --> {1:" + fmt + "}", fmt, ts2); Console.WriteLine(); } // The example displays the following output: // dd\.hh\:mm\:ss --> 00.23:17:47 // dd\.hh\:mm\:ss --> 365.21:19:45 // // ddd\.hh\:mm\:ss --> 000.23:17:47 // ddd\.hh\:mm\:ss --> 365.21:19:45 // // dddd\.hh\:mm\:ss --> 0000.23:17:47 // dddd\.hh\:mm\:ss --> 0365.21:19:45 // // ddddd\.hh\:mm\:ss --> 00000.23:17:47 // ddddd\.hh\:mm\:ss --> 00365.21:19:45 // // dddddd\.hh\:mm\:ss --> 000000.23:17:47 // dddddd\.hh\:mm\:ss --> 000365.21:19:45 // // ddddddd\.hh\:mm\:ss --> 0000000.23:17:47 // ddddddd\.hh\:mm\:ss --> 0000365.21:19:45 // // dddddddd\.hh\:mm\:ss --> 00000000.23:17:47 // dddddddd\.hh\:mm\:ss --> 00000365.21:19:45
string[] values = { "49", "9", "06" }; TimeSpan interval; foreach (string value in values) { if (TimeSpan.TryParseExact(value, "ss", null, out interval)) Console.WriteLine(interval.ToString("c")); else Console.WriteLine("Unable to convert '{0}' to a time interval", value); } // The example displays the following output: // 00:00:49 // Unable to convert '9' to a time interval // 00:00:06
TimeSpan ts = new TimeSpan(1003498765432); string fmt; Console.WriteLine(ts.ToString("c")); Console.WriteLine(); for (int ctr = 1; ctr <= 7; ctr++) { fmt = new String('f', ctr); if (fmt.Length == 1) fmt = "%" + fmt; Console.WriteLine("{0,10}: {1:" + fmt + "}", fmt, ts); } Console.WriteLine(); for (int ctr = 1; ctr <= 7; ctr++) { fmt = new String('f', ctr); Console.WriteLine("{0,10}: {1:s\\." + fmt + "}", "s\\." + fmt, ts); } // The example displays the following output: // %f: 8 // ff: 87 // fff: 876 // ffff: 8765 // fffff: 87654 // ffffff: 876543 // fffffff: 8765432 // // s\.f: 29.8 // s\.ff: 29.87 // s\.fff: 29.876 // s\.ffff: 29.8765 // s\.fffff: 29.87654 // s\.ffffff: 29.876543 // s\.fffffff: 29.8765432
TimeSpan ts = new TimeSpan(1003498765432); string fmt; Console.WriteLine(ts.ToString("c")); Console.WriteLine(); for (int ctr = 1; ctr <= 7; ctr++) { fmt = new String('f', ctr); if (fmt.Length == 1) fmt = "%" + fmt; Console.WriteLine("{0,10}: {1:" + fmt + "}", fmt, ts); } Console.WriteLine(); for (int ctr = 1; ctr <= 7; ctr++) { fmt = new String('f', ctr); Console.WriteLine("{0,10}: {1:s\\." + fmt + "}", "s\\." + fmt, ts); } // The example displays the following output: // %f: 8 // ff: 87 // fff: 876 // ffff: 8765 // fffff: 87654 // ffffff: 876543 // fffffff: 8765432 // // s\.f: 29.8 // s\.ff: 29.87 // s\.fff: 29.876 // s\.ffff: 29.8765 // s\.fffff: 29.87654 // s\.ffffff: 29.876543 // s\.fffffff: 29.8765432
TimeSpan ts = new TimeSpan(1003498765432); string fmt; Console.WriteLine(ts.ToString("c")); Console.WriteLine(); for (int ctr = 1; ctr <= 7; ctr++) { fmt = new String('f', ctr); if (fmt.Length == 1) fmt = "%" + fmt; Console.WriteLine("{0,10}: {1:" + fmt + "}", fmt, ts); } Console.WriteLine(); for (int ctr = 1; ctr <= 7; ctr++) { fmt = new String('f', ctr); Console.WriteLine("{0,10}: {1:s\\." + fmt + "}", "s\\." + fmt, ts); } // The example displays the following output: // %f: 8 // ff: 87 // fff: 876 // ffff: 8765 // fffff: 87654 // ffffff: 876543 // fffffff: 8765432 // // s\.f: 29.8 // s\.ff: 29.87 // s\.fff: 29.876 // s\.ffff: 29.8765 // s\.fffff: 29.87654 // s\.ffffff: 29.876543 // s\.fffffff: 29.8765432
TimeSpan ts = new TimeSpan(1003498765432); string fmt; Console.WriteLine(ts.ToString("c")); Console.WriteLine(); for (int ctr = 1; ctr <= 7; ctr++) { fmt = new String('f', ctr); if (fmt.Length == 1) fmt = "%" + fmt; Console.WriteLine("{0,10}: {1:" + fmt + "}", fmt, ts); } Console.WriteLine(); for (int ctr = 1; ctr <= 7; ctr++) { fmt = new String('f', ctr); Console.WriteLine("{0,10}: {1:s\\." + fmt + "}", "s\\." + fmt, ts); } // The example displays the following output: // %f: 8 // ff: 87 // fff: 876 // ffff: 8765 // fffff: 87654 // ffffff: 876543 // fffffff: 8765432 // // s\.f: 29.8 // s\.ff: 29.87 // s\.fff: 29.876 // s\.ffff: 29.8765 // s\.fffff: 29.87654 // s\.ffffff: 29.876543 // s\.fffffff: 29.8765432
TimeSpan ts = new TimeSpan(1003498765432); string fmt; Console.WriteLine(ts.ToString("c")); Console.WriteLine(); for (int ctr = 1; ctr <= 7; ctr++) { fmt = new String('f', ctr); if (fmt.Length == 1) fmt = "%" + fmt; Console.WriteLine("{0,10}: {1:" + fmt + "}", fmt, ts); } Console.WriteLine(); for (int ctr = 1; ctr <= 7; ctr++) { fmt = new String('f', ctr); Console.WriteLine("{0,10}: {1:s\\." + fmt + "}", "s\\." + fmt, ts); } // The example displays the following output: // %f: 8 // ff: 87 // fff: 876 // ffff: 8765 // fffff: 87654 // ffffff: 876543 // fffffff: 8765432 // // s\.f: 29.8 // s\.ff: 29.87 // s\.fff: 29.876 // s\.ffff: 29.8765 // s\.fffff: 29.87654 // s\.ffffff: 29.876543 // s\.fffffff: 29.8765432
TimeSpan ts = new TimeSpan(1003498765432); string fmt; Console.WriteLine(ts.ToString("c")); Console.WriteLine(); for (int ctr = 1; ctr <= 7; ctr++) { fmt = new String('f', ctr); if (fmt.Length == 1) fmt = "%" + fmt; Console.WriteLine("{0,10}: {1:" + fmt + "}", fmt, ts); } Console.WriteLine(); for (int ctr = 1; ctr <= 7; ctr++) { fmt = new String('f', ctr); Console.WriteLine("{0,10}: {1:s\\." + fmt + "}", "s\\." + fmt, ts); } // The example displays the following output: // %f: 8 // ff: 87 // fff: 876 // ffff: 8765 // fffff: 87654 // ffffff: 876543 // fffffff: 8765432 // // s\.f: 29.8 // s\.ff: 29.87 // s\.fff: 29.876 // s\.ffff: 29.8765 // s\.fffff: 29.87654 // s\.ffffff: 29.876543 // s\.fffffff: 29.8765432
TimeSpan ts = new TimeSpan(1003498765432); string fmt; Console.WriteLine(ts.ToString("c")); Console.WriteLine(); for (int ctr = 1; ctr <= 7; ctr++) { fmt = new String('f', ctr); if (fmt.Length == 1) fmt = "%" + fmt; Console.WriteLine("{0,10}: {1:" + fmt + "}", fmt, ts); } Console.WriteLine(); for (int ctr = 1; ctr <= 7; ctr++) { fmt = new String('f', ctr); Console.WriteLine("{0,10}: {1:s\\." + fmt + "}", "s\\." + fmt, ts); } // The example displays the following output: // %f: 8 // ff: 87 // fff: 876 // ffff: 8765 // fffff: 87654 // ffffff: 876543 // fffffff: 8765432 // // s\.f: 29.8 // s\.ff: 29.87 // s\.fff: 29.876 // s\.ffff: 29.8765 // s\.fffff: 29.87654 // s\.ffffff: 29.876543 // s\.fffffff: 29.8765432
Console.WriteLine("Formatting:"); TimeSpan ts1 = TimeSpan.Parse("0:0:3.669"); Console.WriteLine("{0} ('%F') --> {0:%F}", ts1); TimeSpan ts2 = TimeSpan.Parse("0:0:3.091"); Console.WriteLine("{0} ('ss\\.F') --> {0:ss\\.F}", ts2); Console.WriteLine(); Console.WriteLine("Parsing:"); string[] inputs = { "0:0:03.", "0:0:03.1", "0:0:03.12" }; string fmt = @"h\:m\:ss\.F"; TimeSpan ts3; foreach (string input in inputs) { if (TimeSpan.TryParseExact(input, fmt, null, out ts3)) Console.WriteLine("{0} ('{1}') --> {2}", input, fmt, ts3); else Console.WriteLine("Cannot parse {0} with '{1}'.", input, fmt); } // The example displays the following output: // Formatting: // 00:00:03.6690000 ('%F') --> 6 // 00:00:03.0910000 ('ss\.F') --> 03. // // Parsing: // 0:0:03. ('h\:m\:ss\.F') --> 00:00:03 // 0:0:03.1 ('h\:m\:ss\.F') --> 00:00:03.1000000 // Cannot parse 0:0:03.12 with 'h\:m\:ss\.F'.
Console.WriteLine("Formatting:"); TimeSpan ts1 = TimeSpan.Parse("0:0:3.697"); Console.WriteLine("{0} ('FF') --> {0:FF}", ts1); TimeSpan ts2 = TimeSpan.Parse("0:0:3.809"); Console.WriteLine("{0} ('ss\\.FF') --> {0:ss\\.FF}", ts2); Console.WriteLine(); Console.WriteLine("Parsing:"); string[] inputs = { "0:0:03.", "0:0:03.1", "0:0:03.127" }; string fmt = @"h\:m\:ss\.FF"; TimeSpan ts3; foreach (string input in inputs) { if (TimeSpan.TryParseExact(input, fmt, null, out ts3)) Console.WriteLine("{0} ('{1}') --> {2}", input, fmt, ts3); else Console.WriteLine("Cannot parse {0} with '{1}'.", input, fmt); } // The example displays the following output: // Formatting: // 00:00:03.6970000 ('FF') --> 69 // 00:00:03.8090000 ('ss\.FF') --> 03.8 // // Parsing: // 0:0:03. ('h\:m\:ss\.FF') --> 00:00:03 // 0:0:03.1 ('h\:m\:ss\.FF') --> 00:00:03.1000000 // Cannot parse 0:0:03.127 with 'h\:m\:ss\.FF'.
Console.WriteLine("Formatting:"); TimeSpan ts1 = TimeSpan.Parse("0:0:3.6974"); Console.WriteLine("{0} ('FFF') --> {0:FFF}", ts1); TimeSpan ts2 = TimeSpan.Parse("0:0:3.8009"); Console.WriteLine("{0} ('ss\\.FFF') --> {0:ss\\.FFF}", ts2); Console.WriteLine(); Console.WriteLine("Parsing:"); string[] inputs = { "0:0:03.", "0:0:03.12", "0:0:03.1279" }; string fmt = @"h\:m\:ss\.FFF"; TimeSpan ts3; foreach (string input in inputs) { if (TimeSpan.TryParseExact(input, fmt, null, out ts3)) Console.WriteLine("{0} ('{1}') --> {2}", input, fmt, ts3); else Console.WriteLine("Cannot parse {0} with '{1}'.", input, fmt); } // The example displays the following output: // Formatting: // 00:00:03.6974000 ('FFF') --> 697 // 00:00:03.8009000 ('ss\.FFF') --> 03.8 // // Parsing: // 0:0:03. ('h\:m\:ss\.FFF') --> 00:00:03 // 0:0:03.12 ('h\:m\:ss\.FFF') --> 00:00:03.1200000 // Cannot parse 0:0:03.1279 with 'h\:m\:ss\.FFF'.
Console.WriteLine("Formatting:"); TimeSpan ts1 = TimeSpan.Parse("0:0:3.69749"); Console.WriteLine("{0} ('FFFF') --> {0:FFFF}", ts1); TimeSpan ts2 = TimeSpan.Parse("0:0:3.80009"); Console.WriteLine("{0} ('ss\\.FFFF') --> {0:ss\\.FFFF}", ts2); Console.WriteLine(); Console.WriteLine("Parsing:"); string[] inputs = { "0:0:03.", "0:0:03.12", "0:0:03.12795" }; string fmt = @"h\:m\:ss\.FFFF"; TimeSpan ts3; foreach (string input in inputs) { if (TimeSpan.TryParseExact(input, fmt, null, out ts3)) Console.WriteLine("{0} ('{1}') --> {2}", input, fmt, ts3); else Console.WriteLine("Cannot parse {0} with '{1}'.", input, fmt); } // The example displays the following output: // Formatting: // 00:00:03.6974900 ('FFFF') --> 6974 // 00:00:03.8000900 ('ss\.FFFF') --> 03.8 // // Parsing: // 0:0:03. ('h\:m\:ss\.FFFF') --> 00:00:03 // 0:0:03.12 ('h\:m\:ss\.FFFF') --> 00:00:03.1200000 // Cannot parse 0:0:03.12795 with 'h\:m\:ss\.FFFF'.
Console.WriteLine("Formatting:"); TimeSpan ts1 = TimeSpan.Parse("0:0:3.697497"); Console.WriteLine("{0} ('FFFFF') --> {0:FFFFF}", ts1); TimeSpan ts2 = TimeSpan.Parse("0:0:3.800009"); Console.WriteLine("{0} ('ss\\.FFFFF') --> {0:ss\\.FFFFF}", ts2); Console.WriteLine(); Console.WriteLine("Parsing:"); string[] inputs = { "0:0:03.", "0:0:03.12", "0:0:03.127956" }; string fmt = @"h\:m\:ss\.FFFFF"; TimeSpan ts3; foreach (string input in inputs) { if (TimeSpan.TryParseExact(input, fmt, null, out ts3)) Console.WriteLine("{0} ('{1}') --> {2}", input, fmt, ts3); else Console.WriteLine("Cannot parse {0} with '{1}'.", input, fmt); } // The example displays the following output: // Formatting: // 00:00:03.6974970 ('FFFFF') --> 69749 // 00:00:03.8000090 ('ss\.FFFFF') --> 03.8 // // Parsing: // 0:0:03. ('h\:m\:ss\.FFFF') --> 00:00:03 // 0:0:03.12 ('h\:m\:ss\.FFFF') --> 00:00:03.1200000 // Cannot parse 0:0:03.127956 with 'h\:m\:ss\.FFFF'.
Console.WriteLine("Formatting:"); TimeSpan ts1 = TimeSpan.Parse("0:0:3.6974974"); Console.WriteLine("{0} ('FFFFFF') --> {0:FFFFFF}", ts1); TimeSpan ts2 = TimeSpan.Parse("0:0:3.8000009"); Console.WriteLine("{0} ('ss\\.FFFFFF') --> {0:ss\\.FFFFFF}", ts2); Console.WriteLine(); Console.WriteLine("Parsing:"); string[] inputs = { "0:0:03.", "0:0:03.12", "0:0:03.1279569" }; string fmt = @"h\:m\:ss\.FFFFFF"; TimeSpan ts3; foreach (string input in inputs) { if (TimeSpan.TryParseExact(input, fmt, null, out ts3)) Console.WriteLine("{0} ('{1}') --> {2}", input, fmt, ts3); else Console.WriteLine("Cannot parse {0} with '{1}'.", input, fmt); } // The example displays the following output: // Formatting: // 00:00:03.6974974 ('FFFFFF') --> 697497 // 00:00:03.8000009 ('ss\.FFFFFF') --> 03.8 // // Parsing: // 0:0:03. ('h\:m\:ss\.FFFFFF') --> 00:00:03 // 0:0:03.12 ('h\:m\:ss\.FFFFFF') --> 00:00:03.1200000 // Cannot parse 0:0:03.1279569 with 'h\:m\:ss\.FFFFFF'.
Console.WriteLine("Formatting:"); TimeSpan ts1 = TimeSpan.Parse("0:0:3.6974974"); Console.WriteLine("{0} ('FFFFFFF') --> {0:FFFFFFF}", ts1); TimeSpan ts2 = TimeSpan.Parse("0:0:3.9500000"); Console.WriteLine("{0} ('ss\\.FFFFFFF') --> {0:ss\\.FFFFFFF}", ts2); Console.WriteLine(); Console.WriteLine("Parsing:"); string[] inputs = { "0:0:03.", "0:0:03.12", "0:0:03.1279569" }; string fmt = @"h\:m\:ss\.FFFFFFF"; TimeSpan ts3; foreach (string input in inputs) { if (TimeSpan.TryParseExact(input, fmt, null, out ts3)) Console.WriteLine("{0} ('{1}') --> {2}", input, fmt, ts3); else Console.WriteLine("Cannot parse {0} with '{1}'.", input, fmt); } // The example displays the following output: // Formatting: // 00:00:03.6974974 ('FFFFFFF') --> 6974974 // 00:00:03.9500000 ('ss\.FFFFFFF') --> 03.95 // // Parsing: // 0:0:03. ('h\:m\:ss\.FFFFFFF') --> 00:00:03 // 0:0:03.12 ('h\:m\:ss\.FFFFFFF') --> 00:00:03.1200000 // 0:0:03.1279569 ('h\:m\:ss\.FFFFFFF') --> 00:00:03.1279569
-
Se puede escribirlo entre comillas sencillas (delimitador de cadena literal). -
Se puede anteponer una barra diagonal inversa ("\"), la cual se interpreta como un carácter de escape. En C#, esto significa que la cadena de formato debe escribirse entre comillas e ir precedida del símbolo @ o que el carácter literal debe ir precedido de una barra diagonal inversa adicional.
TimeSpan interval = new TimeSpan(0, 32, 45); // Escape literal characters in a format string. string fmt = @"mm\:ss\ \m\i\n\u\t\e\s"; Console.WriteLine(interval.ToString(fmt)); // Delimit literal characters in a format string with the ' symbol. fmt = "mm':'ss' minutes'"; Console.WriteLine(interval.ToString(fmt)); // The example displays the following output: // 32:45 minutes // 32:45 minutes
Importante