Forum Moderators: open

Message Too Old, No Replies

military time

trying to write a time-clock script

         

mylungsarempty

9:31 pm on Feb 23, 2004 (gmt 0)

10+ Year Member



I'm trying to figure out a way to set up a script that will let me take values like

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]

mylungsarempty

12:15 am on Feb 24, 2004 (gmt 0)

10+ Year Member



my questions always stump =Z

ajkimoto

7:54 pm on Feb 24, 2004 (gmt 0)

10+ Year Member



mylungsarempty,

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

mylungsarempty

6:57 pm on Feb 25, 2004 (gmt 0)

10+ Year Member



Ajkimoto, I couldn't get that to work for some reason. It looks logical. Here's the approach I've been taking with it:

<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.

DrDoc

7:01 pm on Feb 25, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



military time

You realize there's no such thing as 500pm, right?
500 = 5am
1700 = 5pm

And since 24-hour format is JavaScript native, you don't have to do any conversion. Just concatenate the hour + minute without the colon between them and - voila - you have a military style clock. :)

mylungsarempty

7:13 pm on Feb 25, 2004 (gmt 0)

10+ Year Member



those values i gave in the beginning weren't intended to be military. just regular average-guy time, minus the colon.

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.

DrDoc

7:22 pm on Feb 25, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<script type="text/javascript">
in_ = "830am";
out_ = "125pm";

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]

mylungsarempty

7:44 pm on Feb 25, 2004 (gmt 0)

10+ Year Member



I'm not sure that's all right... When i print the value of timeDiff, it gives me -42600. I thought that perhaps if i divide it by 60 twice, it would give me the number of hours, but it doesn't... plus, it's a negative number, anyway.. so.. i'm stumped.

mylungsarempty

1:06 pm on Feb 26, 2004 (gmt 0)

10+ Year Member



The script certainly works quite well after you've made your little adjustments :)

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>