- Home [object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
- Getting started
- Manage types
- Global context and isolation
- Manage apps
- Batch actions with app items
- Manage external services
- Scripts in widgets
- Web components
- Access permissions
- Getting started with processes
- Getting started with signatures
- Getting started with file previews
- Getting started with the organizational chart
- Getting started with users and groups
-
Getting started with the
Table data type - Use cases
- How to set up custom view of app items
- How to set up dynamic display of fields and widgets with a complex condition
- How to register a document
- How to calculate the number of days between two dates
- How to create a substitution for a user
- How to use pagination and sorting when searching for app items
- API
- Object types
- Data types
- Global constants
- Work with apps
- Web requests
- Access permissions
- Document flow
- Live Chats
- “Code” widget
- Signatures
- Business calendars
- Integration with IP telephony
- Integration with email marketing services
In this article
How to calculate the number of days between two dates
You can use custom scripts to calculate the number of days between two dates.
Calculating the duration of a business trip
As an example, let’s see how you can calculate the duration of a business trip in days.
Let’s add a Date type variable to the app’s context and name it Business trip end date. Let’s also create a Number type variable called Business trip duration in days.
Let’s open the creation form of the app in the interface designer and add the Business trip end date and Business trip duration in days fields to the creation form. The second field needs to be read-only. In our example, the business trip starts on the day the app item is created, so we’ll need to get the current date using a script.
To access the settings of the Business trip end date variable, click the gear icon. On the Events tab, you need to create a script that will change the necessary values. Let’s open the created function on the Scripts tab and write the script.
Script
async function caclDays(): Promise<void> { /* We need to access the variable whose change the script is linked with and set the following condition: the code will only be executed if the variable is defined */ if (Context.data.trip_end_date) { // Let’s create a new variable and write the current date to it using the following constructor: let currentDate = new TDate(); /* Now we need to write the number of days to the context variable by subtracting the current date from the specified business trip end date. Using the `asDateTime` method, we’ll present both variables in the Date/Time format (the Date type needs to be converted to Date/Time; this will grant access to the method used to calculate the number of days). Then, using the `unix` method, we’ll present both dates as the number of seconds passed since 00:00:00 UTC 01/01/1970 and subtract one from the other. Now let’s convert the result from seconds to days by dividing it by the product of 60x60x24 (60 seconds, 60 minutes, and 24 hours) */ Context.data.trip_duration_in_days = (Context.data.trip_end_date.asDatetime(new TTime(0, 0, 0, 0)).unix() - currentDate.asDatetime(new TTime(0, 0, 0, 0)).unix()) / (24 * 60 * 60); } }
Result
When the user enters the business trip end date, the number of days is successfully passed to the app item’s creation form.
Calculating the number of days needed to complete an order
Let’s consider another case. We need to calculate the number of business days (according to the system business calendar) that will need to be spent to fulfil an order. On the creation form, there are two Date type fields: Manufacturing start date and Scheduled shipping time. Let’s open the settings of the Scheduled shipping time field and create a script to change the value we need on the Event tab.
Script
/* We need to write the number of business hours between the dates to the variable using the getWorkingTime method that refers to the Business Calendar. The method will only work correctly if both arguments are presented in the Date/Time format. Let’s convert the Context.data.start_date variable from Date to Date/Time and set its time to 00:00. */ const workingTime = await System.productionSchedule.getWorkingTime(Context.data.start_date.asDatetime(new TTime(0, 0, 0, 0)), Context.data.end_date); // Now we’ll write the result in the form of business days. To do that, we’ll divide the number of hours by 8 (as a business day lasts for 8 hours) and round the value Context.data.production_days = Math.floor(workingTime.hours / 8);
Result
When the user enters the scheduled shipping date, the number of days the manufacturing will take with regard to the business calendar is shown on the form.