3

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.

rocky
  • 7,506
  • 3
  • 33
  • 48
Michelle
  • 249
  • 4
  • 14
  • You may want to reconsider who gets the reputation as I've answered you question first and correct. And still, I got minus points from the others... – rocky Oct 30 '16 at 06:58

6 Answers6

1

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.

rocky
  • 7,506
  • 3
  • 33
  • 48
1

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") %>
Enn
  • 2,137
  • 15
  • 24
0

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")%}

Brenden Kehren
  • 5,919
  • 16
  • 27
0

You can use:

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

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

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
-1

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

{% GetDateTime(CurrentDocument.GetValue("ReleaseDate"), "MMMM yyyy")%} 
Zach Perry
  • 746
  • 3
  • 12