Reputation: 249
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
Reputation: 21
<%# 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
Reputation: 287
You can use:
<%= String.Format("{0:MMMM yyyy}", CurrentDocument.GetValue("ReleaseDate")) %>
Upvotes: 0
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:
Your statement could look something like this in your transformation:
<%# If(IsLast(), GetDateTime("ReleaseDate", "MMMM yyyy"), "") %>
Your macro code would look like so:
{% GetDateTime(CurrentDocument.GetValue("ReleaseDate"), "MMMM yyyy")%}
Upvotes: 0
Reputation: 756
Here is the Kentico macro that should get you what you want:
{% GetDateTime(CurrentDocument.GetValue("ReleaseDate"), "MMMM yyyy")%}
Upvotes: -1
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
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