0

I have a display template that is working awesome but for the formatting of the Managed Property not displaying the date correctly. I do not want the time included in the display. I am still learning and really could use some help with this.

The code for the display template is below, in a hope that someone can provide me an answer quickly.

List Table Item-Countermeasures

0</mso:TemplateHidden> ListID','ListItemID','ContentTypeId','Link URL':'Path','Property 1':'Title','Property 2':'','Property 3':'','Property 4:'','Property 5':'','Property 6':'','Property 7':'','Property 8':'','Property 9':'','Property 10':'','FileExtension','SecondaryFileExtension'</mso:ManagedPropertyMapping> This is the item row template, use this template in combination with the List Table Control template.</mso:MasterPageDescription> 0x0101002039C03B61C64EC4A04F5361F385106603</mso:ContentTypeId> ;#SearchResults;#Content Web Parts;#</mso:TargetControlType> 1</mso:HtmlDesignAssociated> https://share.philips.com/sites/STS020170908232517/_catalogs/masterpage/Display Templates/Search/Item_List_Item-CM.html, Conversion successful.</mso:HtmlDesignStatusAndPreview> True</mso:HtmlDesignConversionSucceeded> </mso:CrawlerXSLFile> </mso:HtmlDesignPreviewUrl> </mso:CustomDocumentProperties> </xml>

var encodedId = $htmlEncode(ctx.ClientControl.get_nextUniqueId() + "tableitems");

var linkURL = $getItemValue(ctx, "Link URL"); linkURL.overrideValueRenderer($urlHtmlEncode); var iconURL = Srch.ContentBySearch.getIconSourceFromItem(ctx.CurrentItem);

var listID = $getItemValue(ctx, 'ListID'); var listItemID = $getItemValue(ctx, 'ListItemID'); var ContentTypeId = $getItemValue(ctx, 'ContentTypeId');

var startDate = new Date($getItemValue(ctx, "Start Date"));

var rowId = encodedId + "row"; var editCellId = rowId + "_cell";

var currentItemIdx = ctx.CurrentItemIdx + 1; var alternating = currentItemIdx % 2 === 0 ? "ms-alternating" : "";

if (currentItemIdx === 1) { _#-->

        <!--#_ 

        // Show the properties that aren't empty
        for(var i = 1; i <= 10; i++) { 
            var property = $getItemValue(ctx, String.format("Property {0}", i));
            if(property.managedPropertyName !== String.format("Property {0}", i)) {
                var propertyName = typeof propertyNames[property.propertyLookupName] === "undefined" ? property.managedPropertyName : propertyNames[property.propertyLookupName];
        _#-->
        <th class="ms-vh2">
            _#= propertyName =#_ 

            <span id="_#= property.managedPropertyName.replace(/ /g, '-') =#_">
                <span style="" class="sortarrow ms-sortarrowup-iconouter" ></span>
                <a id="_#= property.managedPropertyName =#_ASC" href="#" title="Ascending" onclick="$getClientControl(this).sortOrRank('_#= property.managedPropertyName =#_ASC');return false;">
                    <img alt="Ascending" src="/_layouts/15/images/sortaz.gif">
                </a>
                <a id="_#= property.managedPropertyName =#_DES" href="#" title="Descending" onclick="$getClientControl(this).sortOrRank('_#= property.managedPropertyName =#_DES');return false;">
                    <img alt="Descending" src="/_layouts/15/images/sortza.gif">
                </a>
            </span>
        </th>
        <!--#_ 
            }
        } 
        _#-->
    </tr>
</thead>
<tbody>

{1}", linkURL, property) : property; if (Object.prototype.toString.call(property.value) === '[object Date]') {value = property.value.format('MM/dd/yyyy'); }

        _#-->
                _#= value =#_
        <!--#_
                    if (i === 1 && !listID.isEmpty && !listItemID.isEmpty && !ContentTypeId.isEmpty) {
                        var viewPropUrl = String.format('/_layouts/15/listform.aspx?ListId={0}&ID={1}&ContentTypeID={2}&Source={3}', listID, listItemID, ContentTypeId, window.location.href.split('#')[0]);
        _#-->
                        &nbsp;&nbsp;
                        <span title="Open Menu" class="NotificationDiv ms-ellipsis-a" rel="_#= viewPropUrl =#_" ref="_#= property =#_" path="_#= linkURL =#_" style="cursor: pointer; margin-top: 3px; float: right;">
                            <img alt="Open Menu" src="/_layouts/15/images/spcommon.png" class="ms-ellipsis-icon">
                        </span>
        <!--#_
                    }
                }
        _#-->
            </td>
        <!--#_
            }
        } 
        _#-->
    </tr>

Thank you in advance.

Dorinda

UPDATE: the template is now rendering but the format is still not correct. I need help getting this working please

1
  • I have this partially working but really could use some help the formatting is not applying.
    – Dorinda
    Mar 2, 2019 at 17:09

1 Answer 1

0

After below line:

var startDate = new Date($getItemValue(ctx, "Start Date"));

Use below code:

var startDateFormatted = (startDate.getMonth() + 1) + '/' + startDate.getDate() + '/' +  startDate.getFullYear();

And use startDateFormatted to show on the page instead of startDate.

Update:

Try replacing value = property.value.format('MM/dd/yyyy'); with below code:

value = (property.value.getMonth() + 1) + '/' + property.value.getDate() + '/' +  property.value.getFullYear();
10
  • do i remove the line and replace with the one you provided? I added what you suggested and it now has broken the display template
    – Dorinda
    Feb 28, 2019 at 15:25
  • No... Don't replace... Just place it after startDate initialization line.... And use code from updated answer... There was missing parenthesis Feb 28, 2019 at 15:50
  • I added var startDateFormatted = new Date($getItemValue(ctx, "Start Date")); var startDateFormatted = (startDate.getMonth() + 1) + '/' + startDate.getDate() + '/' + startDate.getFullYear(); but it is still not working do i need to add anything down in the actual html?
    – Dorinda
    Feb 28, 2019 at 15:52
  • Yes, in html wherever you have used the startDate reference replace it with startDateFormatted Feb 28, 2019 at 16:00
  • the only place i reference a date is here <!--#_ if(!property.isEmpty) { var value = i === 1 ? String.format("<a href='{0}' class='ms-listlink'>{1}</a>", linkURL, property) : property; if (Object.prototype.toString.call(property.value) === '[object Date]') {value = property.value.format('MM/dd/yyyy'); } _#--> I dont know what to put?
    – Dorinda
    Feb 28, 2019 at 16:04

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.