Changing date format in display templates for SharePoint 2013
Asked Answered
I

1

6

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 ?

Irruption answered 22/5, 2013 at 11:45 Comment(0)
T
10

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>
Theriault answered 22/5, 2013 at 18:21 Comment(2)
You could just use ctx.CurrentItem.LastModifiedTime instead. Or, If you need to convert the date to a current locale: var modifiedDateTime = new Date(Date.parse(ctx.CurrentItem.LastModifiedTime)); modifiedDateTime = modifiedDateTime.toLocaleDateString() + " " + modifiedDateTime.toLocaleTimeString();Corrade
Additionally, you can format the date to suit your needs using #= new Date(dt).format("M/d/yyyy") =#. In my case, I did not want the leading zeroes in "MM/dd/yyyy". For the OP, the format would be "dd.MM.yyyy - HH:mm" to produce "16.05.2013 - 12:54" per the question.Baalman

© 2022 - 2024 — McMap. All rights reserved.