Excel "fraction of 24 hours" to VB datetime

reasra

New member
Joined
Oct 2, 2009
Messages
2
Programming Experience
1-3
OK, I've looked all over Bing and Google and cannot find a straight answer for this one.

I've got a .net application that reads in a time value from Excel 2000 (hopefully we'll upgrade to 2007 before long). Because I'm not sure that the value will be formatted correctly, I need to get the .value (not .text) of the cell.

My problem is converting the value of the cell to hh:mm:ss, because Excel is giving me a fraction of 24 hours (0.5 = 12:00pm).

I wrote a little function that multiplies the number by 24 to get hours. Then, multiplies the remainder by 60 to get minutes. Then, multiplies that remainder by 60 again to get seconds. The problem lies in the inconsistency of the seconds. Sometimes it rounds up and sometimes down even though I'm using the Math.Floor function.

If anyone knows of a function that will do this automatically, that'd be great. If anyone knows of a better way to calculate the time, that'd be good also.

VB.NET:
hour = Math.Floor(time * 24)
minute = Math.Floor(((time * 24) - hour) * 60)
second = Math.Floor(((((time * 24) - hour) * 60) - minute) * 60)
return hour.ToString & ":" & minute.ToString & ":" & second.ToString

Thanks,
Bob
 
Back
Top