The following IronPython code shows how to calculate the difference in months between two dates passed by a property control.
Introduction
The following IronPython code shows how to calculate the difference in months between two dates passed by a property control. In the code, startDate and endDate receive the Document Properties values that are filled by two Input Property Controls. The result is stored in a Document Property for this example. I couldn't find an example of that (or something like this), so I thought I would share this. I hope it's useful. Has an attached .dxp file for better understanding.
Code Sample
# Input Property Control (Document Properties) startDate = Document.Properties["inicialDate"] endDate = Document.Properties["finalDate"] # Converting date to string and then splitting it to capture the month and year to calculate number of months of difference #Pay attention to the date format. # This script consider that you are using dd/mm/yyyy format. #If not, just configure the range between '[]' to your case. # Getting inicial month and year and then converting to integer startMonth = int((str(startDate))[3:5]) startYear = int((str(startDate))[6:10]) # Getting final month and year and then converting to integer endMonth = int((str(endDate))[3:5]) endYear = int((str(endDate))[6:10]) # Be sure that the end date is bigger than start date. if (startDate > endDate): Document.Properties["Result"]="This range is not valid. Please, try again." else: # Apply this formula to calculate difference in months numberOfMonths = (((endYear - startYear) * 12) - startMonth + 1) + endMonth #Storing the number of months in a document property Document.Properties["Result"]=str(numberOfMonths) #print numberOfMonths
differenceinmonthsbetweentwodates.dxp
Recommended Comments
There are no comments to display.