Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert timespan to decimal?

I have a value of exactly 1.08:43:23 in my textbox, which is equal to 1d, 08:43:23. I wanted to convert the value to decimal for me to multiply it by another decimal value, however when I used Convert.ToDecimal it returns the error

Input string is not a valid format

Is Convert.ToDecimal not appropriate for this kind of conversion ? Is there a way to create a decimal value from such input?

like image 791
pruuylan Avatar asked Oct 23 '15 07:10

pruuylan


2 Answers

Is Convert.ToDecimal is not appropriate for this kind of conversion ?

No. You need to parse it to TimeSpan first (with a culture that has : as a TimeSeparator of course.). Then you can get which duration type do you want as a double from it.

var ts = TimeSpan.Parse("1.08:43:23", CultureInfo.InvariantCulture);

enter image description here

Then you can use TotalXXX properties which type duration do you want as a double (seconds, milliseconds etc.).

like image 163
Soner Gönül Avatar answered Oct 19 '22 23:10

Soner Gönül


This will give u total number of ticks ..

decimal dec = Convert.ToDecimal(TimeSpan.Parse("11:30").Ticks);
like image 34
Kishore Sahasranaman Avatar answered Oct 20 '22 00:10

Kishore Sahasranaman