Format TimeSpan greater than 24 hour
Well, the simplest thing to do is to format this yourself, e.g.
return string.Format("{0}hr {1}mn {2}sec",
(int) span.TotalHours,
span.Minutes,
span.Seconds);
In VB:
Public Shared Function FormatTimeSpan(span As TimeSpan) As String
Return String.Format("{0}hr {1}mn {2}sec", _
CInt(Math.Truncate(span.TotalHours)), _
span.Minutes, _
span.Seconds)
End Function
I don't know whether any of the TimeSpan
formatting in .NET 4 would make this simpler.
How to display Hours and Minutes from timespan with more than 24 hours
Looking at MSDN, you can't do it with a standard .ToString() format.
The TotalHours property of a timespan seems to be what you need to get from days to hours.
From MSDN:
TimeSpan interval = new TimeSpan(1, 15, 42, 45, 750);
Console.WriteLine("Value of TimeSpan: {0}", interval);Console.WriteLine("{0:N5} hours, as follows:", interval.TotalHours);
Console.WriteLine(" Hours: {0,3}",
interval.Days * 24 + interval.Hours);
Console.WriteLine(" Minutes: {0,3}", interval.Minutes);
Console.WriteLine(" Seconds: {0,3}", interval.Seconds);
Console.WriteLine(" Milliseconds: {0,3}", interval.Milliseconds);
So in your case:
FormClock.Text = string.Format("{0}:{1}:{2}". TimeToInstall.Days * 24 + TimeToInstall.Hours, TimeToInstall.Minutes, TimeToInstall.Seconds);
If it's something you're going to use a lot you could look at rolling it into some sort of extension method.
How to parse string with hours greater than 24 to TimeSpan?
If you're certain that the format will always be "HH:mm" then try something like this:
string span = "35:15";
TimeSpan ts = new TimeSpan(int.Parse(span.Split(':')[0]), // hours
int.Parse(span.Split(':')[1]), // minutes
0); // seconds
Parse a TimeSpan greater than 24 hours?
I'm not sure it can be done using standard TimeSpan.Parse()
method, but you can do that:
public static decimal Hours(string s)
{
decimal r;
if (decimal.TryParse(s, out r))
return r;
var parts = s.Split(':');
return (decimal)new TimeSpan(int.Parse(parts[0]), int.Parse(parts[1]),0).TotalHours;
}
Converting TimeSpan to hh:mm higher than 23:59
You won't be able to format a TimeSpan
object via .ToString(format)
overload the way you'd like.
Look at Microsoft doc, hh format cannot exceed 23 :
hh The number of hours, which ranges from "00" to "23".
You will have to build the format you want using .TotalHours.
What about:
var ts = new TimeSpan(hours: 50, minutes: 10, seconds: 20);
var hours = Math.Truncate(ts.TotalHours);
var formatted = $"{hours}:{ts.Minutes}:{ts.Seconds}";
Console.WriteLine(formatted); // Display 50:10:20
You could even create an extension method.
Format timespan with hours going above 24 instead of using a days part
You just need to do the calculation:
Dim secs = 86400
Dim ts = TimeSpan.FromSeconds(secs)
Dim res = String.Format("{0}:{1:00}:{2:00}", (ts.Days * 24 + ts.Hours), ts.Minutes, ts.Seconds)
Console.WriteLine(res)
Outputs:
24:00:00
If you want at least two digits for the hours, use "{0:00}:{1:00}:{2:00}"
.
(You could use String.Format("{0}:{1:00}:{2:00}", Math.Floor(ts.TotalHours), ts.Minutes, ts.Seconds)
if you think it looks better.)
Related Topics
What Is Managed or Unmanaged Code in Programming
The Type or Namespace Name Does Not Exist in the Namespace 'System.Web.Mvc'
C# 6.0 Features Not Working with Visual Studio 2015
Passing a Value from One Form to Another Form
How to Loop Through Ienumerable in Batches
Entity Framework 4 Delete Object from Entity Collection
Import and Export Excel - What Is the Best Library
Redirect from Action Filter Attribute
Does Anyone Still Use [Goto] in C# and If So Why
Reliably Stop System.Threading.Timer