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/v2/column-formatting.schema.json",
"elmType": "div",
"debugMode": true,
"txtContent": "@currentField",
"style": {
"color": "=if([$ExpirationDate] <= @now, '#ff0000', ''"
}
}
Where ExpirationDate
is the internal name of your SharePoint list column. You can get the internal name of your SharePoint list columns by following this article: How to find the Internal name of columns in SharePoint Online?
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:
- SharePoint 2013 Client Side Rendering: List Views.