Michelle
Michelle

Reputation: 249

Kentico - Get/Format Date Time

I have the following code in the page template but don't know how to format ReleaseDate to display full month and year e.g. October 2016. Please help!

<ul class="icon-list">     
<cms:CMSWebPartZone ZoneID="ZoneLinks" runat="server" />
<li class="date inline">Published: <%= CurrentDocument.GetValue("ReleaseDate") %></li>
</ul>

In a different transformation, I have this <%# GetDateTime("ReleaseDate", "MMMM yyyy") %> and it works, but I don't know how to use it in the above context. I have tried CurrentDocument.GetDateTime(....), but didn't work.

Upvotes: 3

Views: 2561

Answers (6)

Amarnath behera
Amarnath behera

Reputation: 21

Query

<%# String.IsNullOrEmpty(GetDate("Date").ToString()) ? FormatDate(DateTime.Now) : GetDate("Date") %>

Transformation type: ASCX

Already I tested in my local and its working: Expected Output

Upvotes: 0

Anton Grekhovodov
Anton Grekhovodov

Reputation: 287

You can use:

<%= String.Format("{0:MMMM yyyy}", CurrentDocument.GetValue("ReleaseDate")) %>

Upvotes: 0

Brenden Kehren
Brenden Kehren

Reputation: 6117

All the answers provided will work BUT. The problem is anytime you use <%= %> in a page template or layout, it will error out the page because of the page lifecycle. So when you view the page in Design mode in the Pages app it will not be visible or cause an error.

There are 2 solutions I use for something like this:

  • put an in statement in your transformation to check if it is the last one or not and render that text with dynamic content.

Your statement could look something like this in your transformation:

<%# If(IsLast(), GetDateTime("ReleaseDate", "MMMM yyyy"), "") %>

  • place the content in the Content After portion of the webpart and use a simple macro as Zach Perry mentioned.

Your macro code would look like so:

{% GetDateTime(CurrentDocument.GetValue("ReleaseDate"), "MMMM yyyy")%}

Upvotes: 0

Zach Perry
Zach Perry

Reputation: 756

Here is the Kentico macro that should get you what you want:

{% GetDateTime(CurrentDocument.GetValue("ReleaseDate"), "MMMM yyyy")%} 

Upvotes: -1

Enn
Enn

Reputation: 2209

You can use C# directly like this:

<%= ((System.DateTime)CMS.DocumentEngine.DocumentContext.CurrentDocument.DocumentModifiedWhen).ToString("MMMM yyyy") %>

<%= ((System.DateTime)CMS.DocumentEngine.DocumentContext.CurrentDocument.GetValue("ReleaseDate")).ToString("MMMM yyyy") %>

Upvotes: 1

rocky
rocky

Reputation: 7696

You can't use transformation methods outside transformations. However, you can use vanilla .NET approach:

<%= ((DateTime)CurrentDocument.GetValue("ReleaseDate")).ToString("MMMM yyyy") %>

Make sure you apply a null check if ReleaseDate can be null.

Upvotes: 1

Related Questions