...

View Full Version : [RESOLVED]Select lists and time totals



tripwater
12-29-2005, 09:12 PM
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.



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

Thank you for any help with this.

Ancora
12-29-2005, 09:19 PM
Deleted as being useless and off topic.

tripwater
12-29-2005, 09:45 PM
yeah not quite what I was hoping for...


I was hoping I could take a time function and pass in the select choices and then subtract them

//To time minus From time
like time(12:00 pm) - time(9:00 am) = 3

is there a time function that I can pass in the time string to and subtract it from the other time and get the results?


for example:




var fromtime = document.event.from_hour.value + ":" + document.event.from_min.value + " " + document.event.from_merid.value;

var totime = document.event.to_hour.value + ":" + document.event.to_min.value + " " + document.event.to_merid.value;


var totaltime = stringtotime(totime) - stringtotime(fromtime);





I just used psuedo code above I know it is wrong but is there an easy way using JS to accomplish the above? Using maybe functions built into the JS system for time conversion?

thank you again for your help and time

Ancora
12-29-2005, 10:07 PM
I was hoping I could take a time function and pass in the select choices and then subtract them

//To time minus From time
like time(12:00 pm) - time(9:00 am) = 3

My code does that, along with subtracting "lunch time."

I suppose you aren't willing to make any effort to modify it to suit you.

No problem. You want what you want.

I have nothing further.

tripwater
12-29-2005, 10:14 PM
wow appreciate the sarcasm there guy. I did not realize it. I am pretty new to javascript and I did not understand what all the code was doing when I looked at it so I will have a very hard time applying it to what I need. Also adding in lunch time only further confused things for me...

why are you even on here with an attitude like that?

But thanks anyway for your time...

Ancora
12-29-2005, 10:43 PM
I did not realize it. I am pretty new to javascript and I did not understand what all the code was doing when I looked at it

I'm not here to provide custom code to someone who "tests" code by looking at it.

You LOOKED at code and whined, that it
wasn't what [you] hoped for.

Why are YOU here?

Are you too lazy or conceited to test code by someone who expended their valuable time to post it JUST FOR YOU?

And, you posted the same "question" on Web Developer, and goodnesss what other forums.

Why are YOU here?

To play others off each other?

You have a juvenile sense of entitlement, and you'll have to find someone else to try and manipulate.


Yes I did post on another forum because I have posted questions in the past on a forum and it took quite some time to get a response...

Aw, gee. Others aren't VOLUNTEERING fast enough for you. How very sad for you.

tripwater
12-29-2005, 10:52 PM
You sir are a pretentious ***. I am done with you. You are making assumptions based on what ever the hell you are doing at the time...I did not whine, I made an observation. I never asked you to provide custom code...in fact, I believe I tried my best to demonstrate what I wanted and asked if there were simple functions out there to do this...Yes I did post on another forum because I have posted questions in the past on a forum and it took quite some time to get a response...I do not think that is wrong or "playing others off each other" I don't even get this stupid *** comment.

I am not manipulating anyone. I do not have the time nor the patience to be bothered by the likes of you. Do us both a favor and avoid my posts permanently.

tripwater
12-29-2005, 10:54 PM
Does anyone else have the patience to help me please?

Thank you a head of time.

Nischumacher
12-30-2005, 12:58 PM
function getDiff(strFrom, strTo)
{
var int_to_Hour = Number(strTo.split(':')[0]);
var int_to_Minute = Number(strTo.split(':')[1].split(' ')[0]);
var str_to_Meridian = strTo.split(' ')[1];
if(str_to_Meridian=='PM' && int_to_Hour!=12) int_to_Hour+=12;
if(str_to_Meridian=='AM' && int_to_Hour==12) int_to_Hour-=12;

var int_from_Hour = Number(strFrom.split(':')[0]);
var int_from_Minute = Number(strFrom.split(':')[1].split(' ')[0]);
var str_from_Meridian = strFrom.split(' ')[1];
if(str_from_Meridian=='PM' && int_from_Hour!=12) int_from_Hour+=12;
if(str_from_Meridian=='AM' && int_from_Hour==12) int_from_Hour-=12;

var tempDiff = (int_to_Hour*60 + int_to_Minute) - (int_from_Hour*60 +

int_from_Minute);

var Diff = parseInt(tempDiff/60) + ' Hours';
Diff += (tempDiff%60==0) ? '' : (' & ' + tempDiff%60 + ' Minutes');

return Diff;
}

alert(getDiff('12:34 AM', '6:54 AM'));
alert(getDiff('9:00 AM', '12:00 PM'));

tripwater
12-30-2005, 03:58 PM
Thank you very much Nischumacher! This is exactly what I was looking for. Simple, easy to figure out and apply.

Thank you for your time.

tripwater
12-30-2005, 04:10 PM
one last question if I may...

I assume I place this function in one of the select's onchange event. But if I have a separate drop for the hours, minutes and meridiem for the from and to...how do I grab the strFrom, strTo to pass into the function if they are comprised of multiple selects?

I will do my best to figure this out on my own, but I am not very versed in JS and as I tried to impliment it into my code, I realized I had no idea where to call this function so it knew that any of the 6 select lists had changed...just my ignorance here, I'm afraid.

thank you again for your time.

tripwater
12-30-2005, 04:24 PM
I thought about using another function to get the values...





