Get Datetime

Get Datetime

Datetime Fields

  • Stores field values as string in ISO format (yyyymmddThhmmss)
  • Use Sitecore.DateUtil.ParseDateTime() method to parse date format into raw value
  • Supported by <sc:date> web control in declarative markup
  • Supported by Sitecore.Data.Fields.DateField class in Sitecore API
  • Field values can be displayed by the FieldRenderer.Render() method and FieldRenderer control
  • Natively supports displaying field values in the Page Editor

Controls featured in this example:

  • Sitecore Web Control

Classes featured in this example:

  • Sitecore.Context
  • Sitecore.Data.Fields.DateField
  • Sitecore.Data.Items.Item
  • Sitecore.DateUtil
  • System.DateTime

Datatime C# Web Control Code

Sitecore.Data.Items.Item item = Sitecore.Context.Item;
if (item == null) return;

Sitecore.Data.Fields.DateField date = item.Fields["__updated"];
if (date == null) return;

System.DateTime datetime = Sitecore.DateUtil.IsoDateToDateTime(date.Value);
if (datetime == null) return;

output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-weight:bold; width:100%; clear:both");
output.Write("C# Web Control Real-time Output");

output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-size:x-small; width:100%; clear:both");
output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-weight:bold");
//using &#58; for colon and &#160; for space
output.Write("{0}&#58;&#160;{1}", "Item", item.DisplayName);
output.RenderEndTag(); //P

output.Write("{0}&#58;&#160;{1}", "Raw Datetime", "<b>" + date.Value + "</b><br/>");
output.Write("{0}&#58;&#160;{1}", "Long Datetime", "<b>" + Sitecore.DateUtil.FormatLongDateTime(datetime) + "</b><br/>");
output.Write("{0}&#58;&#160;{1}", "Short Datetime", "<b>" + Sitecore.DateUtil.FormatShortDateTime(datetime) + "</b><br/>");
output.Write("{0}&#58;&#160;{1}", "Military Datetime", "<b>" + Sitecore.DateUtil.DateTimeToMilitary(datetime) + "</b>");
output.RenderEndTag(); //Div

Datatime C# Web Control Output

Raw Datetime: 20140823T184540:635444163404775062
Long Datetime: Saturday, August 23, 2014 6:45 PM
Short Datetime: 8/23/2014 6:45 PM
Military Datetime: 23-AUG-2014 18:45:40


  • This code can be used to access date fields and datetime fields
  • The code uses a fields collection to reference the "__updated" field from the Statistics base template