Kentico - 获取/格式化日期时间

时间:2016-10-28 06:08:23

标签: kentico

我在页面模板中有以下代码,但不知道如何格式化ReleaseDate以显示完整的月份和年份,例如2016年10月。请帮忙!

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

在另一个转换中,我有<%# GetDateTime("ReleaseDate", "MMMM yyyy") %>并且它有效,但我不知道如何在上述环境中使用它。我尝试了CurrentDocument.GetDateTime(....),但没有工作。

5 个答案:

答案 0 :(得分:1)

您无法使用transformation methods外部转换。但是,您可以使用vanilla .NET方法:

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

如果ReleaseDate可以为null,请确保应用空检查。

答案 1 :(得分:1)

你可以直接使用C#:

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

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

答案 2 :(得分:0)

所提供的所有答案都可行。但是问题是,无论何时在页面模板或布局中使用<%= %>,由于页面生命周期,它都会使页面出错。因此,当您在“页面”应用程序中以设计模式查看页面时,它将不可见或导致错误。

我使用的解决方案有两种:

  • 在转换中添加一个in语句,以检查它是否是最后一个,并使用动态内容呈现该文本。

您的陈述在转型中看起来像这样:

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

  • 将内容放在webpart的Content After部分中,并使用Zach Perry提到的简单宏。

你的宏代码看起来像这样:

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

答案 3 :(得分:0)

您可以使用:

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

答案 4 :(得分:-1)

这是Kentico宏,应该可以满足您的需求:

{% GetDateTime(CurrentDocument.GetValue("ReleaseDate"), "MMMM yyyy")%} 
相关问题