Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 21
  1. #1
    Regular Coder
    Join Date
    May 2005
    Posts
    262
    Thanks
    4
    Thanked 0 Times in 0 Posts

    [RESOLVED]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>
    Thank you for any help with this.
    Last edited by tripwater; 12-30-2005 at 06:28 PM.

  • #2
    Banned
    Join Date
    Oct 2005
    Location
    I'm in GMT -5
    Posts
    314
    Thanks
    0
    Thanked 1 Time in 1 Post
    Deleted as being useless and off topic.
    Last edited by Ancora; 12-29-2005 at 10:08 PM.

  • #3
    Regular Coder
    Join Date
    May 2005
    Posts
    262
    Thanks
    4
    Thanked 0 Times in 0 Posts
    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:

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

  • #4
    Banned
    Join Date
    Oct 2005
    Location
    I'm in GMT -5
    Posts
    314
    Thanks
    0
    Thanked 1 Time in 1 Post
    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.

  • #5
    Regular Coder
    Join Date
    May 2005
    Posts
    262
    Thanks
    4
    Thanked 0 Times in 0 Posts
    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...
    Last edited by tripwater; 12-29-2005 at 10:17 PM.

  • #6
    Banned
    Join Date
    Oct 2005
    Location
    I'm in GMT -5
    Posts
    314
    Thanks
    0
    Thanked 1 Time in 1 Post
    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.
    Last edited by Ancora; 12-29-2005 at 11:22 PM.

  • #7
    Regular Coder
    Join Date
    May 2005
    Posts
    262
    Thanks
    4
    Thanked 0 Times in 0 Posts
    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.
    Last edited by tripwater; 12-29-2005 at 11:03 PM.

  • #8
    Regular Coder
    Join Date
    May 2005
    Posts
    262
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Does anyone else have the patience to help me please?

    Thank you a head of time.

  • #9
    Regular Coder Nischumacher's Avatar
    Join Date
    Oct 2005
    Location
    Bombay, India
    Posts
    196
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Code:
    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'));
    - NS 666
    .net DEVILoper

  • #10
    Regular Coder
    Join Date
    May 2005
    Posts
    262
    Thanks
    4
    Thanked 0 Times in 0 Posts
    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.

  • #11
    Regular Coder
    Join Date
    May 2005
    Posts
    262
    Thanks
    4
    Thanked 0 Times in 0 Posts
    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.

  • #12
    Regular Coder
    Join Date
    May 2005
    Posts
    262
    Thanks
    4
    Thanked 0 Times in 0 Posts
    I thought about using another function to get the values...


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

  • #13
    Regular Coder Nischumacher's Avatar
    Join Date
    Oct 2005
    Location
    Bombay, India
    Posts
    196
    Thanks
    0
    Thanked 2 Times in 2 Posts
    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...
    Code:
    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...
    Code:
    <input type="button" name="btnDiffCalculate" value="Calulate The Difference" onclick="getDiff(fromHour, fromMinute, fromMeridiem, toHour, toMinute, toMeridiem)">
    - NS 666
    .net DEVILoper

  • #14
    Regular Coder
    Join Date
    May 2005
    Posts
    262
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Thank you. That did it for me...I learned a good bit from your script. THank you for your time.

  • #15
    Regular Coder
    Join Date
    May 2005
    Posts
    262
    Thanks
    4
    Thanked 0 Times in 0 Posts
    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.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •