I have an Event content type that has a Start Event Date field and an End Event Date field. On my template I’m trying to figure out how I can count the days between these 2 fields. The idea being that on the template we want to say “Event Length: x days”
In my environment $sys.item.getProperty(“start_date”) returns as com.percussion.utils.jsr170.PSProperty which is Property. $sys.item.getProperty(“start_date”).Date return as java.util.GregorianCalendar which inherits getTime() from the Calendar class. This getTime() returns a Date object; therefore, we need to call getTime() once more in order to obtain the Long value for calculations.
UPDATE: Provided a version with less function calls. Swapping the use of getTime() with getTimeInMillis()
For some reason I thought that getTime() would return the Unix timestamp in milliseconds, not a date object as it does with java.util.GregorianCalendar… Thanks for the explanation!
Thank you for pointing out the double call to getTime(). It was enough encouragement to learn more about those objects to find a solution with less function calls.
Thank you Riley, this really helped me a lot. I have a number of templates that are doing formatting in French for dates and they have requirements of using different formatting based on number of days in an event, so this great.
Just a word of warning with this solution, it may not give you the result you want in all cases. Remember that not every day has 24 hours. If the date range crosses over a daylight savings change it will be slightly out. If Time is included within your date also differences in the time used for the start date and end date may cause your number of days to be one day out. To get this totally correct though is a little more difficult. I have seen solutions where you just keep adding a day through the Calender object till you get to your end date, which is not the best performance wise or using a better date api http://joda-time.sourceforge.net/ which we do not currently have in the product.
What you have may work fine for you purposes though.
Thanks for the warning Stephen. We’re definitely playing around this to figure out all it’s limitations and quarks but overall I think it’ll work for our purposes.