Script to Calculate the Difference Between Dates in Days
Description
Often there is a requirement to calculate the difference between 2 different dates on a label, such as an entered date versus the current date. This can be done with a script.
How to Create a Script to Calculate the Difference between Dates in Days
The following script will compare a user entered date to the current date, and calculate the difference between the 2, putting the resulting number of days onto the label for printing. The script may be modified to compare any 2 date fields.
Use the link below to see the sample label.
Link = Script to Compare Current Date With Entered Date.lwl
// The following defines a variable to take any date in any format from a variable field Base_Date in this case), and turn it into a proper date string for conversion
var dateString = String(label.fields.field.(@name == 'Base_Date').@data);
var bd = new Date(dateString);
// The following defines a variable that takes the new date variable (bd) and converts it into milliseconds
var bd1 = Date.parse(bd);
// The following defines a variable to take any date in any format from a variable field Entered_Date in this case), and turn it into a proper date string for conversion
var dateString = String(label.fields.field.(@name == 'Entered_Date').@data);
var ed = new Date(dateString);
// The following defines a variable that takes the new date variable (ed) and converts it into milliseconds
var ed1 = Date.parse(ed);
//The following defines a variable that calculates the difference between the current date and the entered date
var ddms = ed-bd
//The following defines a variable that calculates the date difference in days by taking the date difference in milliseconds, dividing by 86400000 (the amount of milliseconds per day), then rounding the value
var dd = Math.round(ddms/86400000)
//The following formulates the printed Date Difference
label.fields.field.(@name == 'Date_Difference').@data = dd;
Article Number
2016019
Versions
Loftware Label A label is a design area on the computer screen where a label format is created or edited. Manager / Loftware Print Server versions 9.5.2.0 - 11.1
Environment
All supported environments, 9.5 and higher.