Click here to Skip to main content
15,936,602 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a string list of time duration. which have two string-
"01:33:56:00","00:23:34:00"

When i adding this list value, i got exception-
The TimeSpan could not be parsed because at least one of the numeric components is out of range or contains too many digits.

I am using this-
Quote:
olst.TotleQcDuration = olst.Reports.Aggregate(TimeSpan.Zero, (subtotal, t) => subtotal.Add(TimeSpan.Parse(t.Duration))).ToString();

What I have tried:

C#
olst.TotleQcDuration = olst.Reports.Aggregate(TimeSpan.Zero, (subtotal, t) => subtotal.Add(TimeSpan.Parse(t.Duration))).ToString();
Posted
Updated 21-Dec-16 19:25pm
v2
Comments
Peter Leow 22-Dec-16 1:07am    
Where and how are these "001:33:56:00","00:23:34:00" stored?

Try-
"01:33:56:00" instead of "001:33:56:00"
That extra 0 can cause the problem.

Hope, it helps :)
 
Share this answer
 
Comments
mukesh mourya 22-Dec-16 1:05am    
Sorry, it is Only "01:33:56:00".
Suvendu Shekhar Giri 22-Dec-16 1:09am    
and you still getting the error?
mukesh mourya 22-Dec-16 1:10am    
Yes
Where and how are these "01:33:56:00","00:23:34:00" stored?
Check this out TimeSpan.Parse Method (String) (System)[^], pay particular attention to the parameter format under Remarks
[ws][-]{ d | [d.]hh:mm[:ss[.ff]] }[ws]
 
Share this answer
 
Comments
Suvendu Shekhar Giri 22-Dec-16 1:17am    
Yes.. makes sense. my 5!
Hi Mukesh,

Your first parameter is causing an overflow exception. The value after the first colon of "001:33:56:00" is an hour value and it must be less than 24 (yours is 33). Please correct it and trap it using Try-Catch:
C#
try
{
    olst.TotleQcDuration = olst.Reports.Aggregate(TimeSpan.Zero, (subtotal, t) => subtotal.Add(TimeSpan.Parse(t.Duration))).ToString();
}
catch (OverflowException)
{
    MessageBox.Show("A value in the string exceeds a legitimate value.");
}
 
Share this answer
 
v3

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