Blog

Kentico Macros for Formatting Date Ranges

by Joel Dahlin on August 7, 2015

Kentico Macros for Formatting Date Ranges

With these macros you can display the date range in the formats:

  • Month day
  • Month day - day
  • Month day - Month day (if the months are not the same)
<%# FormatDateTime(Eval("EventDate"), "MMMM d") %>
<%# IfEmpty(Eval("EventEndDate"), "", IfCompare(FormatDateTime(Eval("EventDate"), "MMM d"), FormatDateTime(Eval("EventEndDate"), "MMM d"), " - " + IfCompare(Eval<DateTime>("EventDate").Month, Eval<DateTime>("EventEndDate").Month, FormatDateTime(Eval("EventEndDate"), "MMMM d"), FormatDateTime(Eval("EventEndDate"), " d")), "")) %>

*Tip

To find other out of the box macros like IfEmpty and IfCompare click on the "Available transformation methods" link when building your transformation.

#macro #tip

Comments
gopi ch
How can can i convert date to march,16,2016 format in text/xml tranformation. if user enters 2/16/2016 12:00:00 AM format
3/23/2016 5:20:58 AM

Adam Reece
Good stuff, Joel! Handy little snippet that I used today. :)
8/17/2015 1:06:07 PM

 Security code