Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get total no seconds from a string in time format greater than 24hrs using C#

I need to get total no of sec from string in time span format greater than 24 hrs. Below is code snipped i used

static void Main(string[] args)
    {
        string s = "24:55:00.00";
        double d = (int)TimeSpan.Parse(s).TotalSeconds;
        Console.WriteLine(d);            
    }

when i ran above getting Exception OverflowException was unhandled . when i use string less than 24hrs say like 23:55:05.09 . code is working fine. is it is real limitation of C# or i am missing something

Thanks

like image 978
RamKumar Avatar asked Jan 20 '26 00:01

RamKumar


2 Answers

You can not use more than 23h in hours

OverflowException s represents a number that is less than TimeSpan.MinValue or greater than TimeSpan.MaxValue. -or- At least one of the days, hours, minutes, or seconds components is outside its valid range.

see here at MSDN

Max hours is 23, minutes 60 etc.

change your string to:

1:0:55:00.00

it will be equal to 24h55m

like image 99
Kamil Budziewski Avatar answered Jan 22 '26 13:01

Kamil Budziewski


To be able to parse a string representation of a time span of 24 hours and 55 minutes you have to use this string:

1.0:55:00.00

If you are unsure about the string representation used by TimeSpan you can perform the reverse conversion:

(TimeSpan.FromDays(1) + TimeSpan.FromMinutes(55)).ToString()

This will return the string:

1.0:55:00
like image 38
Martin Liversage Avatar answered Jan 22 '26 12:01

Martin Liversage



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!