Get Current Date

Get Current Date

Controls featured in this example:

  • Sitecore Web Control

Classes featured in this example:

  • Sitecore.Data.Items.Item
  • Sitecore.DateUtil

Current Date C# Web Control Code

Sitecore.Data.Items.Item item = this.GetItem();
if (item == 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

output.Write("{0}&#58;&#160;{1}", "Raw Date Value", "<b>" + Sitecore.DateUtil.IsoNow + "</b><br/>");
output.Write("{0}&#58;&#160;{1}", "Default Format", "<b>" + Sitecore.DateUtil.FormatIsoDate(Sitecore.DateUtil.IsoNow) + "</b><br/>");
output.Write("{0}&#58;&#160;{1}", "MMddyy Format", "<b>" + Sitecore.DateUtil.FormatIsoDate(Sitecore.DateUtil.IsoNow, "MM/dd/yy") + "</b><br/>");
output.Write("{0}&#58;&#160;{1}", "MMddyyyy Format", "<b>" + Sitecore.DateUtil.FormatIsoDate(Sitecore.DateUtil.IsoNow, "MM/dd/yyyy") + "</b><br/>");
output.Write("{0}&#58;&#160;{1}", "yyyy-MM-dd Format", "<b>" + Sitecore.DateUtil.FormatIsoDate(Sitecore.DateUtil.IsoNow, "yyyy-MM-dd") + "</b><br/>");
output.Write("{0}&#58;&#160;{1}", "MMMM dd, yyyy Format", "<b>" + Sitecore.DateUtil.FormatIsoDate(Sitecore.DateUtil.IsoNow, "MMMM dd, yyyy") + "</b>");

output.RenderEndTag(); //Div


Current Date XSLT Rendering Code

<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: <sc:text field="title" select="$sc_item" disable-web-editing="true"/></b></p>

Raw Date Value: <b><xsl:value-of select="sc:isoNow()" /></b><br/>
Default Format: <b><xsl:value-of select="sc:formatdate(sc:isoNow())" /></b><br/>
MMddyy Format: <b><xsl:value-of select="sc:formatdate(sc:isoNow(),'MM/dd/yy')" /></b><br/>
MMddyyyy Format: <b><xsl:value-of select="sc:formatdate(sc:isoNow(),'MM/dd/yyyy')" /></b><br/>
yyyy-MM-dd Format: <b><xsl:value-of select="sc:formatdate(sc:isoNow(),'yyyy-MM-dd')" /></b><br/>
MMMM dd, yyyy Format: <b><xsl:value-of select="sc:formatdate(sc:isoNow(),'MMMM dd, yyyy')" /></b><br/>
</div><br/>

<!-- alternative XSLT rendering -->
<div style="font-size:x-small; width:100%; clear:both">
<p><b>Item: <sc:text field="title" select="$sc_item" disable-web-editing="true"/></b></p>

<xsl:variable name="now" select="dateutil:get_IsoNow()" />

Raw Date Value: <b><xsl:value-of select="$now" /></b><br/>
Default Format: <b><xsl:value-of select="sc:formatdate($now)" /></b><br/>
MMddyy Format: <b><xsl:value-of select="sc:formatdate( $now, 'MM/dd/yy' )" /></b><br/>
MMddyyyy Format: <b><xsl:value-of select="sc:formatdate( $now, 'MM/dd/yyyy' )" /></b><br/>
yyyy-MM-dd Format: <b><xsl:value-of select="sc:formatdate( $now, 'yyyy-MM-dd' )" /></b><br/>
MMMM dd, yyyy Format: <b><xsl:value-of select="sc:formatdate( $now, 'MMMM dd, yyyy' )" /></b>
</div>


Current Date C# Web Control Output

Raw Date Value: 20141006T075819
Default Format: Monday, October 06, 2014 7:58 AM
MMddyy Format: 10/06/14
MMddyyyy Format: 10/06/2014
yyyy-MM-dd Format: 2014-10-06
MMMM dd, yyyy Format: October 06, 2014


Current Date XSLT Rendering Output

Raw Date Value: 20141006T075819
Default Format: 10/6/2014 7:58:19 AM
MMddyy Format: 10/06/14
MMddyyyy Format: 10/06/2014
yyyy-MM-dd Format: 2014-10-06
MMMM dd, yyyy Format: October 06, 2014


Raw Date Value: 20141006T075819
Default Format: 10/6/2014 7:58:19 AM
MMddyy Format: 10/06/14
MMddyyyy Format: 10/06/2014
yyyy-MM-dd Format: 2014-10-06
MMMM dd, yyyy Format: October 06, 2014

Notes

  • The system date value is from the time zone where the server is located
  • The first XSLT snippet is used to generate the real-time XSLT output below

Sources