I am using the following code to display date in a publishing page layout.

<SharePointWebControls:FieldValue runat="server" FieldName="Created" id="CreatedOn"/>

This displays both date & time and I only want to display date and not time.
Secondly it displays date in format


and I want it in format

month_name dd, yyyy

For e.g. December 04, 2013

How can I do that?


Probably many people would say to use the DateTimeField and set DateOnly="True", like this:

<SharePointWebControls:DateTimeField runat="server" DateOnly="true" FieldName="Created" id="CreatedOn"/>

But that tends to create problems and throws errors.

What I would do is to use inline code:

<%=SPContext.Current.ListItem["Created"] != null? (new DateTime(SPContext.Current.ListItem["Created"].ToString()).ToString("USE FORMAT DATE STRING HERE"): string.Empty %>

I have not tried the above codes solidity, but it should give you an idea of how to use it.

More on the DateTime.ToString(string) here: http://msdn.microsoft.com/en-us/library/zdtaw1bw(v=vs.110).aspx

| improve this answer | |
  • Can I use the above code in Page Layout in SharePoint designer? – Frank Martin Dec 4 '13 at 11:05
  • In a page layout it should be OK, but why not just update the Page Layout in your solution and update it? – Robert Lindgren Dec 4 '13 at 11:22
  • You mean I should open it in Visual Studio and then edit? – Frank Martin Dec 4 '13 at 11:41
  • That would be the persistant approach – Robert Lindgren Dec 4 '13 at 11:54

I had to do something similar to Aakash, but also added a check as to not break edit mode.

<div id="my-change-date" class="my-date-line">..</div>
   $(function () {
   var date = new Date($("#my-change-date").text());
   if (date instanceof Date && !isNaN(date.valueOf())) {
| improve this answer | |

Yes, DateOnly="true" throw error. So, you can replace value by script like this:

wrap the field

<span class="myDate">
    <SharePointWebControls:FieldValue runat="server" FieldName="Created" id="CreatedOn"/>

set value by js :

    if($('.myDate').text() != ""){
       var date = $('.myDate').text().split(' ')[0].split('/')
       var formatDate = new Date(date[2],date[1]-1,date[0])    
       $('.myDate').text(formatDate.format("MMM dd,yyyy"))
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.