What is the recommended way of formatting TimeSpan
objects into a string with a custom format?
将TimeSpan对象格式化为具有自定义格式的字符串的推荐方法是什么?
19 个解决方案
#1
Please note: this answer is for .Net 4.0 and above. If you want to format a TimeSpan in .Net 3.5 or below please see JohannesH's answer.
请注意:此答案适用于.Net 4.0及更高版本。如果您想在.Net 3.5或更低版本中格式化TimeSpan,请参阅JohannesH的答案。
Custom TimeSpan format strings were introduced in .Net 4.0. You can find a full reference of available format specifiers at the MSDN Custom TimeSpan Format Strings page.
自定义TimeSpan格式字符串是在.Net 4.0中引入的。您可以在MSDN Custom TimeSpan Format Strings页面上找到可用格式说明符的完整参考。
Here's an example timespan format string:
这是一个示例时间跨度格式字符串:
string.Format("{0:hh\\:mm\\:ss}", myTimeSpan); //example output 15:36:15
(UPDATE) and here is an example using C# 6 string interpolation:
(更新),这是一个使用C#6字符串插值的示例:
$"{myTimeSpan:hh\\:mm\\:ss}"; //example output 15:36:15
You need to escape the ":" character with a "\" (which itself must be escaped unless you're using a verbatim string).
您需要使用“\”转义“:”字符(除非您使用逐字字符串,否则必须对其进行转义)。
This excerpt from the MSDN Custom TimeSpan Format Strings page explains about escaping the ":" and "." characters in a format string:
MSDN Custom TimeSpan Format Strings页面的摘录解释了有关转义“:”和“。”的内容。格式字符串中的字符:
The custom TimeSpan format specifiers do not include placeholder separator symbols, such as the symbols that separate days from hours, hours from minutes, or seconds from fractional seconds. Instead, these symbols must be included in the custom format string as string literals. For example, "dd.hh:mm" defines a period (.) as the separator between days and hours, and a colon (:) as the separator between hours and minutes.
自定义TimeSpan格式说明符不包括占位符分隔符符号,例如将小时数与小时数,小时数与分数小时数或秒数与小数秒数分隔开的符号。相反,这些符号必须作为字符串文字包含在自定义格式字符串中。例如,“dd.hh:mm”将句点(。)定义为天和小时之间的分隔符,冒号(:)作为小时和分钟之间的分隔符。
#2
For .NET 3.5 and lower you could use:
对于.NET 3.5及更低版本,您可以使用:
string.Format ("{0:00}:{1:00}:{2:00}", (int)myTimeSpan.TotalHours, myTimeSpan.Minutes, myTimeSpan.Seconds);
Code taken from a Jon Skeet answer on bytes
代码取自Jon Skeet的字节答案
For .NET 4.0 and above, see DoctaJonez answer.
对于.NET 4.0及更高版本,请参阅DoctaJonez答案。
#3
One way is to create a DateTime
object and use it for formatting:
一种方法是创建一个DateTime对象并将其用于格式化:
new DateTime(myTimeSpan.Ticks).ToString(myCustomFormat)// or using String.Format:String.Format("{0:HHmmss}", new DateTime(myTimeSpan.Ticks))
This is the way I know. I hope someone can suggest a better way.
这就是我所知道的。我希望有人能提出更好的方法。
#4
Simple. Use TimeSpan.ToString
with c, g or G. More information at MSDN
简单。使用带有c,g或G的TimeSpan.ToString。更多信息,请访问MSDN
#5
I would go with
我会去
myTimeSpan.ToString("hh\\:mm\\:ss");
#6
Dim duration As New TimeSpan(1, 12, 23, 62)DEBUG.WriteLine("Time of Travel: " + duration.ToString("dd\.hh\:mm\:ss"))
It works for Framework 4
它适用于Framework 4
#7
Personally, I like this approach:
就个人而言,我喜欢这种方法:
TimeSpan ts = ...;string.Format("{0:%d}d {0:%h}h {0:%m}m {0:%s}s", ts);
You can make this as custom as you like with no problems:
您可以根据需要将其设置为自定义,而不会出现任何问题:
string.Format("{0:%d}days {0:%h}hours {0:%m}min {0:%s}sec", ts);string.Format("{0:%d}d {0:%h}h {0:%m}' {0:%s}''", ts);
#8
This is awesome one:
这太棒了:
string.Format("{0:00}:{1:00}:{2:00}", (int)myTimeSpan.TotalHours, myTimeSpan.Minutes, myTimeSpan.Seconds);
#9
You can also go with:
你也可以选择:
Dim ts As New TimeSpan(35, 21, 59, 59) '(11, 22, 30, 30) 'Dim TimeStr1 As String = String.Format("{0:c}", ts)Dim TimeStr2 As String = New Date(ts.Ticks).ToString("dd.HH:mm:ss")
EDIT:
You can also look at Strings.Format.
您还可以查看Strings.Format。
Dim ts As New TimeSpan(23, 30, 59) Dim str As String = Strings.Format(New DateTime(ts.Ticks), "H:mm:ss")
#10
if (timeSpan.TotalDays < 1) return timeSpan.ToString(@"hh\:mm\:ss");return timeSpan.TotalDays < 2 ? timeSpan.ToString(@"d\ \d\a\y\ hh\:mm\:ss") : timeSpan.ToString(@"d\ \d\a\y\s\ hh\:mm\:ss");
All literal characters must be escaped.
必须转义所有文字字符。
#11
I used the code below. It is long, but still it is one expression, and produces very friendly output, as it does not outputs days, hours, minutes, or seconds if they have value of zero.
我使用下面的代码。它很长,但它仍然是一个表达式,并且产生非常友好的输出,因为如果它们的值为零,它不会输出天,小时,分钟或秒。
In the sample it produces output: "4 days 1 hour 3 seconds".
在样本中它产生输出:“4天1小时3秒”。
TimeSpan sp = new TimeSpan(4,1,0,3);string.Format("{0}{1}{2}{3}", sp.Days > 0 ? ( sp.Days > 1 ? sp.ToString(@"d\ \d\a\y\s\ "): sp.ToString(@"d\ \d\a\y\ ")):string.Empty, sp.Hours > 0 ? (sp.Hours > 1 ? sp.ToString(@"h\ \h\o\u\r\s\ ") : sp.ToString(@"h\ \h\o\u\r\ ")):string.Empty, sp.Minutes > 0 ? (sp.Minutes > 1 ? sp.ToString(@"m\ \m\i\n\u\t\e\s\ ") :sp.ToString(@"m\ \m\i\n\u\t\e\ ")):string.Empty, sp.Seconds > 0 ? (sp.Seconds > 1 ? sp.ToString(@"s\ \s\e\c\o\n\d\s"): sp.ToString(@"s\ \s\e\c\o\n\d\s")):string.Empty);
#12
I use this method. I'm Belgian and speak dutch so plural of hours and minutes is not just adding 's' to the end but almost a different word than singular.
我用这种方法。我是比利时人并说荷兰语,所以多个小时和分钟不仅仅是添加's'到最后,而是几乎与单数不同。
It may seem long but it is very readable I think:
它可能看起来很长但我认为它非常易读:
public static string SpanToReadableTime(TimeSpan span) { string[] values = new string[4]; //4 slots: days, hours, minutes, seconds StringBuilder readableTime = new StringBuilder(); if (span.Days > 0) { if (span.Days == 1) values[0] = span.Days.ToString() + " dag"; //day else values[0] = span.Days.ToString() + " dagen"; //days readableTime.Append(values[0]); readableTime.Append(", "); } else values[0] = String.Empty; if (span.Hours > 0) { if (span.Hours == 1) values[1] = span.Hours.ToString() + " uur"; //hour else values[1] = span.Hours.ToString() + " uren"; //hours readableTime.Append(values[1]); readableTime.Append(", "); } else values[1] = string.Empty; if (span.Minutes > 0) { if (span.Minutes == 1) values[2] = span.Minutes.ToString() + " minuut"; //minute else values[2] = span.Minutes.ToString() + " minuten"; //minutes readableTime.Append(values[2]); readableTime.Append(", "); } else values[2] = string.Empty; if (span.Seconds > 0) { if (span.Seconds == 1) values[3] = span.Seconds.ToString() + " seconde"; //second else values[3] = span.Seconds.ToString() + " seconden"; //seconds readableTime.Append(values[3]); } else values[3] = string.Empty; return readableTime.ToString(); }//end SpanToReadableTime
#13
Here is my extension method:
这是我的扩展方法:
public static string ToFormattedString(this TimeSpan ts){ const string separator = ", "; if (ts.TotalMilliseconds < 1) { return "No time"; } return string.Join(separator, new string[] { ts.Days > 0 ? ts.Days + (ts.Days > 1 ? " days" : " day") : null, ts.Hours > 0 ? ts.Hours + (ts.Hours > 1 ? " hours" : " hour") : null, ts.Minutes > 0 ? ts.Minutes + (ts.Minutes > 1 ? " minutes" : " minute") : null, ts.Seconds > 0 ? ts.Seconds + (ts.Seconds > 1 ? " seconds" : " second") : null, ts.Milliseconds > 0 ? ts.Milliseconds + (ts.Milliseconds > 1 ? " milliseconds" : " millisecond") : null, }.Where(t => t != null));}
Example call:
string time = new TimeSpan(3, 14, 15, 0, 65).ToFormattedString();
Output:
3 days, 14 hours, 15 minutes, 65 milliseconds
#14
This is a pain in VS 2010, here's my workaround solution.
这是VS 2010的痛苦,这是我的解决方案。
public string DurationString { get { if (this.Duration.TotalHours < 24) return new DateTime(this.Duration.Ticks).ToString("HH:mm"); else //If duration is more than 24 hours { double totalminutes = this.Duration.TotalMinutes; double hours = totalminutes / 60; double minutes = this.Duration.TotalMinutes - (Math.Floor(hours) * 60); string result = string.Format("{0}:{1}", Math.Floor(hours).ToString("00"), Math.Floor(minutes).ToString("00")); return result; } } }
#15
This is the approach I used my self with conditional formatting. and I post it here because I think this is clean way.
这是我使用条件格式的方法。我在这里发布,因为我认为这是干净的方式。
$"{time.Days:#0:;;\\}{time.Hours:#0:;;\\}{time.Minutes:00:}{time.Seconds:00}"
example of outputs:
产出的例子:
00:00
(minimum)
1:43:04
(when we have hours)1:43:04(我们有几个小时的时候)
15:03:01
(when hours are more than 1 digit)15:03:01(小时数超过1位数)
2:4:22:04
(when we have days.)2:4:22:04(我们有几天的时候。)
The formatting is easy. time.Days:#0:;;\\
the format before ;;
is for when value is positive. negative values are ignored. and for zero values we have;;\\
in order to hide it in formatted string. note that the escaped backslash is necessary otherwise it will not format correctly.
格式化很容易。 time.Days:#0:;; \\格式之前;;当价值为正时。负值被忽略。对于零值,我们有;; \\以便将其隐藏在格式化字符串中。请注意,转义反斜杠是必要的,否则它将无法正确格式化。
#16
Here is my version. It shows only as much as necessary, handles pluralization, negatives, and I tried to make it lightweight.
这是我的版本。它只显示必要的,处理复数,负面,我试图使它轻量级。
Output Examples
0 seconds1.404 seconds1 hour, 14.4 seconds14 hours, 57 minutes, 22.473 seconds1 day, 14 hours, 57 minutes, 22.475 seconds
Code
public static class TimeSpanExtensions{ public static string ToReadableString(this TimeSpan timeSpan) { int days = (int)(timeSpan.Ticks / TimeSpan.TicksPerDay); long subDayTicks = timeSpan.Ticks % TimeSpan.TicksPerDay; bool isNegative = false; if (timeSpan.Ticks < 0L) { isNegative = true; days = -days; subDayTicks = -subDayTicks; } int hours = (int)((subDayTicks / TimeSpan.TicksPerHour) % 24L); int minutes = (int)((subDayTicks / TimeSpan.TicksPerMinute) % 60L); int seconds = (int)((subDayTicks / TimeSpan.TicksPerSecond) % 60L); int subSecondTicks = (int)(subDayTicks % TimeSpan.TicksPerSecond); double fractionalSeconds = (double)subSecondTicks / TimeSpan.TicksPerSecond; var parts = new List<string>(4); if (days > 0) parts.Add(string.Format("{0} day{1}", days, days == 1 ? null : "s")); if (hours > 0) parts.Add(string.Format("{0} hour{1}", hours, hours == 1 ? null : "s")); if (minutes > 0) parts.Add(string.Format("{0} minute{1}", minutes, minutes == 1 ? null : "s")); if (fractionalSeconds.Equals(0D)) { switch (seconds) { case 0: // Only write "0 seconds" if we haven't written anything at all. if (parts.Count == 0) parts.Add("0 seconds"); break; case 1: parts.Add("1 second"); break; default: parts.Add(seconds + " seconds"); break; } } else { parts.Add(string.Format("{0}{1:.###} seconds", seconds, fractionalSeconds)); } string resultString = string.Join(", ", parts); return isNegative ? "(negative) " + resultString : resultString; }}
#17
If you want the duration format similar to youtube, given the number of seconds
如果您希望持续时间格式与youtube类似,则给定秒数
int[] duration = { 0, 4, 40, 59, 60, 61, 400, 4000, 40000, 400000 };foreach (int d in duration){ Console.WriteLine("{0, 6} -> {1, 10}", d, d > 59 ? TimeSpan.FromSeconds(d).ToString().TrimStart("00:".ToCharArray()) : string.Format("0:{0:00}", d));}
Output:
0 -> 0:00 4 -> 0:04 40 -> 0:40 59 -> 0:59 60 -> 1:00 61 -> 1:01 400 -> 6:40 4000 -> 1:06:40 40000 -> 11:06:40400000 -> 4.15:06:40
#18
I wanted to return a string such as "1 day 2 hours 3 minutes" and also take into account if for example days or minuttes are 0 and then not showing them. thanks to John Rasch for his answer which mine is barely an extension of
我想返回一个字符串,如“1天2小时3分钟”,并考虑例如天或微小是否为0然后不显示它们。感谢John Rasch的回答,我的回答几乎没有
TimeSpan timeLeft = New Timespan(0, 70, 0);String.Format("{0}{1}{2}{3}{4}{5}", Math.Floor(timeLeft.TotalDays) == 0 ? "" : Math.Floor(timeLeft.TotalDays).ToString() + " ", Math.Floor(timeLeft.TotalDays) == 0 ? "" : Math.Floor(timeLeft.TotalDays) == 1 ? "day " : "days ", timeLeft.Hours == 0 ? "" : timeLeft.Hours.ToString() + " ", timeLeft.Hours == 0 ? "" : timeLeft.Hours == 1 ? "hour " : "hours ", timeLeft.Minutes == 0 ? "" : timeLeft.Minutes.ToString() + " ", timeLeft.Minutes == 0 ? "" : timeLeft.Minutes == 1 ? "minute " : "minutes ");
#19
The Substring
method works perfectly when you only want the Hours:Minutes:Seconds. It's simple, clean code and easy to understand.
当您只需要Hours:Minutes:Seconds时,Substring方法可以正常工作。它简单,干净的代码,易于理解。
var yourTimeSpan = DateTime.Now - DateTime.Now.AddMinutes(-2); var formatted = yourTimeSpan.ToString().Substring(0,8);// 00:00:00 Console.WriteLine(formatted);
#1
Please note: this answer is for .Net 4.0 and above. If you want to format a TimeSpan in .Net 3.5 or below please see JohannesH's answer.
请注意:此答案适用于.Net 4.0及更高版本。如果您想在.Net 3.5或更低版本中格式化TimeSpan,请参阅JohannesH的答案。
Custom TimeSpan format strings were introduced in .Net 4.0. You can find a full reference of available format specifiers at the MSDN Custom TimeSpan Format Strings page.
自定义TimeSpan格式字符串是在.Net 4.0中引入的。您可以在MSDN Custom TimeSpan Format Strings页面上找到可用格式说明符的完整参考。
Here's an example timespan format string:
这是一个示例时间跨度格式字符串:
string.Format("{0:hh\\:mm\\:ss}", myTimeSpan); //example output 15:36:15
(UPDATE) and here is an example using C# 6 string interpolation:
(更新),这是一个使用C#6字符串插值的示例:
$"{myTimeSpan:hh\\:mm\\:ss}"; //example output 15:36:15
You need to escape the ":" character with a "\" (which itself must be escaped unless you're using a verbatim string).
您需要使用“\”转义“:”字符(除非您使用逐字字符串,否则必须对其进行转义)。
This excerpt from the MSDN Custom TimeSpan Format Strings page explains about escaping the ":" and "." characters in a format string:
MSDN Custom TimeSpan Format Strings页面的摘录解释了有关转义“:”和“。”的内容。格式字符串中的字符:
The custom TimeSpan format specifiers do not include placeholder separator symbols, such as the symbols that separate days from hours, hours from minutes, or seconds from fractional seconds. Instead, these symbols must be included in the custom format string as string literals. For example, "dd.hh:mm" defines a period (.) as the separator between days and hours, and a colon (:) as the separator between hours and minutes.
自定义TimeSpan格式说明符不包括占位符分隔符符号,例如将小时数与小时数,小时数与分数小时数或秒数与小数秒数分隔开的符号。相反,这些符号必须作为字符串文字包含在自定义格式字符串中。例如,“dd.hh:mm”将句点(。)定义为天和小时之间的分隔符,冒号(:)作为小时和分钟之间的分隔符。
#2
For .NET 3.5 and lower you could use:
对于.NET 3.5及更低版本,您可以使用:
string.Format ("{0:00}:{1:00}:{2:00}", (int)myTimeSpan.TotalHours, myTimeSpan.Minutes, myTimeSpan.Seconds);
Code taken from a Jon Skeet answer on bytes
代码取自Jon Skeet的字节答案
For .NET 4.0 and above, see DoctaJonez answer.
对于.NET 4.0及更高版本,请参阅DoctaJonez答案。
#3
One way is to create a DateTime
object and use it for formatting:
一种方法是创建一个DateTime对象并将其用于格式化:
new DateTime(myTimeSpan.Ticks).ToString(myCustomFormat)// or using String.Format:String.Format("{0:HHmmss}", new DateTime(myTimeSpan.Ticks))
This is the way I know. I hope someone can suggest a better way.
这就是我所知道的。我希望有人能提出更好的方法。
#4
Simple. Use TimeSpan.ToString
with c, g or G. More information at MSDN
简单。使用带有c,g或G的TimeSpan.ToString。更多信息,请访问MSDN
#5
I would go with
我会去
myTimeSpan.ToString("hh\\:mm\\:ss");
#6
Dim duration As New TimeSpan(1, 12, 23, 62)DEBUG.WriteLine("Time of Travel: " + duration.ToString("dd\.hh\:mm\:ss"))
It works for Framework 4
它适用于Framework 4
#7
Personally, I like this approach:
就个人而言,我喜欢这种方法:
TimeSpan ts = ...;string.Format("{0:%d}d {0:%h}h {0:%m}m {0:%s}s", ts);
You can make this as custom as you like with no problems:
您可以根据需要将其设置为自定义,而不会出现任何问题:
string.Format("{0:%d}days {0:%h}hours {0:%m}min {0:%s}sec", ts);string.Format("{0:%d}d {0:%h}h {0:%m}' {0:%s}''", ts);
#8
This is awesome one:
这太棒了:
string.Format("{0:00}:{1:00}:{2:00}", (int)myTimeSpan.TotalHours, myTimeSpan.Minutes, myTimeSpan.Seconds);
#9
You can also go with:
你也可以选择:
Dim ts As New TimeSpan(35, 21, 59, 59) '(11, 22, 30, 30) 'Dim TimeStr1 As String = String.Format("{0:c}", ts)Dim TimeStr2 As String = New Date(ts.Ticks).ToString("dd.HH:mm:ss")
EDIT:
You can also look at Strings.Format.
您还可以查看Strings.Format。
Dim ts As New TimeSpan(23, 30, 59) Dim str As String = Strings.Format(New DateTime(ts.Ticks), "H:mm:ss")
#10
if (timeSpan.TotalDays < 1) return timeSpan.ToString(@"hh\:mm\:ss");return timeSpan.TotalDays < 2 ? timeSpan.ToString(@"d\ \d\a\y\ hh\:mm\:ss") : timeSpan.ToString(@"d\ \d\a\y\s\ hh\:mm\:ss");
All literal characters must be escaped.
必须转义所有文字字符。
#11
I used the code below. It is long, but still it is one expression, and produces very friendly output, as it does not outputs days, hours, minutes, or seconds if they have value of zero.
我使用下面的代码。它很长,但它仍然是一个表达式,并且产生非常友好的输出,因为如果它们的值为零,它不会输出天,小时,分钟或秒。
In the sample it produces output: "4 days 1 hour 3 seconds".
在样本中它产生输出:“4天1小时3秒”。
TimeSpan sp = new TimeSpan(4,1,0,3);string.Format("{0}{1}{2}{3}", sp.Days > 0 ? ( sp.Days > 1 ? sp.ToString(@"d\ \d\a\y\s\ "): sp.ToString(@"d\ \d\a\y\ ")):string.Empty, sp.Hours > 0 ? (sp.Hours > 1 ? sp.ToString(@"h\ \h\o\u\r\s\ ") : sp.ToString(@"h\ \h\o\u\r\ ")):string.Empty, sp.Minutes > 0 ? (sp.Minutes > 1 ? sp.ToString(@"m\ \m\i\n\u\t\e\s\ ") :sp.ToString(@"m\ \m\i\n\u\t\e\ ")):string.Empty, sp.Seconds > 0 ? (sp.Seconds > 1 ? sp.ToString(@"s\ \s\e\c\o\n\d\s"): sp.ToString(@"s\ \s\e\c\o\n\d\s")):string.Empty);
#12
I use this method. I'm Belgian and speak dutch so plural of hours and minutes is not just adding 's' to the end but almost a different word than singular.
我用这种方法。我是比利时人并说荷兰语,所以多个小时和分钟不仅仅是添加's'到最后,而是几乎与单数不同。
It may seem long but it is very readable I think:
它可能看起来很长但我认为它非常易读:
public static string SpanToReadableTime(TimeSpan span) { string[] values = new string[4]; //4 slots: days, hours, minutes, seconds StringBuilder readableTime = new StringBuilder(); if (span.Days > 0) { if (span.Days == 1) values[0] = span.Days.ToString() + " dag"; //day else values[0] = span.Days.ToString() + " dagen"; //days readableTime.Append(values[0]); readableTime.Append(", "); } else values[0] = String.Empty; if (span.Hours > 0) { if (span.Hours == 1) values[1] = span.Hours.ToString() + " uur"; //hour else values[1] = span.Hours.ToString() + " uren"; //hours readableTime.Append(values[1]); readableTime.Append(", "); } else values[1] = string.Empty; if (span.Minutes > 0) { if (span.Minutes == 1) values[2] = span.Minutes.ToString() + " minuut"; //minute else values[2] = span.Minutes.ToString() + " minuten"; //minutes readableTime.Append(values[2]); readableTime.Append(", "); } else values[2] = string.Empty; if (span.Seconds > 0) { if (span.Seconds == 1) values[3] = span.Seconds.ToString() + " seconde"; //second else values[3] = span.Seconds.ToString() + " seconden"; //seconds readableTime.Append(values[3]); } else values[3] = string.Empty; return readableTime.ToString(); }//end SpanToReadableTime
#13
Here is my extension method:
这是我的扩展方法:
public static string ToFormattedString(this TimeSpan ts){ const string separator = ", "; if (ts.TotalMilliseconds < 1) { return "No time"; } return string.Join(separator, new string[] { ts.Days > 0 ? ts.Days + (ts.Days > 1 ? " days" : " day") : null, ts.Hours > 0 ? ts.Hours + (ts.Hours > 1 ? " hours" : " hour") : null, ts.Minutes > 0 ? ts.Minutes + (ts.Minutes > 1 ? " minutes" : " minute") : null, ts.Seconds > 0 ? ts.Seconds + (ts.Seconds > 1 ? " seconds" : " second") : null, ts.Milliseconds > 0 ? ts.Milliseconds + (ts.Milliseconds > 1 ? " milliseconds" : " millisecond") : null, }.Where(t => t != null));}
Example call:
string time = new TimeSpan(3, 14, 15, 0, 65).ToFormattedString();
Output:
3 days, 14 hours, 15 minutes, 65 milliseconds
#14
This is a pain in VS 2010, here's my workaround solution.
这是VS 2010的痛苦,这是我的解决方案。
public string DurationString { get { if (this.Duration.TotalHours < 24) return new DateTime(this.Duration.Ticks).ToString("HH:mm"); else //If duration is more than 24 hours { double totalminutes = this.Duration.TotalMinutes; double hours = totalminutes / 60; double minutes = this.Duration.TotalMinutes - (Math.Floor(hours) * 60); string result = string.Format("{0}:{1}", Math.Floor(hours).ToString("00"), Math.Floor(minutes).ToString("00")); return result; } } }
#15
This is the approach I used my self with conditional formatting. and I post it here because I think this is clean way.
这是我使用条件格式的方法。我在这里发布,因为我认为这是干净的方式。
$"{time.Days:#0:;;\\}{time.Hours:#0:;;\\}{time.Minutes:00:}{time.Seconds:00}"
example of outputs:
产出的例子:
00:00
(minimum)
1:43:04
(when we have hours)1:43:04(我们有几个小时的时候)
15:03:01
(when hours are more than 1 digit)15:03:01(小时数超过1位数)
2:4:22:04
(when we have days.)2:4:22:04(我们有几天的时候。)
The formatting is easy. time.Days:#0:;;\\
the format before ;;
is for when value is positive. negative values are ignored. and for zero values we have;;\\
in order to hide it in formatted string. note that the escaped backslash is necessary otherwise it will not format correctly.
格式化很容易。 time.Days:#0:;; \\格式之前;;当价值为正时。负值被忽略。对于零值,我们有;; \\以便将其隐藏在格式化字符串中。请注意,转义反斜杠是必要的,否则它将无法正确格式化。
#16
Here is my version. It shows only as much as necessary, handles pluralization, negatives, and I tried to make it lightweight.
这是我的版本。它只显示必要的,处理复数,负面,我试图使它轻量级。
Output Examples
0 seconds1.404 seconds1 hour, 14.4 seconds14 hours, 57 minutes, 22.473 seconds1 day, 14 hours, 57 minutes, 22.475 seconds
Code
public static class TimeSpanExtensions{ public static string ToReadableString(this TimeSpan timeSpan) { int days = (int)(timeSpan.Ticks / TimeSpan.TicksPerDay); long subDayTicks = timeSpan.Ticks % TimeSpan.TicksPerDay; bool isNegative = false; if (timeSpan.Ticks < 0L) { isNegative = true; days = -days; subDayTicks = -subDayTicks; } int hours = (int)((subDayTicks / TimeSpan.TicksPerHour) % 24L); int minutes = (int)((subDayTicks / TimeSpan.TicksPerMinute) % 60L); int seconds = (int)((subDayTicks / TimeSpan.TicksPerSecond) % 60L); int subSecondTicks = (int)(subDayTicks % TimeSpan.TicksPerSecond); double fractionalSeconds = (double)subSecondTicks / TimeSpan.TicksPerSecond; var parts = new List<string>(4); if (days > 0) parts.Add(string.Format("{0} day{1}", days, days == 1 ? null : "s")); if (hours > 0) parts.Add(string.Format("{0} hour{1}", hours, hours == 1 ? null : "s")); if (minutes > 0) parts.Add(string.Format("{0} minute{1}", minutes, minutes == 1 ? null : "s")); if (fractionalSeconds.Equals(0D)) { switch (seconds) { case 0: // Only write "0 seconds" if we haven't written anything at all. if (parts.Count == 0) parts.Add("0 seconds"); break; case 1: parts.Add("1 second"); break; default: parts.Add(seconds + " seconds"); break; } } else { parts.Add(string.Format("{0}{1:.###} seconds", seconds, fractionalSeconds)); } string resultString = string.Join(", ", parts); return isNegative ? "(negative) " + resultString : resultString; }}
#17
If you want the duration format similar to youtube, given the number of seconds
如果您希望持续时间格式与youtube类似,则给定秒数
int[] duration = { 0, 4, 40, 59, 60, 61, 400, 4000, 40000, 400000 };foreach (int d in duration){ Console.WriteLine("{0, 6} -> {1, 10}", d, d > 59 ? TimeSpan.FromSeconds(d).ToString().TrimStart("00:".ToCharArray()) : string.Format("0:{0:00}", d));}
Output:
0 -> 0:00 4 -> 0:04 40 -> 0:40 59 -> 0:59 60 -> 1:00 61 -> 1:01 400 -> 6:40 4000 -> 1:06:40 40000 -> 11:06:40400000 -> 4.15:06:40
#18
I wanted to return a string such as "1 day 2 hours 3 minutes" and also take into account if for example days or minuttes are 0 and then not showing them. thanks to John Rasch for his answer which mine is barely an extension of
我想返回一个字符串,如“1天2小时3分钟”,并考虑例如天或微小是否为0然后不显示它们。感谢John Rasch的回答,我的回答几乎没有
TimeSpan timeLeft = New Timespan(0, 70, 0);String.Format("{0}{1}{2}{3}{4}{5}", Math.Floor(timeLeft.TotalDays) == 0 ? "" : Math.Floor(timeLeft.TotalDays).ToString() + " ", Math.Floor(timeLeft.TotalDays) == 0 ? "" : Math.Floor(timeLeft.TotalDays) == 1 ? "day " : "days ", timeLeft.Hours == 0 ? "" : timeLeft.Hours.ToString() + " ", timeLeft.Hours == 0 ? "" : timeLeft.Hours == 1 ? "hour " : "hours ", timeLeft.Minutes == 0 ? "" : timeLeft.Minutes.ToString() + " ", timeLeft.Minutes == 0 ? "" : timeLeft.Minutes == 1 ? "minute " : "minutes ");
#19
The Substring
method works perfectly when you only want the Hours:Minutes:Seconds. It's simple, clean code and easy to understand.
当您只需要Hours:Minutes:Seconds时,Substring方法可以正常工作。它简单,干净的代码,易于理解。
var yourTimeSpan = DateTime.Now - DateTime.Now.AddMinutes(-2); var formatted = yourTimeSpan.ToString().Substring(0,8);// 00:00:00 Console.WriteLine(formatted);