Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

AEM Form : Subtraction and addition of time

Avatar

Level 3

I am trying to make a form in a tabular form in which there are two sets of time in a single row 

1st set

Cell4 : InTime     

Cell5 : OutTime

2nd set 

Cell6: InTime

Cell7:OutTime  

I have the script  and it calculates the difference between intime and outime and then add both set  but  it adds only after all the four cells are filled... i want if either of the set is filled  .. it should start calculating...... if cell4& cell5 is filled then it should show the result even if cell6 and cell7 are not filled and vice versa...... 

 

Below is the Javascript 

 

var t1 = "";
var t2 = "";
var vTime1 = Cell4.rawValue;
var vTime2 = Cell5.rawValue;
vTime1 = vTime1.replace(":", "");
vTime2 = vTime2.replace(":", "");
if (vTime1.length == 4 && vTime2.length == 4) {
var vTime1Minutes = parseInt(vTime1.substring(0, 1))*600 + parseInt(vTime1.substring(1, 2))*60 + parseInt(vTime1.substring(2, 4));
var vTime2Minutes = parseInt(vTime2.substring(0, 1))*600 + parseInt(vTime2.substring(1, 2))*60 + parseInt(vTime2.substring(2, 4));
if (vTime1Minutes > vTime2Minutes){
this.rawValue = "0";}
else {
t1 = vTime2Minutes - vTime1Minutes;
}}


var vTime3 = Cell6.rawValue;
var vTime4 = Cell7.rawValue;
vTime3 = vTime3.replace(":", "");
vTime4 = vTime4.replace(":", "");
if (vTime3.length == 4 && vTime4.length == 4) {
var vTime3Minutes = parseInt(vTime3.substring(0, 1))*600 + parseInt(vTime3.substring(1, 2))*60 + parseInt(vTime3.substring(2, 4));
var vTime4Minutes = parseInt(vTime4.substring(0, 1))*600 + parseInt(vTime4.substring(1, 2))*60 + parseInt(vTime4.substring(2, 4));
if (vTime3Minutes > vTime4Minutes){
this.rawValue = "0";}
else {
t2 = vTime4Minutes - vTime3Minutes;
}}

var t3 = t1 + t2
var Hours = Math.floor(t3/60);
var Minutes = t3%60;
if (Hours == 0)
Hours = 24;
if (Hours <= 9)
Hours = "0" + Hours;
if (Minutes < 10)
Minutes = "0" + Minutes;
this.rawValue = Hours + ":" + Minutes;

 

 

Thanks

1 Accepted Solution

Avatar

Correct answer by
Employee

The 24 thing I saw but it was your script - you have fixed it it seems

For the formatting this is how you can do it:

1) Change ALL the INPUT fields to DateTime type

kprokopi_0-1595226795324.png

2) Set binding to Time

kprokopi_1-1595226880748.png

3) Set All the Patterns to time{HH:MM}

kprokopi_2-1595227356305.png

4) activate validation for input fields

kprokopi_3-1595227644171.png

5) make a change in the script object

Change .rawValue to .formattedValue

kprokopi_4-1595227726569.png

 

kprokopi_5-1595227775435.png

kprokopi_6-1595227837922.png

 

 

View solution in original post

13 Replies

Avatar

Employee

Hi

you are most likely using the calcuate event which always waits until ALL dependencies of the calculation contain a calculable value.

I took another approach and also moved your script into a script object as we call the calculation in different places and don't want code duplication

kprokopi_0-1594975934013.png

The code in there is as follows:

function calcResult () {
var t1 = calcDiff(intime1,outtime1);
var t2 = calcDiff(intime2,outtime2);
var t3 = t1 + t2

if (t3 != 0) {
var Hours = Math.floor(t3/60);
var Minutes = t3%60;
if (Hours == 0) Hours = 24;
if (Hours <= 9) Hours = "0" + Hours;
if (Minutes < 10) Minutes = "0" + Minutes;
result.rawValue = Hours + ":" + Minutes;
} else {
result.rawValue = "";
}
}

function calcDiff(intime, outtime) {
try {
var vTime1 = intime.rawValue;
var vTime2 = outtime.rawValue;
if (vTime1 != null && vTime1 != "") {
vTime1 = vTime1.replace(":", "");
} else {
vTime1 = "";
}
if (vTime2 != null && vTime2 != "") {
vTime2 = vTime2.replace(":", "");
} else {
vTime1 = "";
}
if (vTime1.length == 4 && vTime2.length == 4) {
var vTime1Minutes = parseInt(vTime1.substring(0, 1))*600 + parseInt(vTime1.substring(1, 2))*60 + parseInt(vTime1.substring(2, 4));
var vTime2Minutes = parseInt(vTime2.substring(0, 1))*600 + parseInt(vTime2.substring(1, 2))*60 + parseInt(vTime2.substring(2, 4));
if (vTime1Minutes > vTime2Minutes){
return 0;
} else {
return vTime2Minutes - vTime1Minutes;
}
} else {
return 0;
}
} catch(e) {
return 0;
}
}

 

