Jump to content

Is It Possible To Calculate How Many Hours Were Spent?


Recommended Posts

Hi everyone!


 


I want to add possibility to enter "start time", "end time", the start and the end of a break and to get the how many hours were spent.


It would be great to use separate fields for hours and minutes, and spinner controls for every field.


 


Is it possible?


 


I do not want to use Date/Time fields, just Integers.


 


 


Any ideas are very much appreciated!


 


Thank you for your time!


Link to comment
Share on other sites

Hi Cameron,

 

I am not sure that I understand your question correctly, but maybe the following code can help:

(you can add a Header&Footer element, select the Footer, click the Source button and enter the code)

<SCRIPT LANGUAGE="JavaScript">
function set_name(fieldname)
{
fieldname = "InsertRecord" + fieldname;
return fieldname;
}

function set_spinner(fieldname, maxValue)
{
document.getElementById(fieldname).setAttribute("type", "NUMBER");
document.getElementById(fieldname).setAttribute("min", "0");
document.getElementById(fieldname).setAttribute("max", maxValue);
}

/* Names of fields */
var start_hours = "start_hours";
var start_minutes = "start_minutes";
var end_hours = "end_hours";
var end_minutes = "end_minutes";
var break_hours = "break_time_hours";
var break_minutes = "break_time_minutes";

/* set spinner controls */
set_spinner( set_name(start_hours), 23);
set_spinner(set_name(start_minutes), 59);
set_spinner(set_name(end_hours), 23);
set_spinner(set_name(end_minutes), 59);
set_spinner(set_name(break_hours), 23);
set_spinner(set_name(break_minutes), 59);


function calculate_hours()
{
/* Names of fields */
var start_hours = "start_hours";
var start_minutes = "start_minutes";
var end_hours = "end_hours";
var end_minutes = "end_minutes";
var break_hours = "break_time_hours";
var break_minutes = "break_time_minutes";

var result = "hours";

/* get values */ 
var int_start_hours = parseFloat(document.getElementById(set_name(start_hours)).value);
var int_start_minutes = parseFloat(document.getElementById(set_name(start_minutes)).value);
var int_end_hours = parseFloat(document.getElementById(set_name(end_hours)).value);
var int_end_minutes = parseFloat(document.getElementById(set_name(end_minutes)).value);
var int_break_hours = parseFloat(document.getElementById(set_name(break_hours)).value);
var int_break_minutes = parseFloat(document.getElementById(set_name(break_minutes)).value);

/* calculate */
var total_hours = int_end_hours - int_start_hours - int_break_hours;
var total_minutes = int_end_minutes - int_start_minutes - int_break_minutes + total_hours*60;
total_hours = total_minutes/60;

document.getElementById(set_name(result)).value = total_hours;
}

document.getElementById("caspioform").onsubmit=calculate_hours;
</SCRIPT>

Please insert names of your fields instead of main in both blocks "Names of fields".

Also I would recommend you to make all fields required, if any field will be empty, the code does not work.

 

I hope, it helps.

Link to comment
Share on other sites

Hi Cameron !

 

In my app I have a DataPage which shows how many hours Employee worked. I have created 2 LookUp tables named " Hours" (1-24) and " Minutes" (5,10,15,20,etc) , and one main table with following fields: 

-Come_hour

-Come_minutes

-Left_hour

-Left_minutes

-Worked_minutes 

 

Here are steps which might help you:

 

1) Create a new Submission Form.

2) Select following fields from the main Table:

 

  • Come_hour ( Dropdown Form Element)
  • Come_minutes( Dropdown Form Element)
  • Left_hour( Dropdown Form Element)
  • Left_minutes( Dropdown Form Element)

 

These 4 fields take values from the "Hours" and "Minutes" Lookup Tables

 

  • Worked_minutes ( Hidden Form Element) 

 

This field  stores worked time

 

3) Add the " Header&Footer" Element.

 

4) Insert the following script  into the Footer : 

<script>
function f_collectTime(){
	try{
		var v_arH = Number(document.getElementById('InsertRecordCome_hour').value);
		var v_arM = Number(document.getElementById('InsertRecordCome_minute').value);
		var v_deH = Number(document.getElementById('InsertRecordLeft_hour').value);
		var v_deM = Number(document.getElementById('InsertRecordLeft_minute').value);
		
		document.getElementById('InsertRecordWorkedMinutes').value = (v_deH - v_arH) * 60 + (v_deM - v_arM);
		
	}catch(v_ex){}	
}

try{
	var v_btn = document.getElementById('Submit');
	v_btn.onclick = f_collectTime;
}catch(v_ex){}

</script>

5) Go to the Destination&Emails step of the DataPage 

6) Select " Display the message" form the Destination after record submit list.

7) Insert the following script in the message field:

Your work time is
<Script>
var v_timeSpan =  Number('[@field:WorkedMinutes]');
var v_hours = Math.floor(v_timeSpan / 60);
var v_minutes = v_timeSpan - v_hours * 60;
document.write( String(v_hours) + ' hours ' + String(v_minutes) + ' minutes');
</script>

I hope this can help you .

 

Regards, 

 

Aurora :)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...