Win a copy of Functional Reactive Programming this week in the Other Languages forum!

# I want to create hours calculator

rushikesh kalikar
Greenhorn
Posts: 14
we are designing timesheet management system.in that we have to calculate hours given by user ..
we done something but its not working properly...i will not calculate hours when starthours and endhours are same.
please help me

function calculatehours(){

var startworkhours = document.getElementById("firsthourstart").value;
var startworkminutes = document.getElementById("firstminutestart").value;
var endworkhours = document.getElementById("firsthourend").value;
var endworkminutes = document.getElementById("firstminuteend").value;

if(endworkhours>=startworkhours){

if(startworkhours!="Select" && startworkminutes!="Select" && endworkhours!="Select" && endworkminutes!="Select"){

var totHours1 = 60*(startworkhours)+(1*startworkminutes);
var totHours2 = 60*(endworkhours)+(1*endworkminutes);

var linearminutesworked = (totHours2-totHours1);

var hoursWorked = parseInt(linearminutesworked/60);
var minutesWorked = linearminutesworked-(hoursWorked*60);

if (minutesWorked < 10){
minutesWorked = "0"+minutesWorked;
}

document.getElementById("totalTime").value = hoursWorked+":"+minutesWorked;
}
}else(startworkhours>endworkhours){
alert('Invalid Information');document.timeWorked.value='';return false}
}

Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34973
379
Welcome to CodeRanch!

Which is the first line where the variable isn't what you expect it to be? (You can add an alert() to print out the values to find this out.)

Bear Bibeault
Author and ninkuma
Marshal
Posts: 65220
95
Or step through the code with the browser's debugger.

rushikesh kalikar
Greenhorn
Posts: 14
I want to calculate time upto 'n' but by using 'getElementById' we have to create new one for each ....
Please tell me if there is any other way ...
In this case can i use 'getElementByName' ?

Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34973
379
What do you mean by "up to n"? I don't see an "n" in your example.

Using byName might not help you. How will you know which text fields go with each other? Can you explain more about what you are trying to do?