Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have this structure {HH:MM}
Example:
C#
string duracion = "26:30";
TimeSpan duraciontime = TimeSpan.Parse(duracion);
int result = Convert.ToInt32(duraciontime.TotalMinutes);

If the hours < 24 I haven't got problem but if the hours > 24 doesn't work.
At the moment I am testing with TryParse.
¿More solutions?
Thanks so much.
Posted
Comments
Sinisa Hajnal 11-Nov-14 7:30am    
So, how it doesn't work? For your example above 26:30 what do you get as result?
Adrián Cejudo 11-Nov-14 7:34am    
I get a OverflowException.
For example If I have 22:30 It success
Afzaal Ahmad Zeeshan 11-Nov-14 7:47am    
That is because there is no 26th hour in .NET or in real life. You can only pass hours equal to or less than 24. You can however pass 26th minute to the TimeSpan object.

See my answer to your question. Solution 1.

Try this :
C#
string duration = "26:30";
string[] a = duration.Split(':');
if (a.length >= 2) {
int result = int.Parse(a[0])*60 + int.Parse(a[1]);
}
 
Share this answer
 
v3
Comments
Adrián Cejudo 11-Nov-14 8:00am    
I like this solution.
I did a do while to get the minutes, but this code is cleaner.
Thank you for your comment
Sinisa Hajnal 11-Nov-14 8:38am    
You should check if there are at least two elements...what if you get empty string or only 22 from the service due to some error. Always check outside input before using it. I've added the check, but now you have to pay attention to result scope :)
I have handled this in the past by using this method:
C#
private TimeSpan TimeSpanEx(int days = 0, int hours = 0, int minutes = 0, int seconds = 0, int milliseconds = 0)
{
    if (milliseconds > 999)
    {
        seconds += milliseconds/1000;
        milliseconds = milliseconds%1000;
    }

    if (seconds > 59)
    {
        minutes += seconds / 60;
        seconds = seconds % 60;
    }

    if (minutes > 59)
    {
        hours += minutes / 60;
        minutes = minutes % 60;
    }

    if (hours > 23)
    {
        days += hours / 24;
        hours = hours % 24;
    }

    return new TimeSpan(days, hours, minutes, seconds, milliseconds);
}
So, if I wanted a TimeSpan object that expressed 25 hours, and 125 minutes:
C#
TimeSpan tSpan = TimeSpanEx(hours: 25, minutes:125);

> ? tSpan
{1.03:05:00}
    Days: 1
    Hours: 3
    Milliseconds: 0
    Minutes: 5
    Seconds: 0
    Ticks: 975000000000
    TotalDays: 1.1284722222222221
    TotalHours: 27.083333333333332
    TotalMilliseconds: 97500000.0
    TotalMinutes: 1625.0
    TotalSeconds: 97500.0
 
Share this answer
 
You're using Parse(), not TryParse(). And the error is because there is no 26th hour in .NET (also in our time). So when you pass 26:30 it throws error. You can however pass 26 as a minute, like this "00:26:30".

This[^] documentation from MSDN has the examples of different string representations of the TimeSpan that you can pass. Let me write them here too.

C#
string[] values = { "6", "6:12", "6:12:14", "6:12:14:45", 
                    "6.12:14:45", "6:12:14:45.3448", 
                    "6:12:14:45,3448", "6:34:14:45" };

//    Current Culture: en-US 
//    6 --> 6.00:00:00 
//    6:12 --> 06:12:00 
//    6:12:14 --> 06:12:14 
//    6:12:14:45 --> 6.12:14:45 
//    6.12:14:45 --> 6.12:14:45 
//    6:12:14:45.3448 --> 6.12:14:45.3448000 
//    6:12:14:45,3448: Bad Format 
//    6:34:14:45: Overflow


So, you see, passing "26:30" would cause the .NET to think you're passing a 26th hour to it. I have created a dotnetfiddle for you test it, here[^] it is. You can see the third result in the console (bottom) it has the 26th minute kind of thing. In the third instance the minutes are 26. So using them in your code would return 26 minutes to your application.
 
Share this answer
 
v2
Comments
Adrián Cejudo 11-Nov-14 7:58am    
My problem is that 26 are hours and 30 are minutes, and this structure can't change.
Thank you for your comment
Afzaal Ahmad Zeeshan 11-Nov-14 7:59am    
But can you please tell me, how can 26 hours be possible?
Adrián Cejudo 11-Nov-14 8:12am    
Is a structure from a REST web service.
I have 26:00 Instead of 01:02:00. This WS is not mine.
Afzaal Ahmad Zeeshan 11-Nov-14 8:14am    
So you can always convert it to the format you want. Use a condition where you check for the minutes or hours, instead of Parse use TryParse and check whether it is a true or a false. :-)

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