I have a SharePoint list with a date column called 'Expiration Date' containing a date value.

Using some JS code, I would like to first check if the date value equals to today and if so I would like to highlight it in red.

Can someone help me with the JS code for the same.

Node: I am working with SPOnline.

Thanks in advance.

For Modern UI:

Yes, you can use json formatting to conditionally color fields in sharepoint online.

The following example colors the current date field red when the value inside an item's ExpirationDate is before the current date/time.

{
   "$schema": 
   "https://developer.microsoft.com/json-schemas/sp/column-formatting.schema.json",
   "elmType": "div",
   "debugMode": true,
   "txtContent": "@currentField",
   "style": {
       "color": "=if([$ExpirationDate] <= @now, '#ff0000', ''"
   }
}

Source: Use column formatting to customize SharePoint.

For Classic UI:

You can use Client Side Rendering (CSR) which represents a rendering engine for list views, list forms and search results. I would recommend you to consider the following approach for your task.

Below example demonstrates how to highlight list rows based on their date values:

SP.SOD.executeFunc("clienttemplates.js", "SPClientTemplates", function() { 

  SPClientTemplates.TemplateManager.RegisterTemplateOverrides({
    OnPostRender: function (ctx) {

        // get today's date
        var today = new Date();
        // zero out the time portion so we will only compare days
        today.setHours(0,0,0,0);

        var rows = ctx.ListData.Row;
        for (var i = 0; i < rows.length; i++) {

            // get the date set in your date YourDateField
            var itemDate = new Date(rows[i]['ExpirationDate']);
            // zero out the time portion so we only compare days
            itemDate.setHours(0,0,0,0);

            var rowId = GenerateIIDForListItem(ctx, rows[i]);
            var row = document.getElementById(rowId);

            if (itemDate >= today) {
                row.style.backgroundColor = '#ED9898';
            }
        }
    }
  });
});

For more clarification and how to add this code in JSLink of list view, see my answer How to Highlight a Row on Active Status.

Source:

  1. SharePoint 2013 Client Side Rendering: List Views.
  • 1
    Thanks Ganesh. Does this work in Modern UI only? Should I use the internal name of Expiration Date field? – mdevm Nov 9 at 2:07
  • Yes. It works in Modern UI and use internal name of date field. – Ganesh Sanap Nov 9 at 2:19
  • 1
    This will work only in Modern UI. for classic UI you need to use JSLink – Rahil Jan Muhammad Nov 9 at 2:22
  • @mdevm are you using modern UI or classic UI? – Ganesh Sanap Nov 9 at 2:30
  • I am using CLassic UI – mdevm Nov 9 at 2:36

In Classic UI, we can add the following code into a script editor web part in list view to achieve it.

<script type="text/javascript">
(function () { 
    // Create object that have the context information about the field that we want to change it's output render  
    var fieldContext = {}; 
    fieldContext.Templates = {}; 
    fieldContext.Templates.Fields = { 
        // Apply the new rendering for Priority field on List View 
        "Expiration_x0020_Date": { "View": fieldTemplate } 
    }; 
    SPClientTemplates.TemplateManager.RegisterTemplateOverrides(fieldContext);
})(); 

// This function provides the rendering logic for list view 
function fieldTemplate(ctx) { 
    var expirationDate = ctx.CurrentItem[ctx.CurrentFieldSchema.ExpirationDate];
    // Return html element with appropriate color based on priority value 
    if(new Date(expirationDate) <= new Date()) {
        return "<span style='color : red'>" + expirationDate + "</span>";
    }else {
        return expirationDate;
    }    
}
</script>

enter image description here

You can use calculated columns or js link See example in the link below

Calculated Column to change text color in a custom list

  • 2
    Do not use calculated column as MS has removed this functionality from SharePoint Online. – Rahil Jan Muhammad Nov 9 at 2:23

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.