Sitecore get date from Date field

adminSitecore0 Comments

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

Your email address will not be published. Required fields are marked *