Format ISO Date

Format ISO Date

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
  • Sitecore.DateUtil
  • Sitecore.Web.UI.WebControls.FieldRenderer
  • System.DateTime

Format ISO Date C# Web Control Code

Sitecore.Data.Database db = Sitecore.Context.Database;
Sitecore.Data.Items.Item item = db.GetItem("{EC44470F-FF45-4023-95C2-EC886FD33D0C}");
if (item == null) return;

string rawDate1 = item["example date"];
if (string.IsNullOrEmpty(rawDate1)) return;

Sitecore.Data.Fields.DateField date = item.Fields["example date"];
if (date == null) return;
string isoDate = Sitecore.DateUtil.ToIsoDate(date.DateTime);

string rawDate2 = date.Value;
if (string.IsNullOrEmpty(rawDate2)) return;

System.DateTime dateTime = date.DateTime;
if (dateTime == 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", item.DisplayName);
output.RenderEndTag(); //P

string MMddyy1 = Sitecore.DateUtil.FormatIsoDate(isoDate, "MM/dd/yy");
string yyMMdd1 = Sitecore.DateUtil.FormatIsoDate(isoDate, "yyMMdd");
string MMMddyyyy1 = Sitecore.DateUtil.FormatIsoDate(isoDate, "MMM dd, yyyy");
string MMMMddyyyy1 = Sitecore.DateUtil.FormatIsoDate(isoDate, "MMMM dd, yyyy");

output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.P);
output.Write("Using DateField Class");
output.RenderEndTag(); //P
output.Write("{0}&#58;&#160;{1}", "Date Raw Format", "<b>" + rawDate1 + "</b><br/>");
output.Write("{0}&#58;&#160;{1}", "Default Format", "<b>" + dateTime.ToString() + "</b><br/>");
output.Write("{0}&#58;&#160;{1}", "MM/dd/yy Format", "<b>" + MMddyy1 + "</b><br/>");
output.Write("{0}&#58;&#160;{1}", "yyMMdd Format", "<b>" + yyMMdd1 + "</b><br/>");
output.Write("{0}&#58;&#160;{1}", "MMM dd, yyyy Format", "<b>" + MMMddyyyy1 + "</b><br/>");
output.Write("{0}&#58;&#160;{1}", "MMMM dd, yyyy Format", "<b>" + MMMMddyyyy1 + "</b><br/>");

string defaultDisplay2 = Sitecore.Web.UI.WebControls.FieldRenderer.Render(item, "example date");
string MMddyy2 = Sitecore.Web.UI.WebControls.FieldRenderer.Render(item, "example date", "format=MM/dd/yy");
string yyMMdd2 = Sitecore.Web.UI.WebControls.FieldRenderer.Render(item, "example date", "format=yyMMdd");
string MMMddyyyy2 = Sitecore.Web.UI.WebControls.FieldRenderer.Render(item, "example date", "format=MMM dd, yyyy");
string MMMMddyyyy2 = Sitecore.Web.UI.WebControls.FieldRenderer.Render(item, "example date", "format=MMMM dd, yyyy");

output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.P);
output.Write("Using FieldRenderer Class");
output.RenderEndTag(); //P
output.Write("{0}&#58;&#160;{1}", "Date Raw Format", "<b>" + rawDate2 + "</b><br/>");
output.Write("{0}&#58;&#160;{1}", "Default Format", "<b>" + defaultDisplay2 + "</b><br/>");
output.Write("{0}&#58;&#160;{1}", "MM/dd/yy Format", "<b>" + MMddyy2 + "</b><br/>");
output.Write("{0}&#58;&#160;{1}", "yyMMdd Format", "<b>" + yyMMdd2 + "</b><br/>");
output.Write("{0}&#58;&#160;{1}", "MMM dd, yyyy Format", "<b>" + MMMddyyyy2 + "</b><br/>");
output.Write("{0}&#58;&#160;{1}", "MMMM dd, yyyy Format", "<b>" + MMMMddyyyy2 + "</b><br/>");

output.RenderEndTag(); //Div


Format ISO Date XSLT Rendering Code

<xsl:variable name="simpleFields" select="sc:item('{EC44470F-FF45-4023-95C2-EC886FD33D0C}',.)" />

<div style="font-weight:bold; width:100%; clear:both"> <br/>
<xsl:value-of select="'XSLT Rendering Real-time Output'"/>
</div>

<div style="font-size:x-small; width:100%; clear:both">
<p><b>Item: <xsl:value-of select="$simpleFields/@name"/></b></p>

<xsl:if test="(sc:fld('example date',$simpleFields)!='')">
Date Raw Format: <b><xsl:value-of select="sc:fld('example date',$simpleFields)" /></b><br/>
Default Format: <b><sc:date field="example date" select="$simpleFields" /></b><br/>
MM/dd/yy Format: <b><sc:date field="example date" format="MM/dd/yy" select="$simpleFields" /></b><br/>
yyMMdd Format: <b><sc:date field="example date" format="yyMMdd" select="$simpleFields" /> </b><br/>
MMM dd, yyyy Format: <b><sc:date field="example date" format="MMM dd, yyyy" select="$simpleFields" /></b><br/>
MMMM dd, yyyy Format: <b><sc:date field="example date" format="MMMM dd, yyyy" select="$simpleFields" /></b>
</xsl:if>

</div>


Format ISO Date C# Web Control Output

Using DateField Class

Date Raw Format: 10/4/2013 3:15:21 PM
Default Format: 1/1/0001 12:00:00 AM
MM/dd/yy Format: 
yyMMdd Format: 
MMM dd, yyyy Format: 
MMMM dd, yyyy Format: 

Using FieldRenderer Class

Date Raw Format: 10/4/2013 3:15:21 PM
Default Format: 
MM/dd/yy Format: 
yyMMdd Format: 
MMM dd, yyyy Format: 
MMMM dd, yyyy Format: 

Format ISO Date XSLT Rendering Output

Date Raw Format: 10/4/2013 3:15:21 PM
Default Format: 
MM/dd/yy Format: 
yyMMdd Format: 
MMM dd, yyyy Format: 
MMMM dd, yyyy Format:

Sources