nan:nan in Background of Field in PDF form - javascript

I have a field which calculates the total hours and minutes for a freelance during the day based on how much they have worked. My issue with this is that before any times are inserted there is the following text always visible in the field (nan:nan) I have below my code I am using, can someone please let me know how to make this not visible, thanks.
function Min2HHMM(nMinutes) {
// convert minutes to HH:MM string and returns formatted time string
return util.printf("%,001.0f:%,202.0f", Math.floor(nMinutes / 60), nMinutes % 60);
}
var T1 = this.getField("Start_Mon_01").value;
var T2 = this.getField("Finish_Mon_01").value;
var T3 = this.getField("Start_Mon_02").value;
var T4 = this.getField("Finish_Mon_02").value;
var start = T1.split(":");
var startmins1 = parseInt(start[0] * 60,10) + parseInt(start[1],10);
var finish = T2.split(":");
var finishmins1 = parseInt(finish[0] * 60,10) + parseInt(finish[1],10);
var start = T3.split(":");
var startmins2 = parseInt(start[0] * 60,10) + parseInt(start[1],10);
var finish = T4.split(":");
var finishmins2 = parseInt(finish[0] * 60,10) + parseInt(finish[1],10);
var timeDiff = finishmins1 - startmins1 + finishmins2 - startmins2;
event.value = Min2HHMM(timeDiff);

Several ways:
function getNum(str) {
return isNaN(str) || str=="" ? 0:parseInt(str,10);
}
var T1 = getNum(this.getField("Start_Mon_01").value);
var T2 = getNum(this.getField("Finish_Mon_01").value);
var T3 = getNum(this.getField("Start_Mon_02").value);
var T4 = getNum(this.getField("Finish_Mon_02").value);
OR
return isNaN(nMinutes) ? "0:00": util.printf("%,001.0f:%,202.0f", Math.floor(nMinutes / 60), nMinutes % 60);
Or
event.value = isNaN(timeDiff)?"0:00": Min2HHMM(timeDiff);
should work.
Also no need to parsint something you multiply.
parseInt(start[0] * 60,10) is the same as start[0] * 60

Related

JavaScript to auto-calculate man-hours in 24-time for Adobe Form Field?

I run a landscape crew and instead of filling out our forms manually I would like to do it via cellphone and have the times auto-calculated in the form fields. I'm not familiar with JavaScript and need some assistance in getting the correct code in order to calculate the crew times and total site man-hours without this error when I change the times. Note: I will use 24-hour time.
I tried a few different JavaScript snippets I discovered and though they work I am getting a format error when manipulating the time input. Any suggestions on how to script this into Adobe?
To generate the employee times I use the code below:
UPDATE EDIT THIS IS GENERATING INCORRECT TIMES:
// start
var start = this.getField("Monday Site #1 Start Time").value;
var startArr = start.split(":");
// finish
var finish = this.getField("Monday Site #1 Depart Time").value;
var finishArr = finish.split(":");
// difference
var hourDiff = Math.abs(finishArr[0] - startArr[0]);
var minDiff = Math.floor((Math.abs(finishArr[1] - startArr[1]) / 60)*100);
if (minDiff.toString().length == 1)
minDiff = '0' + minDiff;
var output = hourDiff + "." + minDiff;
event.value = output;
if ((event.value == "") || (event.value == Infinity) || isNaN(event.value)) {
event.value = "";}
To calculate the total site time (total manhours for the specific site) I used this:
var t1 = this.getField("WS1 Total").value;
var t2 = this.getField("WS1 Total").value;
var t3 = this.getField("WS1 Total").value;
event.value = t1+t2+t3
You can calculate the elapsed time between two Javascript Date objects in milliseconds like this:
function timeDiff(startTime, endTime) {
var startArr = startTime.split(":");
var endArr = endTime.split(":");
var startDate = new Date(0, 0, 0, startArr[0], startArr[1], 0);
var endDate = new Date(0, 0, 0, endArr[0], endArr[1], 0);
var diff = endDate.getTime() - startDate.getTime();
var hours = Math.floor(diff / 1000 / 60 / 60);
diff -= hours * 1000 * 60 * 60;
var minutes = Math.floor(diff / 1000 / 60);
return hours + ":" + (minutes < 9 ? "0" : "") + minutes;
}
console.log(timeDiff('6:24', '8:13')) // 1:49
Or if you want to return hours as decimal.
function timeDiff(startTime, endTime) {
var startArr = startTime.split(":");
var endArr = endTime.split(":");
var startDate = new Date(0, 0, 0, startArr[0], startArr[1], 0);
var endDate = new Date(0, 0, 0, endArr[0], endArr[1], 0);
var diff = endDate.getTime() - startDate.getTime();
var hours = diff / 1000 / 60 / 60;
return hours.toFixed(2)
}
console.log(timeDiff('6:24', '8:13')) // 1.82
I'm guessing that you can then do something like this:
var finish = this.getField("Monday Site #1 Depart Time").value;
var start = this.getField("Monday Site #1 Start Time").value;
this.getField("MS1T").value = timeDiff(start, finish);
This worked, with a tweak for NaN:
var start = this.getField("Monday Site #1 Depart Time").value;
var finish = this.getField("Monday Site #2 Depart Time").value;
this.getField("MS1 Total").value = timeDiff(start, finish);
if ((event.value == "") || (event.value == Infinity) || isNaN(event.value)) {event.value = "";}

Add togther HH:MM form multiple field to give a total of hours and minutes

can someone please assist, i would like to add together multiple fields containing time in HH:MM format. E.g. I have 4 fields first field contains 08:30, second field contains 12:30, the third field contains 13:30 and the fourth field contains 17:30. i would like the total time to add upto 8 hours (8:00).
currently have this code ...
function Min2HHMM(nMinutes) {
// convert minutes to HH:MM string and returns formatted time string
return util.printf("%,001.0f:%,202.0f", Math.floor(nMinutes / 60), nMinutes % 60);
}
var T1 = this.getField("Start_Mon_01").value;
var T2 = this.getField("Finish_Mon_01").value;
var T3 = this.getField("Start_Mon_02").value;
var T4 = this.getField("Finish_Mon_02").value;
var start = T1.split(":");
var startmins1 = parseInt(start[0] * 60) + parseInt(start[1]);
var finish = T2.split(":");
var finishmins1 = parseInt(finish[0] * 60) + parseInt(finish[1]);
var start = T3.split(":");
var startmins2 = parseInt(start[0] * 60) + parseInt(start[1]);
var finish = T4.split(":");
var finishmins2 = parseInt(finish[0] * 60) + parseInt(T4[1]);
var timeDiff = finishmins1 - startmins1 + finishmins2 - startmins2;
event.value = isNaN(timeDiff)?" ": Min2HHMM(timeDiff);
Help would be appreciated, thank you.

Simple calculator in JS

I create a simple web app in pure JS and generally I have few issues which I don't know how to solve. First of all, you can see my demo tool here: http://codepen.io/testerius/pen/EaOPEY
function calculator() {
/*
SPEARMAN
this code below is for Spearman unit
*/
// resource requirements
var spearmanWood = 50;
var spearmanClay = 30;
var spearmanIron = 20;
var spearman = document.getElementById("spearman").value;
// calculate
var spearmanAmount = spearman;
var spearmanWood = spearmanWood * parseInt(spearman);
var spearmanClay = spearmanClay * parseInt(spearman);
var spearmanIron = spearmanIron * parseInt(spearman);
var spearmanProvisions = spearman;
var spearmanTime = spearman * 136; // seconds
// calculate time
var totalSec = spearmanTime;
var hours = Math.floor(totalSec / 3600);
var minutes = Math.floor((totalSec - (hours * 3600)) / 60);
var seconds = totalSec - (hours * 3600) - (minutes * 60);
var spearmanTime = (hours<10 ? "0" + hours : hours) + ":" + (minutes<10 ? "0" + minutes : minutes) + ":" + (seconds<10 ? "0" + seconds : seconds);
// print to table
document.getElementById("spearmanAmount").innerHTML = spearmanAmount;
document.getElementById("spearmanWood").innerHTML = spearmanWood;
document.getElementById("spearmanClay").innerHTML = spearmanClay;
document.getElementById("spearmanIron").innerHTML = spearmanIron;
document.getElementById("spearmanProvisions").innerHTML = spearmanProvisions;
document.getElementById("spearmanTime").innerHTML = spearmanTime;
/*
SWORDSMAN
this code below is for Swordsman unit
*/
// resource requirements
var swordsmanWood = 30;
var swordsmanClay = 30;
var swordsmanIron = 70;
var swordsman = document.getElementById("swordsman").value;
// calculate
var swordsmanAmount = swordsman;
var swordsmanWood = swordsmanWood * parseInt(swordsman);
var swordsmanClay = swordsmanClay * parseInt(swordsman);
var swordsmanIron = swordsmanIron * parseInt(swordsman);
var swordsmanProvisions = swordsman;
var swordsmanTime = swordsman * 194; // seconds
// calculate time
var totalSec = swordsmanTime;
var hours = Math.floor(totalSec / 3600);
var minutes = Math.floor((totalSec - (hours * 3600)) / 60);
var seconds = totalSec - (hours * 3600) - (minutes * 60);
var swordsmanTime = (hours<10 ? "0" + hours : hours) + ":" + (minutes<10 ? "0" + minutes : minutes) + ":" + (seconds<10 ? "0" + seconds : seconds);
// print to table
document.getElementById("swordsmanAmount").innerHTML = swordsmanAmount;
document.getElementById("swordsmanWood").innerHTML = swordsmanWood;
document.getElementById("swordsmanClay").innerHTML = swordsmanClay;
document.getElementById("swordsmanIron").innerHTML = swordsmanIron;
document.getElementById("swordsmanProvisions").innerHTML = swordsmanProvisions;
document.getElementById("swordsmanTime").innerHTML = swordsmanTime;
/*
SUM OF ALL UNITS
this code below is for calculate all units
*/
// all
var allAmount = parseInt(spearmanAmount) + parseInt(swordsmanAmount);
var allWood = parseInt(spearmanWood) + parseInt(swordsmanWood);
var allClay = parseInt(spearmanClay) + parseInt(swordsmanClay);
var allIron = parseInt(spearmanIron) + parseInt(swordsmanIron);
var allProvisions = parseInt(spearmanProvisions) + parseInt(swordsmanProvisions);
var allTime = spearmanTime + swordsmanTime;
// all print
document.getElementById("allAmount").innerHTML = allAmount;
document.getElementById("allWood").innerHTML = allWood;
document.getElementById("allClay").innerHTML = allClay;
document.getElementById("allIron").innerHTML = allIron;
document.getElementById("allProvisions").innerHTML = allProvisions;
document.getElementById("allTime").innerHTML = allTime;
}
How it should work: user type how many units he would create, then JS code makes for him all calculation requirements.
As you can see it works but there are few bugs which I'd like to fix but my lack of knowledge doesn't help me. :P
Problem #1 - how can I hide the requirements table and show it only when user click button Calculate? CSS?
Problem #2 - button Reset clears inputs but doesn't clear results from requirements table, how can I make it work?
Problem #3 - times aren't added as I want, well it's probably string bug but I don't have any idea how to solve it.
Problem #4 - as you can see I repeat some of code, for spearman and swordsman code is very similar. Do you have any idea how I can make it less repeat?
Ok, so that's all what I wanted to ask you guys. Hope someone could help me. Don't get wrong but I quite beginner programmer so my code can be... you know not good. :P
For problem 1, I believe this is a solution. I'm new to Javascript so pretty limited in how I can help.
Here are the lines of code I changed:
In HTML:
<div class="row" id="requirements">
In CSS:
#requirements {
display:none;
}
In JS:
document.getElementById("requirements").style.display="block";

