Formatting dates on template that go multiple months

On our website we are listing a number of events that are formatted as follows:

In-depth Tax Course Part 1 & 2
May 27 - June 1, 2012
Toronto, ON

Since we use this info in multiple locations throughout the site, we’re using the event content type and templating this format.

The issue I"ve run into is where the start and end dates go through multiple dates. What my template is currently doing is the following

In-depth Tax Course Part 1 & 2
May 27 - 01, 2012
Toronto, ON

How can I get it to display the month?

Here’s my code:


#set($retrieve_event_start_date = $sys.item.getProperty("event_start").Date)
#set($retrieve_event_end_date = $sys.item.getProperty("event_end").Date)

#if($retrieve_event_start_date)
	#if($retrieve_event_end_date)
		#if ($lang == 'fr-ca') 
			#set($displayEventStartDate = $tools.date.format("d-", $retrieve_event_start_date, $user.psoStringTools.getLocale("fr-ca")))
		#else		
			#set($displayEventStartDate = $tools.date.format("MMMM dd - ",$sys.item.getProperty("event_start").Date))
		#end
	#else
		#if ($lang == 'fr-ca') 
			#set($displayEventStartDate = $tools.date.format("d MMMM yyyy", $retrieve_event_start_date, $user.psoStringTools.getLocale("fr-ca")))
		#else		
			#set($displayEventStartDate = $tools.date.format("MMMM dd, yyyy ",$sys.item.getProperty("event_start").Date))
		#end
	#end
#end

#if($retrieve_event_end_date)
	#if ($lang == 'fr-ca')
		#set($displayEventEndDate = $tools.date.format("d MMMM yyyy", $retrieve_event_end_date, $user.psoStringTools.getLocale("fr-ca")))
	#else
		#set($displayEventEndDate = $tools.date.format("dd, yyyy",$sys.item.getProperty("event_end").Date))
	#end
#end


#if($eventLink)
	<a target="_blank" href="${eventLink}">#displayfield("displaytitle")</a><br />
#else
	#field("displaytitle")<br />
#end
#if($retrieve_event_end_date)
	<strong>${displayEventStartDate}${displayEventEndDate}</strong><br />
#elseif ($retrieve_event_start_date)
	<strong>${displayEventStartDate}</strong><br />
#end
#field_if_set("" "description" "<br />")
#field_if_set("" "event_location" "")

In #if($retrieve_event_end_date) > #if ($lang == ‘fr-ca’) the else cause, you have

… $tools.date.format(“dd, yyyy”…

… You probably want $tools.date.format(“MMMM dd, yyyy” …

Thanks for the reply, I should have added. We have some events that are only a couple of days, in which case they need to display as I have formatted:

Example: May 08-09, 2012

That is why I have the tools.date.format only displaying partial of the date.

Maybe I’m over thinking this but I’m thinking that i have to do some kind of count in the month and determine if we’re still in the same month, but that is what I don’t know how to do.

So a hack solution would be set a variable for start and end month and years and then compare the two of them to figure out if you need to display the month or not… ie.



#set($startComp = $tools.date.format("MMyyyy ",$sys.item.getProperty("event_start").Date))
#set($endComp = $tools.date.format("MMyyyy ",$sys.item.getProperty("event_end").Date))

...
/* then where you display the end date */
#if($startComp.equals($endComp))
// format the date without the month (because you know that the month and year are the same for start and end dates)
#else
// format the date with the month
#end
...


You don’t have to squash together the month and year as I did in the example above, just make sure that the format between the start and end dates are equivalent. (You want to include the year just in case… and you should probably check for that in your code too…aka how does your code account for a year long event?)

Perfect, that worked great, thank you Jitendra. And at the moment we don’t have any events that go over multiple years, but I will keep that in mind just in case. Thanks again.

Shane