Click here to Skip to main content
15,898,371 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How can I do this? :( I want TS be in this format "00:00:00"
C#
TimeSpan TS =TimeSpan.Parse("140:43") /30;
Posted
Updated 3-Mar-14 4:08am
v2
Comments
Thomas Daniels 3-Mar-14 10:22am    
Will the timespan always have three digits for the hours and two digits for the minutes?
Maciej Los 3-Mar-14 10:28am    
What?

1 solution

Try this:
C#
string input = "140:43";
string[] parts = input.Split(':');
int hours = int.Parse(parts[0]);
int minutes = int.Parse(parts[1]);
int seconds;
if (parts.Length < 3)
{
    seconds = 0;
}
else
{
    seconds = int.Parse(parts[2]);
}
TimeSpan TS = new TimeSpan(hours, minutes, seconds);
TimeSpan resultAfterDivision = new TimeSpan(TS.Ticks / 30);

First, you need to split the given string. The first part is the number of hours and the second part the number of minutes. If no third part is given, the number of seconds is 0. Otherwise, the number of seconds is the third part of the input.
Then, to divide the TimeSpan, you need to divide its number of ticks.
 
Share this answer
 
Comments
Maciej Los 3-Mar-14 10:42am    
+5!
Thomas Daniels 3-Mar-14 10:44am    
Thank you!
mit62 3-Mar-14 10:54am    
Thank you :)
Thomas Daniels 3-Mar-14 11:11am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900