0
votes

I'm working in SharePoint 2013, and I have created a custom display template for a Content Search Web Part. Three of my fields use dates, and all three are returning the dates in long format. I want to return the dates in short format but I just can't seem to get it to work.

I've tried the advice from these blog articles:

http://www.dotnetmafia.com/blogs/dotnettipoftheday/archive/2014/02/26/useful-javascript-for-working-with-sharepoint-display-templates-spc3000-spc14.aspx

https://sharedpointtips.blogspot.com/2015/01/sharepoint-2013-display-template.html

https://sharepoint.protiviti.com/blog/Lists/Posts/Post.aspx?ID=132

http://sharepointfordeveloper.blogspot.com/2015/11/shareppoint-2013-ctx-variable-and-its_17.html

In the Header:

'Review Date'{Review Date Label}:'ReviewDateOWSDATE',

In the JavaScript section I have tried this:

var shortDate = Srch.U.toFormattedDate(dateresponsedue,'ShortDatePattern');

And this:

var shortDate2 = Srch.U.toFormattedDate(shortDate, 'ShortDatePattern');```

And this:

```var shortDate = Srch.U.toFormattedDate(ctx.CurrentItem.DateResponseDueforReviewOWSDATE, 'ShortDatePattern');```

This is my  display code:

```<td rowspan="3" width="85px" style="text-align:center;"> _#= shortDate =#_ </td>'''



I need the date display to change from something like this:

2019-07-11T05:00:00Z;7/11/2019 5:00:00 AM 

To something like this:

2019-07-11
1

1 Answers

0
votes

Considering that Sharepoint base Date format will always be YYYY-MM-DDTHH:MM:SS , if you want to get only the date at the same format you can do:

var fullDate = ctx.CurrentItem.DateResponseDueforReviewOWSDATE;
var shortDate = fullDate.split('T')[0];

/*
in this case 
2019-07-11T05:00:00Z;7/11/2019 5:00:00 AM  returns  2019-07-11
*/

If you then want to re-convert the date to a different format, you can convert the above format to a regular javascript Date object and work with it.