In the fields I place the calculation function in the exit event of the Out Time fields:

kprokopi_1-1594976101902.png

kprokopi_2-1594976224550.png

kprokopi_3-1594976269394.png

kprokopi_4-1594976319604.png

This is sample code based on yours, slightly modified - his may not be perfect!

Result field is readonly and does NOT contain any code.

 

Avatar

Level 3
not working ....... i have whole table with 30 rows with each row having 2 sets of time... ..

Avatar

Level 3
do i need to change "result.rawValue" to my result field name, if that is so then how to use this variable with other rows.....

Avatar

Level 3
variable script you gave is of no use as i have 30 rows .. i had to copy and paste the whole script in each of the outtime cell....

Avatar

Employee

ok, you can move this to a table by creating n rows with identical fields (I call them intime1, outtime1, intime2, outtime2, result)

kprokopi_0-1594984515097.png

Change the script in the script object as shown below and place the following line in each outtime exit event:

calcTimes.calcResult(intime1,outtime1,intime2,outtime2,result);

 

Script object:

function calcResult (intime1, outtime1, intime2, outtime2,result) {
var t1 = calcDiff(intime1,outtime1);
var t2 = calcDiff(intime2,outtime2);
var t3 = t1 + t2

if (t3 != 0) {
var Hours = Math.floor(t3/60);
var Minutes = t3%60;
if (Hours == 0) Hours = 24;
if (Hours <= 9) Hours = "0" + Hours;
if (Minutes < 10) Minutes = "0" + Minutes;
result.rawValue = Hours + ":" + Minutes;
} else {
result.rawValue = "";
}
}

function calcDiff(intime, outtime) {
try {
var vTime1 = intime.rawValue;
var vTime2 = outtime.rawValue;
if (vTime1 != null && vTime1 != "") {
vTime1 = vTime1.replace(":", "");
} else {
vTime1 = "";
}
if (vTime2 != null && vTime2 != "") {
vTime2 = vTime2.replace(":", "");
} else {
vTime1 = "";
}
if (vTime1.length == 4 && vTime2.length == 4) {
var vTime1Minutes = parseInt(vTime1.substring(0, 1))*600 + parseInt(vTime1.substring(1, 2))*60 + parseInt(vTime1.substring(2, 4));
var vTime2Minutes = parseInt(vTime2.substring(0, 1))*600 + parseInt(vTime2.substring(1, 2))*60 + parseInt(vTime2.substring(2, 4));
if (vTime1Minutes > vTime2Minutes){
return 0;
} else {
return vTime2Minutes - vTime1Minutes;
}
} else {
return 0;
}
} catch(e) {
return 0;
}
}

 

You must adapt things to your form and I cannot upload files here.

kprokopi_1-1594984737442.png

 

Avatar

Level 3

ok.... nice... the script is working exactly as i desired... now few things more.. i want to restrict user so that they cannot enter anything expect time format HH:MM also should not be able to enter greater than 00:00 = 12:00 am (24hrs time format)  ..also want to add all results in the total cell

Avatar

Level 3
The script has one issue .... when i input time which is less than an hour then it gives 24 hours .. for example : intime : 06:25; outtime = 06:40 then result is 24:15 instead of 00:15

Avatar

Level 3
Hours less than 1 issue solved......... one last issue is that i want user to user only data in HH:MM format that too between 00:00 to 23:59 (24hrs time)...... when i set the cell to Time form the calculations don't work.... plz guide

Avatar

Correct answer by
Employee

The 24 thing I saw but it was your script - you have fixed it it seems

For the formatting this is how you can do it:

1) Change ALL the INPUT fields to DateTime type

kprokopi_0-1595226795324.png

2) Set binding to Time

kprokopi_1-1595226880748.png

3) Set All the Patterns to time{HH:MM}

kprokopi_2-1595227356305.png

4) activate validation for input fields

kprokopi_3-1595227644171.png

5) make a change in the script object

Change .rawValue to .formattedValue

kprokopi_4-1595227726569.png

 

kprokopi_5-1595227775435.png

kprokopi_6-1595227837922.png

 

 

Avatar

Level 3
ok thanks for solving the validation issue........... one more thing how to add all 30 result to the bottom of the table there are 30 cells.. so do i have to make 30 variables....

Avatar

Level 3
ok.. after doing validation it works good except for that it doesnot force user to input correct value.. instead it just pops up the error and after discarding the popup box it moves to the next field.... i want if the user inputs wrong value like 2559 , then it should popup the error and after user close the popup it should focus on the same field till the user has entered a correct value between 00:00 to 23:59