Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert decimal representing time to hour, minutes, seconds

Tags:

date

math

time

I have a decimal. The range of this decimal is between 0 and 23.999999. This decimal represents a time. For example, if the decimal is 0.25, then the time it represents is 12:15 AM. If the decimal is 23.50, the time it represents is 11:30 PM.

I have three variables: - Hours - Minutes - Seconds

Using this decimal, how do I fill in the Hours, Minutes, and Seconds values?

like image 714
DotNetDateQuestion Avatar asked Dec 10 '22 09:12

DotNetDateQuestion


1 Answers

Well, here's an answer in C#, but it's generally the same idea in most languages:

int hours = (int)hoursDecimal;
decimal minutesDecimal = ((hoursDecimal - hours) * 60);
int minutes = (int)minutesDecimal;
int seconds = (int)((minutesDecimal - minutes) * 60);
like image 74
Domenic Avatar answered May 12 '23 17:05

Domenic