Sitecore Field Renderer and Custom Date Formats


The Problem

On a recent project, I needed to render a date field.  However, the default date format would not do.  When I researched how to format a date field using the Sitecore HTML helper method, Field, it was not clear what the solution was.  I hope this post will help others cut thru the chase on this topic...

The Solution

The solution is actually dead simple.  Support is built into the HTML helper method!  Just add the format parameter to the Field method call.

1
@Html.Sitecore().Field(Templates.CardItem.Fields.Date, new {format="MMM dd, yyyy"})

That's it.  The format value leveraged the standard date format specifications.

Support for the date format parameter is built is built into Sitecore 8.2.  Not quite sure which version support was introduced, but if you're using Sitecore 8.2 and up, you can leverage data format as a parameter in the Field HTML helper method.

Categories: Sitecore, Web Development
Tags: Sitecore;

SEARCH ARTICLES