Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: working out the decimal time

1. ## working out the decimal time

hey guys,

I have an application where a user enters a start time, and an end time in 24 hours format, eg:

startTime = 14.00
endTime = 15.30

Using the function below, I can work out that they've worked for 1.5 hours. the problem is, if you look at the code, its fairly basic maths..... so when they enter

startTime = 12.30
endTime = 13.00

the calculation says they've done 1.5 hours!!
Is there a better way to do this, or is there a way I can work out against a time function ....

Code:
```function calculateHours() {

//work out the fractional times
if (document.frm.endTime.value == "")   { endTime = "0.00";   } else { endTime = document.frm.endTime.value;     }
if (document.frm.startTime.value == "") { startTime = "0.00"; } else { startTime = document.frm.startTime.value; }

if ((document.frm.endTime.value == "") || (document.frm.endTime.value == "")) {
message = "Could not calculate - Both start and end times must be entered";
} else {
//times
if (document.frm.startTime.value > document.frm.endTime.value) {

message = "Could not calculate - The start time cannot be greater than the end time";

} else {

var startArray = startTime.split(".");
var endArray = endTime.split(".");

var totalHours = endArray[0] - startArray[0];

if (parseInt(endArray[1]) > parseInt(startArray[1])) {
var totalMinutes = parseInt(endArray[1]) - parseInt(startArray[1]);
}

if (parseInt(endArray[1]) == parseInt(startArray[1])) {
var totalMinutes = 0;
}

if (parseInt(endArray[1]) < parseInt(startArray[1])) {
var totalMinutes = (60-parseInt(startArray[1])) + parseInt(endArray[1]);
}

//now we have the minutes and the hours, we can work out the fraction
var fraction = (totalMinutes / 60)*100;
var fractionalTime = totalHours + "." + fraction;

message = "Your hours are: " + parseFloat(fractionalTime).toFixed(2);

}
}

}```
thanks for any help

• Specify the optional radix parameter of parseInt(string, [radix]) method. Specify 10 as the base. If not specified, you may get unexpected results if the string starts at 0 which will be parsed as an octal number. I'm not sure though if this causes the wrong computation. Try fixing that and we'll see.

• thanks.... but not sure how that will help me? Some times maybe 08.00 etc...

• Code:
```function calculateHours() {
if ((document.frm.endTime.value == "")
|| (document.frm.endTime.value == "")) {
message = "Could not calculate - Both start and end times must be entered";
return false;
}

//times
if (document.frm.startTime.value > document.frm.endTime.value) {
message = "Could not calculate - The start time cannot be greater than the end time";
return false;
}

var startArray = startTime.split(".");
var endArray = endTime.split(".");

var startminutes = startArray[0]*60 + startArray[1]*1;
var endminutes   = endArray[0]*60   + endArray[1]*1;
var totalminutes = endminutes - startminutes;

var totalhours   = Math.floor(totalminutes / 60);
var totalminutes = totalminutes%60;
var fractionalTime = totalHours + "." + fraction;

message = "Your hours are: " + parseFloat(fractionalTime).toFixed(2);

}```

• cheers..that seems to work ok(Ish)... there's a problem though for example if you enter

start = 12.30
end = 13.45

it says the answer is 1.13 when it should be 1.25

your code seemed to not convert the minutes in to a fraction... eg: start = 12.30 end = 13.00 produced "0.30" when it should be 0.50... I added:

fractionMinutes = (totalminutes/60)*100;

which does the job... so the code now reads:

Code:
```	if (document.frm.endTime.value == "")   { endTime = "0.00";   } else { endTime = document.frm.endTime.value;     }
if (document.frm.startTime.value == "") { startTime = "0.00"; } else { startTime = document.frm.startTime.value; }

if ((document.frm.endTime.value == "") || (document.frm.endTime.value == "")) {
message = "Could not calculate - Both start and end times must be entered";
}

//times
if (document.frm.startTime.value > document.frm.endTime.value) {
message = "Could not calculate - The start time cannot be greater than the end time";
}

var startArray = startTime.split(".");
var endArray = endTime.split(".");

//minutes
var startminutes = startArray[0]*60 + startArray[1]*1;
var endminutes   = endArray[0]*60   + endArray[1]*1;
var totalminutes = endminutes - startminutes;
var fractionMinutes = (totalminutes/60)*100;

var totalhours   = Math.floor(totalminutes / 60);
var totalminutes = totalminutes%60;
var fractionalTime = totalhours + "." + fractionMinutes;

message = "Your hours are: " + parseFloat(fractionalTime).toFixed(2);