Datatype for 1:00 ( number of Hours) in c#

722 views Asked by At

I have column in Excel which has the number of hours in Time Format. For example- 2:00, 3:00 My job is to get those numbers from excel and put them into SQL. so what is the data type that I need to used to get those values in c# code. I have tried a couple of ways! It says - "Cannot convert Double to string" or "Cannot convert Double to TimeSpan" Please let me know what do I need to do.

Thanks

1

There are 1 answers

1
D Stanley On

Excel only uses effectively 2 data types when data is read from C# - numbers and strings. Dates are usually passed as numbers which represent a number of days (including fractional days to represent time) since 1/1/1900, and can easily be converted to DateTime using the FromOADate method. You can then use the TimeOfDay property to get just the time as a TimeSpan object, which has a TotalHours property that represents the number of hours (and fractional hours) the time span represents.

Another method would be to take the fractional part of the number and multiply it by 24.