/    Sign up×
Community /Pin to ProfileBookmark

Select lists and time totals

I have a set of selects for a from hour, From minute, From Meridiem, To Hour, To Minute, To Meridiem.

What I would liek to do is take the times selected in the six selects and figure out the total time between them and display it on the screen.

so if someone selects 9:00am to 12:00pm the total time will be 3hrs. If they change their selections, I want this figure to update as well.

here is the layout of this html…I just need some direction on what JS functions to use to figure out the duration between the two times.

[code]
<form name=event>
<table width=500 align=center border=0>
<tr>
<td colspan=3 align=center>
<hr>
<b>1st Shift</b>
</td>
</tr>
<tr>
<td align=”right”>
Work From:
</td>
<td width=200>
<select name=”from_hour”>”;

for ($i = 1; $i <= 12; $i++)
{
//set a default of 8 am
if ($i == 8)
$value .= “<option value=””.$i.”” selected>”.$i.”</option>”;

if ($i <> 8)
$value .= “<option value=””.$i.””>”.$i.”</option>”;
}

$value .= “</select>
<select name=”from_min”>
<option value=”00″ selected>:00</option>
<option value=”15″>:15</option>
<option value=”30″>:30</option>
<option value=”45″>:45</option>
</select>
<select name=”from_merid”>
<option value=”AM” selected>am</option>
<option value=”PM”>pm</option>

</select>
</td>
</tr>
<tr>
<td align=”right”>
Work To :
</td>
<td>
<select name=”to_hour”>”;

for ($i = 1; $i <= 12; $i++)
{
$value .= “<option value=””.$i.”” >”.$i.”</option>”;
}

$value .= “</select>
<select name=”to_min”>
<option value=”00″>:00</option>
<option value=”15″>:15</option>
<option value=”30″>:30</option>
<option value=”45″>:45</option>
</select>
<select name=”to_merid”>
<option value=”AM”>am</option>
<option value=”PM”>pm</option>

</select>
</td>
</tr>
</table>
</form>[/code]

Thank you for any help with this.

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@phpnoviceApr 20.2006 — This may be months too late, but as long as you're not asking for everything to be done for you, then I'll give you at least this much. So... Given that you've extracted the values and placed them in the variables shown in [COLOR=Red]red[/COLOR], then you can use the following code to obtain the difference ([COLOR=Red]thru24Hour[/COLOR] may even be greater than 23 -- if the thru time is past midnight):
<i>
</i>Date.prototype.elapsedTime = function(startDate) {
if(startDate.getTime) {
var k, e = this.getTime() - startDate.getTime();
var h = Math.floor(e/(k=1000*60*60)).toString();
var m = Math.floor((e%k)/(k=k/60)).toString();
var s = Math.floor((e%k)/(k=k/60)).toString();
var x = Math.floor((e%k)/(k=k/1000)).toString();
return ("00".substr(h.length))+h+
":"+("00".substr(m.length))+m+
":"+("00".substr(s.length))+s+
"."+("0000".substr(x.length))+x;
} else {
return undefined;
}
}
var fromDate = new Date(2000, 1, 1, [COLOR=Red]from24Hour[/COLOR], [COLOR=Red]fromMinute[/COLOR], 0, 0);
var thruDate = new Date(2000, 1, 1, [COLOR=Red]thru24Hour[/COLOR], [COLOR=Red]thruMinute[/COLOR], 0, 0);
alert(thruDate.elapsedTime(fromDate));
×

Success!

Help @tripwater spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...