<js>
function get_values()
{
var from = document.newevent.from_hour.value + ':' + document.newevent.from_min.value + ' ' + document.newevent.from_merid.value;
var to = document.newevent.to_hour.value + ':' + document.newevent.to_min.value + ' ' + document.newevent.to_merid.value;

//then pass it into your function
getDiff(from, to);


}
</js>



I tried my code and of course it does not work...can anyone look at how I am trying to integrate my 6 droplists into Nischumacher's function and tell me if what I am doing is retarded or if I just have a syntax issue? THe error I am getting when I run this is Error : 'from' is undefined

thank you again for any help with this.

Nischumacher
12-30-2005, 05:01 PM
no need to club the values together... and then break them up...
i should have thought of this before... that there would be 6 drop downs...

function getDiff(from_Hour, from_Minute, from_Meridiem, to_Hour, to_Minute, to_Meridiem)
{
var int_from_Hour = Number(from_Hour.value);
var int_from_Minute = Number(from_Minute.value);
var str_from_Meridian = from_Meridiem.value;
if(str_from_Meridian=='PM' && int_from_Hour!=12) int_from_Hour+=12;
if(str_from_Meridian=='AM' && int_from_Hour==12) int_from_Hour-=12;

var int_to_Hour = Number(to_Hour.value);
var int_to_Minute = Number(to_Minute.value);
var str_to_Meridian = to_Meridiem.value;
if(str_to_Meridian=='PM' && int_to_Hour!=12) int_to_Hour+=12;
if(str_to_Meridian=='AM' && int_to_Hour==12) int_to_Hour-=12;

var tempDiff = (int_to_Hour*60 + int_to_Minute) - (int_from_Hour*60 +

int_from_Minute);

var Diff = parseInt(tempDiff/60) + ' Hours';
Diff += (tempDiff%60==0) ? '' : (' & ' + tempDiff%60 + ' Minutes');

alert(Diff);

return Diff;
}
call this function on click of a button...

<input type="button" name="btnDiffCalculate" value="Calulate The Difference" onclick="getDiff(fromHour, fromMinute, fromMeridiem, toHour, toMinute, toMeridiem)">

tripwater
12-30-2005, 06:28 PM
Thank you. That did it for me...I learned a good bit from your script. THank you for your time.

tripwater
01-10-2006, 05:58 PM
I found one problem...if I select a time of say 9:00 pm to 12:00 am I get a calculation of -21.00 instead of 3.00

Any ideas?

FYI, for any readers that see this, yes I opened a new thread pointing to this one because I updated this thread as [RESOLVED] before I found this problem and now I can not modify the subject line again. I figured no one would bother reading this thread with [RESOLVED] in the subject line even if it is at the top of the list. So this is why I opened a new thread pointing to this one. Sorry. Please respond to this thread though as this is where all the conversations are on this matter.

Thank you again for your time.

fishluvr
01-10-2006, 06:41 PM
After the declaration of the tempDiff variable, try:

if (tempDiff < 0) tempDiff += 24;

tripwater
01-10-2006, 06:50 PM
Thanks for the reply, fishluvr.

I added the code but now the calculation between 9:00 pm and 12:00 am comes out as -20 hours and -36 mins

fishluvr
01-10-2006, 07:41 PM
Oops... should've been:

+= 1440

I didn't pay attention to the fact that the hours were converted to minutes :o

tripwater
01-10-2006, 07:54 PM
Thanks for that...that did it!!!!

glenngv
01-11-2006, 04:01 AM
Shorter and IMHO, the better solution.

function computeDiff(fHr, fMin, fAMPM, tHr, tMin, tAMPM){
var fDate = new Date("January 1, 2006 " + fHr + ":" + fMin + " " + fAMPM);
var tDate = (tHr==12 && tAMPM=="AM" && fAMPM=="PM") ? new Date("January 2, 2006 " + tHr + ":" + tMin + " " + tAMPM) : new Date("January 1, 2006 " + tHr + ":" + tMin + " " + tAMPM);
var diffMsec = Date.parse(tDate) - Date.parse(fDate);
var diffHour = Math.floor((diffMsec%(60*60*1000*24))/(60*60*1000)*1);
var diffMin = Math.floor(((diffMsec%(60*60*1000*24))%(60*60*1000))/(60*1000)*1);
var time = new Object();
time.hour = diffHour;
time.minute = diffMin;
return time;
}

//sample usage
var diff = computeDiff(9, 15, "PM", 12, 0, "AM");
if (diff.hour<0){
alert("TO date cannot be earlier than FROM date.");
}
else {
alert("The difference is " + diff.hour + " hour(s)" + ((diff.minute>0) ? " and " + diff.minute + " min(s)." : "."));
}
The computeDiff function only contains the computation itself and the validation or final outcome is separate. This makes it more flexible and modular and easy to manage.

Beagle
01-13-2006, 09:16 PM
Well, it's a question of HOW should your script behave.

Do you want it to ALWAYS return a positive time difference? If so, you can check to see if your result is negative, and if so, add 24 to it:

if (result < 0 ) { result += 24; }

That would make your -21 into 3 in your example.

Now, if you need it to be intelligent and give you negative sometimes and positive other times, you just need to figure out the logic for when each is supposed to happen and code it :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum