hclleapforum

Open full view…

How to insert the Javascript - what part of the code to where?

ikettle
Thu, 08 Aug 2019 12:52:42 GMT

Hi Guys, I am trying to add the piece of javascript code to my application for working out days between dates. I checked the javascript functions and under dates, I found the code I can use. However, I have tried putting this in as is (obviously changing BO to what I have for start date, end date. But I am unsure of what piece of code goes where. Here is the code; var startDate = BO.F_Date.getValue(); var endDate = BO.F_Date0.getValue(); if(startDate !== null && startDate !== "" && endDate !== null && endDate !== "") { var days= app.getSharedData().workingDaysBetweenDates(startDate,endDate, true); BO.F_SingleLine.setValue(days); } Where does the above code get placed? In the ItemChange Event or StartApp event? // includeWeekends - Pass true if you want to include weekends otherwise false. app.getSharedData().workingDaysBetweenDates = function(startDt, endDt, includeWeekends) { // Validate input if (endDt < startDt) return 0; // Calculate days between dates var millisecondsPerDay = 86400 * 1000; // Day in milliseconds startDt.setHours(0,0,0 ,1); // Start just after midnight endDt.setHours(23,59,59,999); // End just before midnight var diff = endDt.getTime() - startDt.getTime(); // Milliseconds between datetime objects var days = Math.ceil(diff / millisecondsPerDay); if(!includeWeekends) { // Subtract two weekend days for every week in between var weeks = Math.floor(days / 7); var days = days - (weeks * 2); // Handle special cases var startDay = startDt.getDay(); var endDay = endDt.getDay(); // Remove weekend not previously removed. if (startDay - endDay > 1) days = days - 2; // Remove start day if span starts on Sunday but ends before Saturday if (startDay === 0 && endDay !== 6) days = days - 1 // Remove end day if span ends on Saturday but starts after Sunday if (endDay === 6 && startDay !== 0) days = days - 1 } return days; } then also where does the function (above go) on itemchange event? I am just unsure of where I need to add what code to where. Its not very clear to me, from the information which can be found here https://hclleapwiki.atlassian.net/wiki/spaces/HL/pages/33025/Dates#Dates-CompareDateFields Any help will greatly be appreciated.

marty_lechleider
Thu, 15 Aug 2019 15:54:34 GMT

The wiki provides the code example for use a a global function....meaning you can define the function once in the application onStart and then use it in different place in your form. app.getSharedData() is a way to define global variables and functions and then refer to them/use them anywhere in your app. This goes in the application onStart app.getSharedData().workingDaysBetweenDates = function(startDt, endDt, includeWeekends) { // Validate input if (endDt < startDt) return 0; // Calculate days between dates var millisecondsPerDay = 86400 1000; // Day in milliseconds startDt.setHours(0,0,0 ,1); // Start just after midnight endDt.setHours(23,59,59,999); // End just before midnight var diff = endDt.getTime() – startDt.getTime(); // Milliseconds between datetime objects var days = Math.ceil(diff / millisecondsPerDay); if(!includeWeekends) { // Subtract two weekend days for every week in between var weeks = Math.floor(days / 7); var days = days – (weeks 2); // Handle special cases var startDay = startDt.getDay(); var endDay = endDt.getDay(); // Remove weekend not previously removed. if (startDay – endDay > 1) days = days – 2; // Remove start day if span starts on Sunday but ends before Saturday if (startDay === 0 && endDay !== 6) days = days – 1 // Remove end day if span ends on Saturday but starts after Sunday if (endDay === 6 && startDay !== 0) days = days – 1 } return days; } and then this code would go in the event you want to trigger the code. My guess is when you enter the 2nd date var startDate = BO.FDate.getValue(); var endDate = BO.FDate0.getValue(); if(startDate !== null && startDate !== "“ && endDate !== null && endDate !== ”") { var days= app.getSharedData().workingDaysBetweenDates(startDate,endDate, true); BO.F_SingleLine.setValue(days); }