How to calculate the different between two datepickers

im trying to calculate the date different for a two datepicker in JavaScript. I tried following other posts, but everytime i calculate the date diff * price, but i'm not getting a result. Can someone please direct me into the right direction in order to accomplish my goal in finding date differential between two datepickers.
//input/saving data for the next form
$(".next").click(function(){
start = $('#checkin').val();
ends = $('#checkout').val();
diff = (ends - start);
days = diff/1000/60/60/24;
//roomPrice = price inserted into selection type(Single = 65, Double = 100, Suite = 120)
roomPrice = Rprices[selectType.value];
//preparing form5
fields = [$('#FirstName').val() + ' ' + $('#LastName').val(),
$('#phone').val(),
$('#email').val(),
$('#StreetAddress').val(),
$('#City').val(),
$('#state').val(),
$('#zip').val(),
$('#checkin').val(),
$('#checkout').val(),
$('#RoomType').val(),
$('#NumOfPeople').val(),
document.getElementById("total").value = days * roomPrice];
I'm not sure what you are asking but:
start = $('#checkin').val(); // assuming this is a date
ends = $('#checkout').val(); // assuming this is a date
var start_date = new Date(start); // assuming the format is correct
var end_date = new Date(ends); // assuming the format is correct
var diff_ms = end_date.getTime() - start_date.getTime(); // assuming both dates exist
will get you the time difference in milliseconds.
getDateDiff(time1, time2) {
var t2 = new Date(time1);
var t1 = new Date(time2);
var diffMS = t1 - t2;
var ret = "";
ret = ret + parseInt(diffMS / 1000 / 60 / 60).toString() + " hours ";
diffMS = diffMS - parseInt(diffMS / 1000 / 60 / 60) * 60 * 60 * 1000;
ret = ret + parseInt(diffMS / 1000 / 60).toString() + " min ";
diffMS = diffMS - parseInt(diffMS / 1000 / 60) * 60 * 1000;
ret = ret + parseInt(diffMS / 1000).toString() + " sec";
return ret ;
}

subtract minutes from calculated time javascript

I need to subtract minutes from the calculated time
var calculatedTime=11.30;// hours
var subtractTime=40;//minutes
var diff=calculatedTime - subtractTime;// How to do this ??
How to do this in javascript
Thanks
Try this:
function converToMinutes(s) {
var c = s.split('.');
return parseInt(c[0]) * 60 + parseInt(c[1]);
}
function parseTime(s) {
return Math.floor(parseInt(s) / 60) + "." + parseInt(s) % 60
}
//var minutes = parseTime(EndTIme) - parseTime(StartTime);
var timeToSubtract = 40;
var startTime = converToMinutes('11.30');
var converted = parseTime(startTime - timeToSubtract);
alert(converted);
Demo here

Categories