Displaying Months

Displaying Months

Controls featured in this example:

  • Sitecore Web Control

Classes featured in this example:

  • Sitecore.Context
  • Sitecore.Data.Database
  • Sitecore.Data.Items.Item
  • Sitecore.Web.UI.WebControls.FieldRenderer

Displaying Months C# Web Control Code

//get Simple Fields metadata item from context database using item guid
Sitecore.Data.Database db = Sitecore.Context.Database;
Sitecore.Data.Items.Item context = Sitecore.Context.Item; 
Sitecore.Data.Items.Item item = db.GetItem("{EC44470F-FF45-4023-95C2-EC886FD33D0C}");
if (item == null || item.Fields["example date"] == 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 : for colon and   for space
output.Write("{0}: {1}", "Item", context.DisplayName);
output.RenderEndTag(); //P

string rawDate = item["example date"];  
string M = Sitecore.Web.UI.WebControls.FieldRenderer.Render(item, "example date", "format=M");
string MM = Sitecore.Web.UI.WebControls.FieldRenderer.Render(item, "example date", "format=MM");
string MMM = Sitecore.Web.UI.WebControls.FieldRenderer.Render(item, "example date", "format=MMM");
string MMMM = Sitecore.Web.UI.WebControls.FieldRenderer.Render(item, "example date", "format=MMMM");

//using : for colon and   for space
output.Write("{0}&#58;&#160;{1}", "Raw Format", "<b>" + rawDate + "</b><br/>");
output.Write("{0}&#58;&#160;{1}", "M Format", "<b>" + M + "</b><br/>");
output.Write("{0}&#58;&#160;{1}", "MM Format", "<b>" + MM + "</b><br/>");
output.Write("{0}&#58;&#160;{1}", "MMM Format", "<b>" + MMM + "</b><br/>");
output.Write("{0}&#58;&#160;{1}", "MMMM Format", "<b>" + MMMM + "</b>");
output.RenderEndTag(); //Div


Displaying Months C# Web Control Output

Raw Format: 10/4/2013 3:15:21 PM
M Format: 
MM Format: 
MMM Format: 
MMMM Format: