2

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

mm/d/yyyy

and I want it in format

month_name dd, yyyy

For e.g. December 04, 2013

How can I do that?

3 Answers 3

3

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

4
  • Can I use the above code in Page Layout in SharePoint designer? Dec 4, 2013 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? Dec 4, 2013 at 11:22
  • You mean I should open it in Visual Studio and then edit? Dec 4, 2013 at 11:41
  • That would be the persistant approach Dec 4, 2013 at 11:54
1

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>
<script>
   $(function () {
   var date = new Date($("#my-change-date").text());
   if (date instanceof Date && !isNaN(date.valueOf())) {
   $("#my-change-date").text(date.format("M/d/yyyy"));
   }
   });
</script>
0

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"/>
</span>

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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