An Advanced custom field can be used to calculate the number of days between two dates. This example code reads two dates from fields called ‘date1’ and ‘date2’, and assumes that the source dates is in the format DD-MM-YYYY.
1 2 3 4 5 6 7 8 9 | SECONDS_IN_A_DAY = 86400 local d, m, y = string.match(FIELD.get('date1'):content(), "(%d+)-(%d+)-(%d+)") local d1 = os.time({year = y, month = m, day = d}) d, m, y = string.match(FIELD.get('date2'):content(), "(%d+)-(%d+)-(%d+)") local d2 = os.time({year = y, month = m, day = d}) return math.ceil(os.difftime(d1, d2) / SECONDS_IN_A_DAY) |
To create an Advanced custom field:
- right click in the EasyCatalog data panel and use the “New Custom Field” option on the pop-up menu that appears.
- At the top of the dialog, give your new field a name.
- Change the ‘Type’ popup to ‘Advanced’.
- In the “Dependencies” field enter the names of the other fields this custom field depends on — in this case “date1,date2”. This will ensure that if either date1 or date2 are updated this custom field will be recalculated.