Modify Date Format for "last modified" in SharePoint:CreatedModifiedInfo
I am trying to modify the date format displayed in a custom template using the field. What I want to display is this:
This page was last modified on 29 Jan 2010 by Joel Spolsky
(where the user name links to their profile).
I found some good examples here on http://mindsharpblogs.com/aaron/archive/2008/02/08/4283.aspx which helped me get the custom template set up. And I looked through the class members on the MSDN documentation. But I cannot figure out how to modify the date format.
Is there any way to pass a date format string such as "d MMM yyyy" to the FieldValue to use for rendering?
This is my current code which works except the date format comes in as 29/01/2010 19:22 which isn't as user-friendly.
<SharePoint:CreatedModifiedInfo ControlMode="Display" runat="server">
<CustomTempla开发者_运维技巧te>
This page was last modified on
<SharePoint:FieldValue FieldName="Modified" runat="server" ControlMode="Display" DisableInputFieldLabel="true"/>
by
<SharePoint:FormField FieldName="Author" runat="server" ControlMode="Display" DisableInputFieldLabel="true" />
</CustomTemplate>
</SharePoint:CreatedModifiedInfo>
I think you can call there directly item field value and format it. Try this code below and add import tag to header.
<%@ Import Namespace="Microsoft.SharePoint" %>
...
<SharePoint:CreatedModifiedInfo ControlMode="Display" runat="server">
<CustomTemplate>
This page was last modified on
<%=SPContext.Current.ListItem["Modified"]==null?"":((DateTime)SPContext.Current.ListItem["Modified"]).ToString("d MMM yyyy")%>
by
<SharePoint:FormField FieldName="Author" runat="server" ControlMode="Display" DisableInputFieldLabel="true" />
</CustomTemplate>
</SharePoint:CreatedModifiedInfo>
I found a slightly different solution the problem. I'm not 100% happy with it, but it's pretty simple. This was based on a solution I read here: http://panvega.wordpress.com/2009/03/16/masterpagepagelayout-format-date-field/
You create a custom column on the library of type "Calculated" and format the date however you want. In my case that meant:
=TEXT(Modified,"d MMM yyyy")
Then in the page layout I just reference this field and the formatting is already done:
<SharePointWebControls:CalculatedField ID="CalculatedField" FieldName="Display Date" runat="server" />
It's not as elegant a solution as I was looking for mostly because it requires adding a custom column on every page library. But it requires very little code.
I'm still open to a better solution. It's seems strange to have to write a whole web control just to format a date, but it seems that might be the only other better alternative.
精彩评论