Sitecore get date from Date field

Quick and safe way to retrieve date/ datetime from Sitecore field

public static DateTime? GetFieldValueAsDateTime(this Item item, string fieldName)
{
    var dateStr = item.GetFieldValue(fieldName);
    
    if (dateStr.HasValue())
    {
        return Sitecore.DateUtil.IsoDateToDateTime(dateStr);
    }
    return null;
}

 

Leave a Reply