Uwaga
Dostęp do tej strony wymaga autoryzacji. Może spróbować zalogować się lub zmienić katalogi.
Dostęp do tej strony wymaga autoryzacji. Możesz spróbować zmienić katalogi.
Ciąg formatu TimeSpan definiuje reprezentację ciągu TimeSpan wartości, która wynika z operacji formatowania. Ciąg formatu niestandardowego składa się z co najmniej jednego specyfikatora formatu niestandardowego TimeSpan wraz z dowolną liczbą znaków literału. Każdy ciąg, który nie jest ciągiem formatu Standard TimeSpan, jest interpretowany jako niestandardowy ciąg formatu TimeSpan.
Ważne
Specyfikatory formatu niestandardowego TimeSpan nie zawierają symboli separatora zastępczego, takich jak symbole oddzielające dni od godzin, godzin od minut lub sekund od sekund ułamkowych. Zamiast tego te symbole muszą być zawarte w ciągu formatu niestandardowego jako literały ciągu ciągu. Na przykład "dd\.hh\:mm"
definiuje kropkę (.) jako separator między dniami a godzinami i dwukropek (:) jako separator między godzinami a minutami.
Specyfikatory formatu niestandardowego TimeSpan również nie zawierają symbolu znaku, który umożliwia rozróżnienie między ujemnymi i dodatnimi interwałami czasu. Aby dołączyć symbol znaku, musisz utworzyć ciąg formatu przy użyciu logiki warunkowej. Sekcja Inne znaki zawiera przykład.
Reprezentacje ciągów wartości TimeSpan są generowane przez wywołania przeciążeń metody TimeSpan.ToString i metod, które obsługują formatowanie złożone, takie jak String.Format. Aby uzyskać więcej informacji, zobacz Typy formatowania i formatowanie złożone. Poniższy przykład ilustruje użycie ciągów formatu niestandardowego w operacjach formatowania.
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: {duration:%d} day(s)");
Console.WriteLine($"Time of Travel: {duration:dd\\.hh\\:mm\\:ss} days");
}
}
// 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
Module Example
Public Sub Main()
Dim duration As New TimeSpan(1, 12, 23, 62)
Dim output As String = Nothing
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)
End Sub
End Module
' 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
Niestandardowe ciągi formatu TimeSpan są również używane przez metody TimeSpan.ParseExact i TimeSpan.TryParseExact do zdefiniowania wymaganego formatu ciągów wejściowych na potrzeby operacji analizowania. (Analizowanie konwertuje reprezentację ciągu wartości na wartość). Poniższy przykład ilustruje użycie standardowych ciągów formatu w operacjach analizowania.
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($"{value} --> {interval.ToString("c")}");
else
Console.WriteLine($"Unable to parse '{value}'");
value = "16:32.05";
if (TimeSpan.TryParseExact(value, @"mm\:ss\.ff", null, out interval))
Console.WriteLine($"{value} --> {interval.ToString("c")}");
else
Console.WriteLine($"Unable to parse '{value}'");
value= "12.035";
if (TimeSpan.TryParseExact(value, "ss\\.fff", null, out interval))
Console.WriteLine($"{value} --> {interval.ToString("c")}");
else
Console.WriteLine($"Unable to parse '{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
Module Example
Public Sub Main()
Dim value As String = Nothing
Dim interval As TimeSpan
value = "6"
If TimeSpan.TryParseExact(value, "%d", Nothing, interval) Then
Console.WriteLine("{0} --> {1}", value, interval.ToString("c"))
Else
Console.WriteLine("Unable to parse '{0}'", value)
End If
value = "16:32.05"
If TimeSpan.TryParseExact(value, "mm\:ss\.ff", Nothing, interval) Then
Console.WriteLine("{0} --> {1}", value, interval.ToString("c"))
Else
Console.WriteLine("Unable to parse '{0}'", value)
End If
value = "12.035"
If TimeSpan.TryParseExact(value, "ss\.fff", Nothing, interval) Then
Console.WriteLine("{0} --> {1}", value, interval.ToString("c"))
Else
Console.WriteLine("Unable to parse '{0}'", value)
End If
End Sub
End Module
' The example displays the following output:
' 6 --> 6.00:00:00
' 16:32.05 --> 00:16:32.0500000
' 12.035 --> 00:00:12.0350000
Poniższa tabela zawiera opis niestandardowych specyfikatorów formatu daty i godziny.
Specyfikator formatu | Opis | Przykład |
---|---|---|
"d", "%d" | Liczba całych dni w interwale czasu. Więcej informacji: Specyfikator formatu niestandardowego "d". |
new TimeSpan(6, 14, 32, 17, 685): %d —> "6"d\.hh\:mm —> "6.14:32" |
"dd"-"ddddd" | Liczba całych dni w interwale czasu wyściełana zerami wiodącymi zgodnie z potrzebami. Więcej informacji: specyfikatory formatu niestandardowego "dd"-"dd". |
new TimeSpan(6, 14, 32, 17, 685): ddd —> "006"dd\.hh\:mm —> "06.14:32" |
"h", "%h" | Liczba całych godzin w interwale czasu, które nie są liczone w ramach dni. Godziny jednocyfrowe nie mają zera wiodącego. Więcej informacji: Specyfikator formatu niestandardowego "h". |
new TimeSpan(6, 14, 32, 17, 685): %h —> "14"hh\:mm —> "14:32" |
"gg" | Liczba całych godzin w interwale czasu, które nie są liczone w ramach dni. Godziny jednocyfrowe mają zero wiodące. Więcej informacji: Specyfikator formatu niestandardowego "hh". |
new TimeSpan(6, 14, 32, 17, 685): hh —> "14"new TimeSpan(6, 8, 32, 17, 685): hh —> 08 |
"m", "%m" | Liczba całych minut w interwale czasu, które nie są uwzględniane w ramach godzin lub dni. Minuty z jedną cyfrą nie mają zera wiodącego. Więcej informacji: Specyfikator formatu niestandardowego "m". |
new TimeSpan(6, 14, 8, 17, 685): %m —> "8"h\:m —> "14:8" |
"mm" | Liczba całych minut w interwale czasu, które nie są uwzględniane w ramach godzin lub dni. Minuty z jedną cyfrą mają zero wiodące. Więcej informacji: Specyfikator formatu niestandardowego "mm". |
new TimeSpan(6, 14, 8, 17, 685): mm —> "08"new TimeSpan(6, 8, 5, 17, 685): d\.hh\:mm\:ss -> 6.08:05:17 |
"s", "%s" | Liczba całych sekund w interwale czasu, które nie są uwzględniane w ramach godzin, dni lub minut. Sekundy jednocyfrowe nie mają zera wiodącego. Więcej informacji: Specyfikator formatu niestandardowego "s". |
TimeSpan.FromSeconds(12.965) :%s —> 12s\.fff —> 12.965 |
„ss” | Liczba całych sekund w interwale czasu, które nie są uwzględniane w ramach godzin, dni lub minut. Sekundy z jedną cyfrą mają zero wiodące. Więcej informacji: Specyfikator formatu niestandardowego "ss". |
TimeSpan.FromSeconds(6.965) :ss —> 06ss\.fff —> 06.965 |
"f", "%f" | Dziesiąte części sekundy w interwale czasu. Więcej informacji: Specyfikator formatu niestandardowego "f". |
TimeSpan.FromSeconds(6.895) :f —> 8ss\.f —> 06.8 |
„ff” | Setne części sekundy w interwale czasu. Więcej informacji: Specyfikator formatu niestandardowego "ff". |
TimeSpan.FromSeconds(6.895) :ff —> 89ss\.ff —> 06.89 |
„fff” | Milisekundy w interwale czasu. Więcej informacji: Specyfikator formatu niestandardowego "fff". |
TimeSpan.FromSeconds(6.895) :fff —> 895ss\.fff —> 06.895 |
„ffff” | Dziesięć tysięcznych części sekundy w interwale czasu. Więcej informacji: Specyfikator formatu niestandardowego "ffff". |
TimeSpan.Parse("0:0:6.8954321") :ffff —> 8954ss\.ffff —> 06.8954 |
„fffff” | Setki tysięcy sekundy w interwale czasu. Więcej informacji: Specyfikator formatu niestandardowego "fffff". |
TimeSpan.Parse("0:0:6.8954321") :fffff —> 89543ss\.fffff —> 06.89543 |
„ffffff” | Milionowe części sekundy w interwale czasu. Więcej informacji: Specyfikator formatu niestandardowego "ffffff". |
TimeSpan.Parse("0:0:6.8954321") :ffffff —> 895432ss\.ffffff —> 06.895432 |
„fffffff” | Dziesięć milionów części sekundy (lub ułamkowe kleszcze) w przedziale czasu. Więcej informacji: Specyfikator formatu niestandardowego "fffffff". |
TimeSpan.Parse("0:0:6.8954321") :fffffff —> 8954321ss\.fffffff —> 06.8954321 |
"F", "%F" | Dziesiąte części sekundy w interwale czasu. Nic nie jest wyświetlane, jeśli cyfra ma wartość zero. Więcej informacji: Specyfikator formatu niestandardowego "F". |
TimeSpan.Parse("00:00:06.32") :%F : 3TimeSpan.Parse("0:0:3.091") :ss\.F : 03. |
„FF” | Setne części sekundy w interwale czasu. Wszystkie zera końcowe ułamkowe lub dwie cyfry zerowe nie są uwzględniane. Więcej informacji: Specyfikator formatu niestandardowego "FF". |
TimeSpan.Parse("00:00:06.329") :FF : 32TimeSpan.Parse("0:0:3.101") :ss\.FF : 03.1 |
„FFF” | Milisekundy w interwale czasu. Wszystkie zera końcowe ułamkowe nie są uwzględniane. Więcej informacji: |
TimeSpan.Parse("00:00:06.3291") :FFF : 329TimeSpan.Parse("0:0:3.1009") :ss\.FFF : 03.1 |
„FFFF” | Dziesięć tysięcznych części sekundy w interwale czasu. Wszystkie zera końcowe ułamkowe nie są uwzględniane. Więcej informacji: Specyfikator formatu niestandardowego "FFFF". |
TimeSpan.Parse("00:00:06.32917") :FFFFF : 3291TimeSpan.Parse("0:0:3.10009") :ss\.FFFF : 03.1 |
„FFFFF” | Setki tysięcy sekundy w interwale czasu. Wszystkie zera końcowe ułamkowe nie są uwzględniane. Więcej informacji: Specyfikator formatu niestandardowego "FFFFF". |
TimeSpan.Parse("00:00:06.329179") :FFFFF : 32917TimeSpan.Parse("0:0:3.100009") :ss\.FFFFF : 03.1 |
„FFFFFF” | Milionowe części sekundy w interwale czasu. Nie są wyświetlane żadne zera końcowe ułamkowe. Więcej informacji: Specyfikator formatu niestandardowego "FFFFFF". |
TimeSpan.Parse("00:00:06.3291791") :FFFFFF : 329179TimeSpan.Parse("0:0:3.1000009") :ss\.FFFFFF : 03.1 |
„FFFFFFF” | Dziesięć milionów sekund w interwale czasu. Nie są wyświetlane żadne zera końcowe ułamkowe lub siedem zer. Więcej informacji: Specyfikator formatu niestandardowego "FFFFFFF". |
TimeSpan.Parse("00:00:06.3291791") :FFFFFF : 3291791TimeSpan.Parse("0:0:3.1900000") :ss\.FFFFFF : 03.19 |
"ciąg" | Ogranicznik ciągu literału. Więcej informacji: Inne znaki. |
new TimeSpan(14, 32, 17): hh':'mm':'ss -> "14:32:17" |
\ | Znak ucieczki. Więcej informacji: Inne znaki. |
new TimeSpan(14, 32, 17): hh\:mm\:ss -> "14:32:17" |
Jakikolwiek inny znak | Każdy inny niewysobowy znak jest interpretowany jako specyfikator formatu niestandardowego. Więcej informacji: Inne znaki. |
new TimeSpan(14, 32, 17): hh\:mm\:ss -> "14:32:17" |
Specyfikator formatu niestandardowego "d"
Specyfikator formatu niestandardowego "d" zwraca wartość właściwości TimeSpan.Days, która reprezentuje liczbę całych dni w interwale czasu. Zwraca pełną liczbę dni w wartości TimeSpan, nawet jeśli wartość ma więcej niż jedną cyfrę. Jeśli wartość właściwości TimeSpan.Days wynosi zero, specyfikator zwraca wartość "0".
Jeśli specyfikator formatu niestandardowego "d" jest używany samodzielnie, określ "%d", aby nie był błędnie interpretowany jako standardowy ciąg formatu. Poniższy przykład stanowi ilustrację.
TimeSpan ts1 = new TimeSpan(16, 4, 3, 17, 250);
Console.WriteLine(ts1.ToString("%d"));
// Displays 16
Dim ts As New TimeSpan(16, 4, 3, 17, 250)
Console.WriteLine(ts.ToString("%d"))
' Displays 16
Poniższy przykład ilustruje użycie specyfikatora formatu niestandardowego "d".
TimeSpan ts2 = new TimeSpan(4, 3, 17);
Console.WriteLine(ts2.ToString(@"d\.hh\:mm\:ss"));
TimeSpan ts3 = new TimeSpan(3, 4, 3, 17);
Console.WriteLine(ts3.ToString(@"d\.hh\:mm\:ss"));
// The example displays the following output:
// 0.04:03:17
// 3.04:03:17
Dim ts2 As New TimeSpan(4, 3, 17)
Console.WriteLine(ts2.ToString("d\.hh\:mm\:ss"))
Dim ts3 As New TimeSpan(3, 4, 3, 17)
Console.WriteLine(ts3.ToString("d\.hh\:mm\:ss"))
' The example displays the following output:
' 0.04:03:17
' 3.04:03:17
Specyfikatory formatu niestandardowego "dd"-"ddd"
Specyfikatory formatu niestandardowego "ddd", "d", "d", "d", "d" i "d" generują wartość właściwości TimeSpan.Days, która reprezentuje liczbę dni całkowitych w interwale czasu.
Ciąg wyjściowy zawiera minimalną liczbę cyfr określonych przez liczbę znaków "d" w specyfikatorze formatu i jest dopełniona zerami wiodącymi zgodnie z potrzebami. Jeśli cyfry w liczbie dni przekraczają liczbę znaków "d" w specyfikatorze formatu, pełna liczba dni jest wynikiem w ciągu wynikowym.
W poniższym przykładzie użyto tych specyfikatorów formatu, aby wyświetlić reprezentację ciągu dwóch TimeSpan wartości. Wartość składnika dni pierwszego interwału to zero; wartość składnika dni drugiego to 365.
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($"{fmt} --> {ts1.ToString(fmt)}");
Console.WriteLine($"{fmt} --> {ts2.ToString(fmt)}");
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
Dim ts1 As New TimeSpan(0, 23, 17, 47)
Dim ts2 As New TimeSpan(365, 21, 19, 45)
For ctr As Integer = 2 To 8
Dim fmt As String = New String("d"c, ctr) + "\.hh\:mm\:ss"
Console.WriteLine("{0} --> {1:" + fmt + "}", fmt, ts1)
Console.WriteLine("{0} --> {1:" + fmt + "}", fmt, ts2)
Console.WriteLine()
Next
' 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
Specyfikator formatu niestandardowego "h"
Specyfikator formatu niestandardowego "h" zwraca wartość właściwości TimeSpan.Hours, która reprezentuje liczbę całych godzin w interwale czasu, który nie jest liczone jako część składnika dnia. Zwraca wartość ciągu jednocyfrowego, jeśli wartość właściwości TimeSpan.Hours wynosi od 0 do 9 i zwraca wartość ciągu dwucyfrowego, jeśli wartość właściwości TimeSpan.Hours waha się od 10 do 23.
Jeśli specyfikator formatu niestandardowego "h" jest używany samodzielnie, określ "%h", aby nie był błędnie interpretowany jako standardowy ciąg formatu. Poniższy przykład stanowi ilustrację.
TimeSpan ts = new TimeSpan(3, 42, 0);
Console.WriteLine($"{ts:%h} hours {ts:%m} minutes");
// The example displays the following output:
// 3 hours 42 minutes
Dim ts As New TimeSpan(3, 42, 0)
Console.WriteLine("{0:%h} hours {0:%m} minutes", ts)
' The example displays the following output:
' 3 hours 42 minutes
Zazwyczaj w operacji analizowania ciąg wejściowy zawierający tylko jedną liczbę jest interpretowany jako liczba dni. Zamiast tego można użyć specyfikatora formatu niestandardowego "%h", aby interpretować ciąg liczbowy jako liczbę godzin. Poniższy przykład stanowi ilustrację.
string value = "8";
TimeSpan interval;
if (TimeSpan.TryParseExact(value, "%h", null, out interval))
Console.WriteLine(interval.ToString("c"));
else
Console.WriteLine($"Unable to convert '{value}' to a time interval");
// The example displays the following output:
// 08:00:00
Dim value As String = "8"
Dim interval As TimeSpan
If TimeSpan.TryParseExact(value, "%h", Nothing, interval) Then
Console.WriteLine(interval.ToString("c"))
Else
Console.WriteLine("Unable to convert '{0}' to a time interval",
value)
End If
' The example displays the following output:
' 08:00:00
Poniższy przykład ilustruje użycie specyfikatora formatu niestandardowego "h".
TimeSpan ts1 = new TimeSpan(14, 3, 17);
Console.WriteLine(ts1.ToString(@"d\.h\:mm\:ss"));
TimeSpan ts2 = new TimeSpan(3, 4, 3, 17);
Console.WriteLine(ts2.ToString(@"d\.h\:mm\:ss"));
// The example displays the following output:
// 0.14:03:17
// 3.4:03:17
Dim ts1 As New TimeSpan(14, 3, 17)
Console.WriteLine(ts1.ToString("d\.h\:mm\:ss"))
Dim ts2 As New TimeSpan(3, 4, 3, 17)
Console.WriteLine(ts2.ToString("d\.h\:mm\:ss"))
' The example displays the following output:
' 0.14:03:17
' 3.4:03:17
Specyfikator formatu niestandardowego "hh"
Specyfikator formatu niestandardowego "hh" zwraca wartość właściwości TimeSpan.Hours, która reprezentuje liczbę całych godzin w interwale czasu, który nie jest liczone jako część składnika dnia. W przypadku wartości od 0 do 9 ciąg wyjściowy zawiera zero wiodące.
Zazwyczaj w operacji analizowania ciąg wejściowy zawierający tylko jedną liczbę jest interpretowany jako liczba dni. Zamiast tego można użyć specyfikatora formatu niestandardowego "hh", aby interpretować ciąg liczbowy jako liczbę godzin. Poniższy przykład stanowi ilustrację.
string value = "08";
TimeSpan interval;
if (TimeSpan.TryParseExact(value, "hh", null, out interval))
Console.WriteLine(interval.ToString("c"));
else
Console.WriteLine($"Unable to convert '{value}' to a time interval");
// The example displays the following output:
// 08:00:00
Dim value As String = "08"
Dim interval As TimeSpan
If TimeSpan.TryParseExact(value, "hh", Nothing, interval) Then
Console.WriteLine(interval.ToString("c"))
Else
Console.WriteLine("Unable to convert '{0}' to a time interval",
value)
End If
' The example displays the following output:
' 08:00:00
Poniższy przykład ilustruje użycie specyfikatora formatu niestandardowego "hh".
TimeSpan ts1 = new TimeSpan(14, 3, 17);
Console.WriteLine(ts1.ToString(@"d\.hh\:mm\:ss"));
TimeSpan ts2 = new TimeSpan(3, 4, 3, 17);
Console.WriteLine(ts2.ToString(@"d\.hh\:mm\:ss"));
// The example displays the following output:
// 0.14:03:17
// 3.04:03:17
Dim ts1 As New TimeSpan(14, 3, 17)
Console.WriteLine(ts1.ToString("d\.hh\:mm\:ss"))
Dim ts2 As New TimeSpan(3, 4, 3, 17)
Console.WriteLine(ts2.ToString("d\.hh\:mm\:ss"))
' The example displays the following output:
' 0.14:03:17
' 3.04:03:17
Specyfikator formatu niestandardowego "m"
Specyfikator formatu niestandardowego "m" zwraca wartość właściwości TimeSpan.Minutes, która reprezentuje liczbę całych minut w interwale czasu, który nie jest liczone jako część składnika dnia. Zwraca wartość ciągu jednocyfrowego, jeśli wartość właściwości TimeSpan.Minutes wynosi od 0 do 9 i zwraca wartość ciągu dwucyfrowego, jeśli wartość właściwości TimeSpan.Minutes waha się od 10 do 59.
Jeśli specyfikator formatu niestandardowego "m" jest używany samodzielnie, określ "%m", aby nie był błędnie interpretowany jako standardowy ciąg formatu. Poniższy przykład stanowi ilustrację.
TimeSpan ts = new TimeSpan(3, 42, 0);
Console.WriteLine($"{ts:%h} hours {ts:%m} minutes");
// The example displays the following output:
// 3 hours 42 minutes
Dim ts As New TimeSpan(3, 42, 0)
Console.WriteLine("{0:%h} hours {0:%m} minutes", ts)
' The example displays the following output:
' 3 hours 42 minutes
Zazwyczaj w operacji analizowania ciąg wejściowy zawierający tylko jedną liczbę jest interpretowany jako liczba dni. Zamiast tego możesz użyć specyfikatora formatu niestandardowego "%m", aby interpretować ciąg liczbowy jako liczbę minut. Poniższy przykład stanowi ilustrację.
string value = "3";
TimeSpan interval;
if (TimeSpan.TryParseExact(value, "%m", null, out interval))
Console.WriteLine(interval.ToString("c"));
else
Console.WriteLine($"Unable to convert '{value}' to a time interval");
// The example displays the following output:
// 00:03:00
Dim value As String = "3"
Dim interval As TimeSpan
If TimeSpan.TryParseExact(value, "%m", Nothing, interval) Then
Console.WriteLine(interval.ToString("c"))
Else
Console.WriteLine("Unable to convert '{0}' to a time interval",
value)
End If
' The example displays the following output:
' 00:03:00
Poniższy przykład ilustruje użycie specyfikatora formatu niestandardowego "m".
TimeSpan ts1 = new TimeSpan(0, 6, 32);
Console.WriteLine($"{ts1:m\\:ss} minutes");
TimeSpan ts2 = new TimeSpan(3, 4, 3, 17);
Console.WriteLine($"Elapsed time: {ts2:m\\:ss}");
// The example displays the following output:
// 6:32 minutes
// Elapsed time: 18:44
Dim ts1 As New TimeSpan(0, 6, 32)
Console.WriteLine("{0:m\:ss} minutes", ts1)
Dim ts2 As New TimeSpan(0, 18, 44)
Console.WriteLine("Elapsed time: {0:m\:ss}", ts2)
' The example displays the following output:
' 6:32 minutes
' Elapsed time: 18:44
Specyfikator formatu niestandardowego "mm"
Specyfikator formatu niestandardowego "mm" generuje wartość właściwości TimeSpan.Minutes, która reprezentuje liczbę całych minut w przedziale czasu, który nie jest uwzględniony w składniku godzin lub dni. W przypadku wartości od 0 do 9 ciąg wyjściowy zawiera zero wiodące.
Zazwyczaj w operacji analizowania ciąg wejściowy zawierający tylko jedną liczbę jest interpretowany jako liczba dni. Zamiast tego możesz użyć specyfikatora formatu niestandardowego "mm", aby interpretować ciąg liczbowy jako liczbę minut. Poniższy przykład stanowi ilustrację.
string value = "07";
TimeSpan interval;
if (TimeSpan.TryParseExact(value, "mm", null, out interval))
Console.WriteLine(interval.ToString("c"));
else
Console.WriteLine($"Unable to convert '{value}' to a time interval");
// The example displays the following output:
// 00:07:00
Dim value As String = "05"
Dim interval As TimeSpan
If TimeSpan.TryParseExact(value, "mm", Nothing, interval) Then
Console.WriteLine(interval.ToString("c"))
Else
Console.WriteLine("Unable to convert '{0}' to a time interval",
value)
End If
' The example displays the following output:
' 00:05:00
Poniższy przykład ilustruje użycie specyfikatora formatu niestandardowego "mm".
TimeSpan departTime = new TimeSpan(11, 12, 00);
TimeSpan arriveTime = new TimeSpan(16, 28, 00);
Console.WriteLine($"Travel time: {arriveTime - departTime:hh\\:mm}");
// The example displays the following output:
// Travel time: 05:16
Dim departTime As New TimeSpan(11, 12, 00)
Dim arriveTime As New TimeSpan(16, 28, 00)
Console.WriteLine("Travel time: {0:hh\:mm}",
arriveTime - departTime)
' The example displays the following output:
' Travel time: 05:16
Specyfikator formatu niestandardowego "s"
Specyfikator formatu niestandardowego "s" generuje wartość właściwości TimeSpan.Seconds, która reprezentuje liczbę całych sekund w interwale czasu, który nie jest uwzględniony w składniku godzin, dni lub minut. Zwraca wartość ciągu jednocyfrowego, jeśli wartość właściwości TimeSpan.Seconds wynosi od 0 do 9 i zwraca wartość ciągu dwucyfrowego, jeśli wartość właściwości TimeSpan.Seconds waha się od 10 do 59.
Jeśli specyfikator formatu niestandardowego "s" jest używany samodzielnie, określ "%s", aby nie był błędnie interpretowany jako standardowy ciąg formatu. Poniższy przykład stanowi ilustrację.
TimeSpan ts = TimeSpan.FromSeconds(12.465);
Console.WriteLine(ts.ToString("%s"));
// The example displays the following output:
// 12
Dim ts As TimeSpan = TimeSpan.FromSeconds(12.465)
Console.WriteLine(ts.ToString("%s"))
' The example displays the following output:
' 12
Zazwyczaj w operacji analizowania ciąg wejściowy zawierający tylko jedną liczbę jest interpretowany jako liczba dni. Zamiast tego można użyć specyfikatora formatu niestandardowego "%s", aby interpretować ciąg liczbowy jako liczbę sekund. Poniższy przykład stanowi ilustrację.
string value = "9";
TimeSpan interval;
if (TimeSpan.TryParseExact(value, "%s", null, out interval))
Console.WriteLine(interval.ToString("c"));
else
Console.WriteLine($"Unable to convert '{value}' to a time interval");
// The example displays the following output:
// 00:00:09
Dim value As String = "9"
Dim interval As TimeSpan
If TimeSpan.TryParseExact(value, "%s", Nothing, interval) Then
Console.WriteLine(interval.ToString("c"))
Else
Console.WriteLine("Unable to convert '{0}' to a time interval",
value)
End If
' The example displays the following output:
' 00:00:09
Poniższy przykład ilustruje użycie specyfikatora formatu niestandardowego "s".
TimeSpan startTime = new TimeSpan(0, 12, 30, 15, 0);
TimeSpan endTime = new TimeSpan(0, 12, 30, 21, 3);
Console.WriteLine(@"Elapsed Time: {0:s\:fff} seconds",
endTime - startTime);
// The example displays the following output:
// Elapsed Time: 6:003 seconds
Dim startTime As New TimeSpan(0, 12, 30, 15, 0)
Dim endTime As New TimeSpan(0, 12, 30, 21, 3)
Console.WriteLine("Elapsed Time: {0:s\:fff} seconds",
endTime - startTime)
' The example displays the following output:
' Elapsed Time: 6:003 seconds
Specyfikator formatu niestandardowego "ss"
Specyfikator formatu niestandardowego "ss" zwraca wartość właściwości TimeSpan.Seconds, która reprezentuje liczbę całych sekund w interwale czasu, który nie jest uwzględniony w ramach jego godzin, dni lub minut składnika. W przypadku wartości od 0 do 9 ciąg wyjściowy zawiera zero wiodące.
Zazwyczaj w operacji analizowania ciąg wejściowy zawierający tylko jedną liczbę jest interpretowany jako liczba dni. Zamiast tego można użyć specyfikatora formatu niestandardowego "ss", aby interpretować ciąg liczbowy jako liczbę sekund. Poniższy przykład stanowi ilustrację.
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 '{value}' to a time interval");
}
// The example displays the following output:
// 00:00:49
// Unable to convert '9' to a time interval
// 00:00:06
Dim values() As String = {"49", "9", "06"}
Dim interval As TimeSpan
For Each value As String In values
If TimeSpan.TryParseExact(value, "ss", Nothing, interval) Then
Console.WriteLine(interval.ToString("c"))
Else
Console.WriteLine("Unable to convert '{0}' to a time interval",
value)
End If
Next
' The example displays the following output:
' 00:00:49
' Unable to convert '9' to a time interval
' 00:00:06
Poniższy przykład ilustruje użycie specyfikatora formatu niestandardowego "ss".
TimeSpan interval1 = TimeSpan.FromSeconds(12.60);
Console.WriteLine(interval1.ToString(@"ss\.fff"));
TimeSpan interval2 = TimeSpan.FromSeconds(6.485);
Console.WriteLine(interval2.ToString(@"ss\.fff"));
// The example displays the following output:
// 12.600
// 06.485
Dim interval1 As TimeSpan = TimeSpan.FromSeconds(12.60)
Console.WriteLine(interval1.ToString("ss\.fff"))
Dim interval2 As TimeSpan = TimeSpan.FromSeconds(6.485)
Console.WriteLine(interval2.ToString("ss\.fff"))
' The example displays the following output:
' 12.600
' 06.485
Specyfikator formatu niestandardowego "f"
Specyfikator formatu niestandardowego "f" generuje dziesiąte części sekundy w interwale czasu. W operacji formatowania wszystkie pozostałe cyfry ułamkowe są obcinane. W operacji analizowania, która wywołuje metodę TimeSpan.ParseExact lub TimeSpan.TryParseExact, ciąg wejściowy musi zawierać dokładnie jedną cyfrę ułamkową.
Jeśli specyfikator formatu niestandardowego "f" jest używany samodzielnie, określ "%f", aby nie był błędnie interpretowany jako standardowy ciąg formatu.
W poniższym przykładzie użyto specyfikatora formatu niestandardowego "f", aby wyświetlić dziesiąte części sekundy w wartości TimeSpan. "f" jest używany jako jedyny specyfikator formatu, a następnie połączony ze specyfikatorem "s" w ciągu formatu niestandardowego.
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($"{fmt,10}: {ts.ToString(fmt)}");
}
Console.WriteLine();
for (int ctr = 1; ctr <= 7; ctr++) {
fmt = $"s\\.{new string('f', ctr)}";
Console.WriteLine($"{fmt,10}: {ts.ToString(fmt)}");
}
// 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
Dim ts As New TimeSpan(1003498765432)
Dim fmt As String
Console.WriteLine(ts.ToString("c"))
Console.WriteLine()
For ctr = 1 To 7
fmt = New String("f"c, ctr)
If fmt.Length = 1 Then fmt = "%" + fmt
Console.WriteLine("{0,10}: {1:" + fmt + "}", fmt, ts)
Next
Console.WriteLine()
For ctr = 1 To 7
fmt = New String("f"c, ctr)
Console.WriteLine("{0,10}: {1:s\." + fmt + "}", "s\." + fmt, ts)
Next
' 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
Specyfikator formatu niestandardowego "ff"
Specyfikator formatu niestandardowego "ff" zwraca setne części sekundy w interwale czasu. W operacji formatowania wszystkie pozostałe cyfry ułamkowe są obcinane. W operacji analizowania, która wywołuje metodę TimeSpan.ParseExact lub TimeSpan.TryParseExact, ciąg wejściowy musi zawierać dokładnie dwie cyfry ułamkowe.
W poniższym przykładzie użyto specyfikatora formatu niestandardowego "ff", aby wyświetlić setne części sekundy w wartości TimeSpan. Wyrażenie "ff" jest używane jako jedyny specyfikator formatu, a następnie łączone ze specyfikatorem "s" w ciągu formatu niestandardowego.
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($"{fmt,10}: {ts.ToString(fmt)}");
}
Console.WriteLine();
for (int ctr = 1; ctr <= 7; ctr++) {
fmt = $"s\\.{new string('f', ctr)}";
Console.WriteLine($"{fmt,10}: {ts.ToString(fmt)}");
}
// 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
Dim ts As New TimeSpan(1003498765432)
Dim fmt As String
Console.WriteLine(ts.ToString("c"))
Console.WriteLine()
For ctr = 1 To 7
fmt = New String("f"c, ctr)
If fmt.Length = 1 Then fmt = "%" + fmt
Console.WriteLine("{0,10}: {1:" + fmt + "}", fmt, ts)
Next
Console.WriteLine()
For ctr = 1 To 7
fmt = New String("f"c, ctr)
Console.WriteLine("{0,10}: {1:s\." + fmt + "}", "s\." + fmt, ts)
Next
' 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
Specyfikator formatu niestandardowego "fff"
Specyfikator formatu niestandardowego "fff" (z trzema znakami "f" ) zwraca milisekundy w interwale czasu. W operacji formatowania wszystkie pozostałe cyfry ułamkowe są obcinane. W operacji analizowania, która wywołuje metodę TimeSpan.ParseExact lub TimeSpan.TryParseExact, ciąg wejściowy musi zawierać dokładnie trzy cyfry ułamkowe.
W poniższym przykładzie użyto specyfikatora formatu niestandardowego "fff", aby wyświetlić milisekundy w wartości TimeSpan. "fff" jest używany jako jedyny specyfikator formatu, a następnie połączony ze specyfikatorem "s" w ciągu formatu niestandardowego.
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($"{fmt,10}: {ts.ToString(fmt)}");
}
Console.WriteLine();
for (int ctr = 1; ctr <= 7; ctr++) {
fmt = $"s\\.{new string('f', ctr)}";
Console.WriteLine($"{fmt,10}: {ts.ToString(fmt)}");
}
// 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
Dim ts As New TimeSpan(1003498765432)
Dim fmt As String
Console.WriteLine(ts.ToString("c"))
Console.WriteLine()
For ctr = 1 To 7
fmt = New String("f"c, ctr)
If fmt.Length = 1 Then fmt = "%" + fmt
Console.WriteLine("{0,10}: {1:" + fmt + "}", fmt, ts)
Next
Console.WriteLine()
For ctr = 1 To 7
fmt = New String("f"c, ctr)
Console.WriteLine("{0,10}: {1:s\." + fmt + "}", "s\." + fmt, ts)
Next
' 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
Specyfikator formatu niestandardowego "ffff"
Specyfikator formatu niestandardowego "ffff" (z czterema znakami "f" ) zwraca dziesięć tysięcznych części sekundy w interwale czasu. W operacji formatowania wszystkie pozostałe cyfry ułamkowe są obcinane. W operacji analizowania, która wywołuje metodę TimeSpan.ParseExact lub TimeSpan.TryParseExact, ciąg wejściowy musi zawierać dokładnie cztery cyfry ułamkowe.
W poniższym przykładzie użyto specyfikatora formatu niestandardowego "ffff", aby wyświetlić dziesięć tysięcznych części sekundy w wartości TimeSpan. Wyrażenie "ffff" jest używane jako jedyny specyfikator formatu, a następnie połączone ze specyfikatorem "s" w ciągu formatu niestandardowego.
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($"{fmt,10}: {ts.ToString(fmt)}");
}
Console.WriteLine();
for (int ctr = 1; ctr <= 7; ctr++) {
fmt = $"s\\.{new string('f', ctr)}";
Console.WriteLine($"{fmt,10}: {ts.ToString(fmt)}");
}
// 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
Dim ts As New TimeSpan(1003498765432)
Dim fmt As String
Console.WriteLine(ts.ToString("c"))
Console.WriteLine()
For ctr = 1 To 7
fmt = New String("f"c, ctr)
If fmt.Length = 1 Then fmt = "%" + fmt
Console.WriteLine("{0,10}: {1:" + fmt + "}", fmt, ts)
Next
Console.WriteLine()
For ctr = 1 To 7
fmt = New String("f"c, ctr)
Console.WriteLine("{0,10}: {1:s\." + fmt + "}", "s\." + fmt, ts)
Next
' 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
Specyfikator formatu niestandardowego "fffff"
Specyfikator formatu niestandardowego "fffff" (z pięcioma znakami "f") zwraca liczbę setek tysięcy sekundy w interwale czasu. W operacji formatowania wszystkie pozostałe cyfry ułamkowe są obcinane. W operacji analizowania, która wywołuje metodę TimeSpan.ParseExact lub TimeSpan.TryParseExact, ciąg wejściowy musi zawierać dokładnie pięć cyfr ułamkowych.
W poniższym przykładzie użyto specyfikatora formatu niestandardowego "fffff", aby wyświetlić setki tysięcy sekundy w wartości TimeSpan. "fffff" jest używany jako jedyny specyfikator formatu, a następnie połączony ze specyfikatorem "s" w ciągu formatu niestandardowego.
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($"{fmt,10}: {ts.ToString(fmt)}");
}
Console.WriteLine();
for (int ctr = 1; ctr <= 7; ctr++) {
fmt = $"s\\.{new string('f', ctr)}";
Console.WriteLine($"{fmt,10}: {ts.ToString(fmt)}");
}
// 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
Dim ts As New TimeSpan(1003498765432)
Dim fmt As String
Console.WriteLine(ts.ToString("c"))
Console.WriteLine()
For ctr = 1 To 7
fmt = New String("f"c, ctr)
If fmt.Length = 1 Then fmt = "%" + fmt
Console.WriteLine("{0,10}: {1:" + fmt + "}", fmt, ts)
Next
Console.WriteLine()
For ctr = 1 To 7
fmt = New String("f"c, ctr)
Console.WriteLine("{0,10}: {1:s\." + fmt + "}", "s\." + fmt, ts)
Next
' 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
Specyfikator formatu niestandardowego "ffffff"
Specyfikator formatu niestandardowego "ffffff" (z sześcioma znakami "f") zwraca milionowe części sekundy w interwale czasu. W operacji formatowania wszystkie pozostałe cyfry ułamkowe są obcinane. W operacji analizowania, która wywołuje metodę TimeSpan.ParseExact lub TimeSpan.TryParseExact, ciąg wejściowy musi zawierać dokładnie sześć cyfr ułamkowych.
W poniższym przykładzie użyto specyfikatora formatu niestandardowego "ffffff", aby wyświetlić milionowe części sekundy w wartości TimeSpan. Jest on używany jako jedyny specyfikator formatu, a następnie połączony ze specyfikatorem "s" w ciągu formatu niestandardowego.
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($"{fmt,10}: {ts.ToString(fmt)}");
}
Console.WriteLine();
for (int ctr = 1; ctr <= 7; ctr++) {
fmt = $"s\\.{new string('f', ctr)}";
Console.WriteLine($"{fmt,10}: {ts.ToString(fmt)}");
}
// 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
Dim ts As New TimeSpan(1003498765432)
Dim fmt As String
Console.WriteLine(ts.ToString("c"))
Console.WriteLine()
For ctr = 1 To 7
fmt = New String("f"c, ctr)
If fmt.Length = 1 Then fmt = "%" + fmt
Console.WriteLine("{0,10}: {1:" + fmt + "}", fmt, ts)
Next
Console.WriteLine()
For ctr = 1 To 7
fmt = New String("f"c, ctr)
Console.WriteLine("{0,10}: {1:s\." + fmt + "}", "s\." + fmt, ts)
Next
' 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
Specyfikator formatu niestandardowego "fffffff"
Specyfikator formatu niestandardowego "fffffff" (z siedmioma znakami "f") zwraca dziesięć milionów części sekundy (lub ułamkową liczbę kleszczy) w interwale czasu. W operacji analizowania, która wywołuje metodę TimeSpan.ParseExact lub TimeSpan.TryParseExact, ciąg wejściowy musi zawierać dokładnie siedem cyfr ułamkowych.
W poniższym przykładzie użyto specyfikatora formatu niestandardowego "fffffff", aby wyświetlić ułamkową liczbę znaczników w wartości TimeSpan. Jest on używany jako jedyny specyfikator formatu, a następnie połączony ze specyfikatorem "s" w ciągu formatu niestandardowego.
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($"{fmt,10}: {ts.ToString(fmt)}");
}
Console.WriteLine();
for (int ctr = 1; ctr <= 7; ctr++) {
fmt = $"s\\.{new string('f', ctr)}";
Console.WriteLine($"{fmt,10}: {ts.ToString(fmt)}");
}
// 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
Dim ts As New TimeSpan(1003498765432)
Dim fmt As String
Console.WriteLine(ts.ToString("c"))
Console.WriteLine()
For ctr = 1 To 7
fmt = New String("f"c, ctr)
If fmt.Length = 1 Then fmt = "%" + fmt
Console.WriteLine("{0,10}: {1:" + fmt + "}", fmt, ts)
Next
Console.WriteLine()
For ctr = 1 To 7
fmt = New String("f"c, ctr)
Console.WriteLine("{0,10}: {1:s\." + fmt + "}", "s\." + fmt, ts)
Next
' 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
Specyfikator formatu niestandardowego "F"
Specyfikator formatu niestandardowego "F" generuje dziesiąte części sekundy w interwale czasu. W operacji formatowania wszystkie pozostałe cyfry ułamkowe są obcinane. Jeśli wartość dziesiątych przedziałów czasu sekundy wynosi zero, nie jest uwzględniona w ciągu wynikowym. W operacji analizowania, która wywołuje metodę TimeSpan.ParseExact lub TimeSpan.TryParseExact, obecność dziesiątych cyfry drugiej jest opcjonalna.
Jeśli specyfikator formatu niestandardowego "F" jest używany samodzielnie, określ "%F", aby nie był błędnie interpretowany jako standardowy ciąg formatu.
W poniższym przykładzie użyto specyfikatora formatu niestandardowego "F", aby wyświetlić dziesiąte części sekundy w wartości TimeSpan. Używa również tego specyfikatora formatu niestandardowego w operacji analizowania.
Console.WriteLine("Formatting:");
TimeSpan ts1 = TimeSpan.Parse("0:0:3.669");
Console.WriteLine($"{ts1} ('%F') --> {ts1:%F}");
TimeSpan ts2 = TimeSpan.Parse("0:0:3.091");
Console.WriteLine($"{ts2} ('ss\\.F') --> {ts2:ss\\.F}");
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($"{input} ('{fmt}') --> {ts3}");
else
Console.WriteLine($"Cannot parse {input} with '{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:")
Dim ts1 As TimeSpan = TimeSpan.Parse("0:0:3.669")
Console.WriteLine("{0} ('%F') --> {0:%F}", ts1)
Dim ts2 As TimeSpan = TimeSpan.Parse("0:0:3.091")
Console.WriteLine("{0} ('ss\.F') --> {0:ss\.F}", ts2)
Console.WriteLine()
Console.WriteLine("Parsing:")
Dim inputs() As String = {"0:0:03.", "0:0:03.1", "0:0:03.12"}
Dim fmt As String = "h\:m\:ss\.F"
Dim ts3 As TimeSpan
For Each input As String In inputs
If TimeSpan.TryParseExact(input, fmt, Nothing, ts3)
Console.WriteLine("{0} ('{1}') --> {2}", input, fmt, ts3)
Else
Console.WriteLine("Cannot parse {0} with '{1}'.",
input, fmt)
End If
Next
' 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'.
Specyfikator formatu niestandardowego "FF"
Specyfikator formatu niestandardowego "FF" zwraca setki sekund w interwale czasu. W operacji formatowania wszystkie pozostałe cyfry ułamkowe są obcinane. Jeśli istnieją jakiekolwiek końcowe zera ułamkowe, nie są uwzględniane w ciągu wynikowym. W operacji analizowania, która wywołuje metodę TimeSpan.ParseExact lub TimeSpan.TryParseExact, obecność dziesiątych i setnych cyfry jest opcjonalna.
W poniższym przykładzie użyto specyfikatora formatu niestandardowego "FF", aby wyświetlić setne części sekundy w wartości TimeSpan. Używa również tego specyfikatora formatu niestandardowego w operacji analizowania.
Console.WriteLine("Formatting:");
TimeSpan ts1 = TimeSpan.Parse("0:0:3.697");
Console.WriteLine($"{ts1} ('FF') --> {ts1:FF}");
TimeSpan ts2 = TimeSpan.Parse("0:0:3.809");
Console.WriteLine($"{ts2} ('ss\\.FF') --> {ts2:ss\\.FF}");
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($"{input} ('{fmt}') --> {ts3}");
else
Console.WriteLine($"Cannot parse {input} with '{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:")
Dim ts1 As TimeSpan = TimeSpan.Parse("0:0:3.697")
Console.WriteLine("{0} ('FF') --> {0:FF}", ts1)
Dim ts2 As TimeSpan = TimeSpan.Parse("0:0:3.809")
Console.WriteLine("{0} ('ss\.FF') --> {0:ss\.FF}", ts2)
Console.WriteLine()
Console.WriteLine("Parsing:")
Dim inputs() As String = {"0:0:03.", "0:0:03.1", "0:0:03.127"}
Dim fmt As String = "h\:m\:ss\.FF"
Dim ts3 As TimeSpan
For Each input As String In inputs
If TimeSpan.TryParseExact(input, fmt, Nothing, ts3)
Console.WriteLine("{0} ('{1}') --> {2}", input, fmt, ts3)
Else
Console.WriteLine("Cannot parse {0} with '{1}'.",
input, fmt)
End If
Next
' 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'.
Specyfikator formatu niestandardowego "FFF"
Specyfikator formatu niestandardowego "FFF" (z trzema znakami "F") zwraca milisekundy w przedziale czasu. W operacji formatowania wszystkie pozostałe cyfry ułamkowe są obcinane. Jeśli istnieją jakiekolwiek końcowe zera ułamkowe, nie są uwzględniane w ciągu wynikowym. W operacji analizowania, która wywołuje metodę TimeSpan.ParseExact lub TimeSpan.TryParseExact, obecność dziesiątych, setnych i tysięcznych cyfr jest opcjonalna.
W poniższym przykładzie użyto specyfikatora formatu niestandardowego "FFF" do wyświetlenia tysięcznych części sekundy w wartości TimeSpan. Używa również tego specyfikatora formatu niestandardowego w operacji analizowania.
Console.WriteLine("Formatting:");
TimeSpan ts1 = TimeSpan.Parse("0:0:3.6974");
Console.WriteLine($"{ts1} ('FFF') --> {ts1:FFF}");
TimeSpan ts2 = TimeSpan.Parse("0:0:3.8009");
Console.WriteLine($"{ts2} ('ss\\.FFF') --> {ts2:ss\\.FFF}");
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($"{input} ('{fmt}') --> {ts3}");
else
Console.WriteLine($"Cannot parse {input} with '{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:")
Dim ts1 As TimeSpan = TimeSpan.Parse("0:0:3.6974")
Console.WriteLine("{0} ('FFF') --> {0:FFF}", ts1)
Dim ts2 As TimeSpan = TimeSpan.Parse("0:0:3.8009")
Console.WriteLine("{0} ('ss\.FFF') --> {0:ss\.FFF}", ts2)
Console.WriteLine()
Console.WriteLine("Parsing:")
Dim inputs() As String = {"0:0:03.", "0:0:03.12", "0:0:03.1279"}
Dim fmt As String = "h\:m\:ss\.FFF"
Dim ts3 As TimeSpan
For Each input As String In inputs
If TimeSpan.TryParseExact(input, fmt, Nothing, ts3)
Console.WriteLine("{0} ('{1}') --> {2}", input, fmt, ts3)
Else
Console.WriteLine("Cannot parse {0} with '{1}'.",
input, fmt)
End If
Next
' 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'.
Specyfikator formatu niestandardowego "FFFF"
Specyfikator formatu niestandardowego "FFFF" (z czterema znakami "F" ) zwraca dziesięć tysięcznych części sekundy w interwale czasu. W operacji formatowania wszystkie pozostałe cyfry ułamkowe są obcinane. Jeśli istnieją jakiekolwiek końcowe zera ułamkowe, nie są uwzględniane w ciągu wynikowym. W operacji analizowania, która wywołuje metodę TimeSpan.ParseExact lub TimeSpan.TryParseExact, obecność dziesiątych, setnych, tysięcznych i dziesiątych części drugiej cyfry jest opcjonalna.
W poniższym przykładzie użyto specyfikatora formatu niestandardowego "FFFF", aby wyświetlić dziesięć tysięcznych części sekundy w wartości TimeSpan. Używa również specyfikatora formatu niestandardowego "FFFF" w operacji analizowania.
Console.WriteLine("Formatting:");
TimeSpan ts1 = TimeSpan.Parse("0:0:3.69749");
Console.WriteLine($"{ts1} ('FFFF') --> {ts1:FFFF}");
TimeSpan ts2 = TimeSpan.Parse("0:0:3.80009");
Console.WriteLine($"{ts2} ('ss\\.FFFF') --> {ts2:ss\\.FFFF}");
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($"{input} ('{fmt}') --> {ts3}");
else
Console.WriteLine($"Cannot parse {input} with '{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:")
Dim ts1 As TimeSpan = TimeSpan.Parse("0:0:3.69749")
Console.WriteLine("{0} ('FFFF') --> {0:FFFF}", ts1)
Dim ts2 As TimeSpan = TimeSpan.Parse("0:0:3.80009")
Console.WriteLine("{0} ('ss\.FFFF') --> {0:ss\.FFFF}", ts2)
Console.WriteLine()
Console.WriteLine("Parsing:")
Dim inputs() As String = {"0:0:03.", "0:0:03.12", "0:0:03.12795"}
Dim fmt As String = "h\:m\:ss\.FFFF"
Dim ts3 As TimeSpan
For Each input As String In inputs
If TimeSpan.TryParseExact(input, fmt, Nothing, ts3)
Console.WriteLine("{0} ('{1}') --> {2}", input, fmt, ts3)
Else
Console.WriteLine("Cannot parse {0} with '{1}'.",
input, fmt)
End If
Next
' 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'.
Specyfikator formatu niestandardowego "FFFFF"
Specyfikator formatu niestandardowego "FFFFF" (z pięcioma znakami "F") zwraca liczbę setek tysięcy sekundy w interwale czasu. W operacji formatowania wszystkie pozostałe cyfry ułamkowe są obcinane. Jeśli istnieją jakiekolwiek końcowe zera ułamkowe, nie są uwzględniane w ciągu wynikowym. W operacji analizowania, która wywołuje metodę TimeSpan.ParseExact lub TimeSpan.TryParseExact, obecność dziesiątych, setnych, tysięcznych, dziesiątych i setnych tysięcy drugiej cyfry jest opcjonalna.
W poniższym przykładzie użyto specyfikatora formatu niestandardowego "FFFFF", aby wyświetlić setki tysięcy sekundy w wartości TimeSpan. Używa również specyfikatora formatu niestandardowego "FFFFF" w operacji analizowania.
Console.WriteLine("Formatting:");
TimeSpan ts1 = TimeSpan.Parse("0:0:3.697497");
Console.WriteLine($"{ts1} ('FFFFF') --> {ts1:FFFFF}");
TimeSpan ts2 = TimeSpan.Parse("0:0:3.800009");
Console.WriteLine($"{ts2} ('ss\\.FFFFF') --> {ts2:ss\\.FFFFF}");
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($"{input} ('{fmt}') --> {ts3}");
else
Console.WriteLine($"Cannot parse {input} with '{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:")
Dim ts1 As TimeSpan = TimeSpan.Parse("0:0:3.697497")
Console.WriteLine("{0} ('FFFFF') --> {0:FFFFF}", ts1)
Dim ts2 As TimeSpan = TimeSpan.Parse("0:0:3.800009")
Console.WriteLine("{0} ('ss\.FFFFF') --> {0:ss\.FFFFF}", ts2)
Console.WriteLine()
Console.WriteLine("Parsing:")
Dim inputs() As String = {"0:0:03.", "0:0:03.12", "0:0:03.127956"}
Dim fmt As String = "h\:m\:ss\.FFFFF"
Dim ts3 As TimeSpan
For Each input As String In inputs
If TimeSpan.TryParseExact(input, fmt, Nothing, ts3)
Console.WriteLine("{0} ('{1}') --> {2}", input, fmt, ts3)
Else
Console.WriteLine("Cannot parse {0} with '{1}'.",
input, fmt)
End If
Next
' 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'.
Specyfikator formatu niestandardowego "FFFFFF"
Specyfikator formatu niestandardowego "FFFFFF" (z sześcioma znakami "F") zwraca milionowe części sekundy w interwale czasu. W operacji formatowania wszystkie pozostałe cyfry ułamkowe są obcinane. Jeśli istnieją jakiekolwiek końcowe zera ułamkowe, nie są uwzględniane w ciągu wynikowym. W operacji analizowania, która wywołuje metodę TimeSpan.ParseExact lub TimeSpan.TryParseExact, obecność dziesiątych, setnych, tysięcznych, dziesięciotysięgowych, setnych i milionowych cyfr jest opcjonalna.
W poniższym przykładzie użyto specyfikatora formatu niestandardowego "FFFFFF", aby wyświetlić milionowe części sekundy w wartości TimeSpan. Używa również tego specyfikatora formatu niestandardowego w operacji analizowania.
Console.WriteLine("Formatting:");
TimeSpan ts1 = TimeSpan.Parse("0:0:3.6974974");
Console.WriteLine($"{ts1} ('FFFFFF') --> {ts1:FFFFFF}");
TimeSpan ts2 = TimeSpan.Parse("0:0:3.8000009");
Console.WriteLine($"{ts2} ('ss\\.FFFFFF') --> {ts2:ss\\.FFFFFF}");
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($"{input} ('{fmt}') --> {ts3}");
else
Console.WriteLine($"Cannot parse {input} with '{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:")
Dim ts1 As TimeSpan = TimeSpan.Parse("0:0:3.6974974")
Console.WriteLine("{0} ('FFFFFF') --> {0:FFFFFF}", ts1)
Dim ts2 As TimeSpan = TimeSpan.Parse("0:0:3.8000009")
Console.WriteLine("{0} ('ss\.FFFFFF') --> {0:ss\.FFFFFF}", ts2)
Console.WriteLine()
Console.WriteLine("Parsing:")
Dim inputs() As String = {"0:0:03.", "0:0:03.12", "0:0:03.1279569"}
Dim fmt As String = "h\:m\:ss\.FFFFFF"
Dim ts3 As TimeSpan
For Each input As String In inputs
If TimeSpan.TryParseExact(input, fmt, Nothing, ts3)
Console.WriteLine("{0} ('{1}') --> {2}", input, fmt, ts3)
Else
Console.WriteLine("Cannot parse {0} with '{1}'.",
input, fmt)
End If
Next
' 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'.
Specyfikator formatu niestandardowego "FFFFFFF"
Specyfikator formatu niestandardowego "FFFFFFF" (z siedmioma znakami "F") zwraca dziesięć milionów części sekundy (lub ułamkową liczbę znaczników) w przedziale czasu. Jeśli istnieją jakiekolwiek końcowe zera ułamkowe, nie są uwzględniane w ciągu wynikowym. W operacji analizowania, która wywołuje metodę TimeSpan.ParseExact lub TimeSpan.TryParseExact, obecność siedmiu cyfr ułamkowych w ciągu wejściowym jest opcjonalna.
W poniższym przykładzie użyto specyfikatora formatu niestandardowego "FFFFFFF", aby wyświetlić części ułamkowe sekundy w wartości TimeSpan. Używa również tego specyfikatora formatu niestandardowego w operacji analizowania.
Console.WriteLine("Formatting:");
TimeSpan ts1 = TimeSpan.Parse("0:0:3.6974974");
Console.WriteLine($"{ts1} ('FFFFFFF') --> {ts1:FFFFFFF}");
TimeSpan ts2 = TimeSpan.Parse("0:0:3.9500000");
Console.WriteLine($"{ts2} ('ss\\.FFFFFFF') --> {ts2:ss\\.FFFFFFF}");
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($"{input} ('{fmt}') --> {ts3}");
else
Console.WriteLine($"Cannot parse {input} with '{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
Console.WriteLine("Formatting:")
Dim ts1 As TimeSpan = TimeSpan.Parse("0:0:3.6974974")
Console.WriteLine("{0} ('FFFFFFF') --> {0:FFFFFFF}", ts1)
Dim ts2 As TimeSpan = TimeSpan.Parse("0:0:3.9500000")
Console.WriteLine("{0} ('ss\.FFFFFFF') --> {0:ss\.FFFFFFF}", ts2)
Console.WriteLine()
Console.WriteLine("Parsing:")
Dim inputs() As String = {"0:0:03.", "0:0:03.12", "0:0:03.1279569"}
Dim fmt As String = "h\:m\:ss\.FFFFFFF"
Dim ts3 As TimeSpan
For Each input As String In inputs
If TimeSpan.TryParseExact(input, fmt, Nothing, ts3)
Console.WriteLine("{0} ('{1}') --> {2}", input, fmt, ts3)
Else
Console.WriteLine("Cannot parse {0} with '{1}'.",
input, fmt)
End If
Next
' 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
Inne znaki
Każdy inny niewyobrażony znak w ciągu formatu, w tym znak odstępu, jest interpretowany jako specyfikator formatu niestandardowego. W większości przypadków obecność innego niewyobrażnego znaku powoduje FormatException.
Istnieją dwa sposoby uwzględnienia znaku literału w ciągu formatu:
Ująć go w pojedynczy cudzysłów (ogranicznik ciągu literału).
Poprzedzić go ukośnikiem odwrotnym ("\"), który jest interpretowany jako znak ucieczki. Oznacza to, że w języku C#ciąg formatu musi być @-quoted lub znak literału musi być poprzedzony dodatkowym ukośnikiem odwrotnym.
W niektórych przypadkach może być konieczne użycie logiki warunkowej w celu uwzględnienia literału ucieczki w ciągu formatu. W poniższym przykładzie użyto logiki warunkowej do uwzględnienia symbolu znaku dla ujemnych interwałów czasu.
using System; public class Example { public static void Main() { TimeSpan result = new DateTime(2010, 01, 01) - DateTime.Now; String fmt = (result < TimeSpan.Zero ? "\\-" : "") + "dd\\.hh\\:mm"; Console.WriteLine(result.ToString(fmt)); Console.WriteLine($"Interval: {result.ToString(fmt)}"); } } // The example displays output like the following: // -5582.12:21 // Interval: -5582.12:21
Module Example Public Sub Main() Dim result As TimeSpan = New DateTime(2010, 01, 01) - Date.Now Dim fmt As String = If(result < TimeSpan.Zero, "\-", "") + "dd\.hh\:mm" Console.WriteLine(result.ToString(fmt)) Console.WriteLine("Interval: {0:" + fmt + "}", result) End Sub End Module ' The example displays output like the following: ' -1291.10:54 ' Interval: -1291.10:54
Platforma .NET nie definiuje gramatyki separatorów w interwałach czasu. Oznacza to, że separatory między dniami i godzinami, godzinami i minutami, minutami i sekundami oraz ułamkami sekundy muszą być traktowane jako literały znaków w ciągu formatu.
W poniższym przykładzie użyto zarówno znaku ucieczki, jak i pojedynczego cudzysłowu do zdefiniowania niestandardowego ciągu formatu zawierającego wyraz "minutes" w ciągu wyjściowym.
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
Dim interval As New TimeSpan(0, 32, 45)
' Escape literal characters in a format string.
Dim fmt As String = "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
Zobacz też
- Typy formatowania
- ciągi formatu TimeSpan w warstwie Standardowa