Search code examples
sharepointsharepoint-2013display-templates

Changing date format in display templates for SharePoint 2013


I have a Content Search Webpart that uses a customized Display Template to display results of recent changed documents. I would like to show the "last modified-date" below the document Title.

I can then use #= ctx.CurrentItem.ModifiedOWSDATE =# to get the date, however the returned date is displayed as 2013-05-16T12:54:10Z.

How can I format the date so it is displayed like 16.05.2013 - 12:54 ?


Solution

  • iirc SharePoint should handle the date conversion if you use the same approach as you do with new Property Mappings.

    Copy/paste a variable in the template, something like:

    var modifiedDate =  $getItemValue(ctx, "ModifiedOWSDATE");
    modifiedDate.overrideValueRenderer($contentLineText);
    

    And use that variable instead:

    <p>_#= modifiedDate =#_</p>
    

    If creating a new custom Value Renderer etc is not an option for formatting the date (The default pattern used by Search.ClientControls isLongDatePattern I think). One could always create a Date variable, and do as fit:

    Example:

    <p>_#= new Date(modifiedDate["inputValue"]).toLocaleString() =#_</p>