Show Date Field

Show Date Field

Date 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.Database
  • Sitecore.Data.Fields.DateField
  • Sitecore.Data.Items.Item

Show Date Field C# Web Control Code

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

//get Simple Fields metadata item from context database using item guid
Sitecore.Data.Database db = Sitecore.Context.Database;
Sitecore.Data.Items.Item simpleFields = db.GetItem("{EC44470F-FF45-4023-95C2-EC886FD33D0C}");
if (simpleFields == null) return;

Sitecore.Data.Fields.DateField dateFld = simpleFields.Fields["example date"];
if (dateFld == null) return;

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

output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-size:x-small; width:100%; clear:both");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Div);
output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-weight:bold");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.P);
//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}", "DateTime Display", "<b>" + dateFld.DateTime.ToString() + "</b><br/>");
output.Write("{0}&#58;&#160;{1}", "LocalTime Display", "<b>" + dateFld.DateTime.ToLocalTime().ToString() + "</b><br/>");
output.Write("{0}&#58;&#160;{1}", "LongDate Display", "<b>" + dateFld.DateTime.ToLongDateString() + "</b><br/>");
output.Write("{0}&#58;&#160;{1}", "LongTime Display", "<b>" + dateFld.DateTime.ToLongTimeString() + "</b><br/>");
output.Write("{0}&#58;&#160;{1}", "Office Automation Date", "<b>" + dateFld.DateTime.ToOADate().ToString() + "</b><br/>");
output.Write("{0}&#58;&#160;{1}", "ShortDate Display", "<b>" + dateFld.DateTime.ToShortDateString() + "</b><br/>");
output.Write("{0}&#58;&#160;{1}", "ShortTime Display", "<b>" + dateFld.DateTime.ToShortTimeString() + "</b><br/>");
output.Write("{0}&#58;&#160;{1}", "UniversalTime Display", "<b>" + dateFld.DateTime.ToUniversalTime().ToString() + "</b>");

output.RenderEndTag(); //Div


Show Date Field C# Web Control Output

DateTime Display: 1/1/0001 12:00:00 AM
LocalTime Display: 1/1/0001 12:00:00 AM
LongDate Display: Monday, January 01, 0001
LongTime Display: 12:00:00 AM
Office Automation Date: 0
ShortDate Display: 1/1/0001
ShortTime Display: 12:00 AM
UniversalTime Display: 1/1/0001 8:00:00 AM