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 ?

Terje A T
  • 63
  • 1
  • 1
  • 3

1 Answers1

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>
Anders Aune
  • 384
  • 4
  • 8
  • 1
    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(); – Denis Molodtsov Nov 23 '14 at 13:16
  • 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. – Draghon May 21 '15 at 17:15