Sitefinity Getdatetime Exception Error

Solving the Sitefinity @item.GetDateTime exception error in widget templates.

This error occurs when a date is not checked for a null date or a date that doesn’t have a value in the field.  The @item.GetDateTime function cannot handle empty values and causes and exception in the template.

You need to test for the existence of the date and then do the @item.GetDateTime(“FestivalDate”, “MMM d, yyyy, HH:mm tt”)

Code:

@foreach (var item in Model.Items)
{
@Html.InlineEditingFieldAttributes(“Title”, “ShortText”)>@item.Fields.Title

@if (item.Fields.FestivalDate != null)
{
         Festival Date: @item.GetDateTime(“FestivalDate”, “MMM d, yyyy, HH:mm tt”)
}
else
{
        No festival date announced
}

}