Forum Moderators: open
monday: 900am, 1130am, 1230pm, 500pm
and do that for the entire week, and add them up. I'm basically just trying to make it so i can put in the in, out, in, out for each day of the week and then return the total number of hours worked. Is this an easy thing to set up with JavaScript? would i need to convert the numbers to military time first, or does JavaScript have some built-in features to work with time? I took the initiative on this project for my superiors at work, so your help is deeply appreciated. Thank you!
[edited by: mylungsarempty at 12:16 am (utc) on Feb. 24, 2004]
I was not clear on the format of the input, but the function below takes a string in the format of:
begin time,endtime;begintime,endtime;
where commas separate times, and semicolons separate shifts
var workMinutes=0
function simpleTime(){
timeString='9:00,12:00;13:00,17:30;6:30,12:00;13:00,18:00'
dayArray=timeString.split(';')
for(i=0;i<dayArray.length;i++){
workArray=dayArray[i].split(',')
startArray=workArray[0].split(':')
startMins=startArray[0]*60+startArray[1]*1
endArray=workArray[1].split(':')
endMins=endArray[0]*60+endArray[1]*1
workMins=endMins-startMins
workMinutes=workMinutes+workMins
}
alert('total work is '+parseInt(workMinutes/60)+' hours and '+workMinutes%60+' minutes')
}
Hope this helps,
ajkimoto
<html>
<head>
<title>Payroll</title>
<script language="Javascript">
function calculate(arg) {
arg.expr.value = eval((arg.am_out.value)-(arg.am_in.value))
}
</script>
</head>
<body>
<form>
<input type="text" name="am_in" size="5"><BR>
<input type="text" name="am_out" size="5"><BR>
<BR>
<input type="text" name="pm_in" size="5"><BR>
<input type="text" name="pm_out" size="5"><BR>
<BR>
<input type="button" value="calculate this" onClick="calculate(this.form)"><BR>
<input type="text" name="expr" size="35" action="calculate(this.form)">
</form>
</body>
</html>
What i would like to achieve is the subtraction of am_in from am_out, the subtraction of pm_in from pm_out, and then the sum of those two results. I have never really hand coded Javascript before... it's always been "copy, paste, edit"... I've invested my time in learning PHP, but i can't use that for this endeavor.
I'm still having trouble understanding how i define the number as a time of day... Simply entering 830 for eight in the morning instead of entering 8:30 is exactly what i'm trying to do, but what do i say to let JavaScript know that 830 is eight-and-one-half-hours from midnight, and not eight-hundred-and-thirty of something (base 60 instead of 100).
I'm also sure that finding the difference beween the AM times and PM times then adding them together isn't too complicated, i'm just not sure what the syntax is.
in_ampm = in_.substring(in_.length-2);
in_minute = (in_.substring((in_.length-4),3) * 1);
in_hour = (in_.substring(0,(in_.length-4)) * 1) + (in_ampm=="pm"?12:0);
out_ampm = out_.substring(out_.length-2);
out_minute = (out_.substring((out_.length-4),3) * 1);
out_hour = (out_.substring(0,(out_.length-4)) * 1) + (out_ampm=="pm"?12:0);
foobar = new Date();
in_timeObject = new Date(foobar.setHours(in_hour));
in_timeObject = in_timeObject.setMinutes(in_minute);
// in_timeObject is now a Unix timestamp
// let's do the same with the out time
out_timeObject = new Date(foobar.setHours(out_hour));
out_timeObject = out_timeObject.setMinutes(out_minute);
// now you just need to subtract...
timeDiff = (out_timeObject - in_timeObject)/1000;
// timeDiff holds the difference (in seconds) between the "in" and "out" times
document.write(in_hour + " h<br>" + in_minute + " m<br>" + in_ampm + "<br>" + out_hour + " h<br>" + out_minute + " m<br>" + out_ampm + "<br>" + in_timeObject + "<br>" + out_timeObject + "<br>" + timeDiff + "<br>")
</script>
<added>Sorry, there were a couple errors in the script...</added>
[edited by: DrDoc at 7:46 pm (utc) on Feb. 25, 2004]
The only thing that bothers me about it is that it treats 1200pm (noon) as if it is 1200am (midnight)... this isn't right! heheh any idea how i can fix that one?
<update> i just realized that 0000pm is noon. This is interesting... This little script however isn't for me, it's for a co-worker to use... so i just want to make it as simple as possible. </update>