count number of days between 2 dates

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”

Thoughts anyone?

Thank you,

Shane

Shane,

You should be able to use the following (if your fields are sys_SimpleCalendar):


#set($firstDate = $sys.item.getProperty("start_date").Date.getTime())##
#set($secondDate = $sys.item.getProperty("end_date").Date.getTime())##
#set($days_between = ($secondDate.getTime()-$firstDate.getTime())/(1000*60*60*24))##

Event Length: $days_between Days

You should probably add conditions to verify that each date variable is populated; otherwise, you may get unexpected behavior.

Double call to getTime()? Probably don’t need it when you set the $days_between variable.

Yes, double getTime() call.

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()

#set($firstDate = $sys.item.getProperty("start_date").Date.getTimeInMillis())##
#set($secondDate = $sys.item.getProperty("end_date").Date.getTimeInMillis())##
#set($days_between = ($secondDate - $firstDate)/(1000*60*60*24))##

Event Length: $days_between Days

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.

Shane

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.

Shane

Searching the forums yielded this post…http://forum.percussion.com/showthread.php?351-Trouble-getting-date-diff&highlight=date+velocity

The latest Velocity Tools has a “Comparison Date Tool” which will do what you want.

This was in 2008, so hopefully 4 years later we have the newer Velocity Tools as standard. Have not checked though…