1

I'm trying to color a column item based on date value. I'd like to highlight date if it was > 6months in the past. Below is what I tried but doesn't work as text becomes completely hidden afterwards

{
      "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
      "elmType": "div",
      "style": {
        "background-color": "=if(Number(@currentField) == 0, '', if(@currentField < subDays(@now, 180), '#808080', if(@currentField >= subDays(@now, 180) && @currentField <= @now, '#FFBF00', if(@currentField > @now && @currentField <= addDays(@now, 30), '#00FF00', if(@currentField > addDays(@now, 30), '#FF0000', '')))))"
      },
      "children": [
        {
          "elmType": "span",
          "txtContent": "@currentField",
          "style": {
            "color": "black"
          }
        }
      ]
    }
1
  • Try below JSON once and let me know if it works for you or if you need any further help with JSON. Commented Apr 4 at 13:08

1 Answer 1

0

There is no subDays operator available in SharePoint JSON formatting.

You have to use addDays in below format to get the date in the past:

addDays(@now, -180)

Here is a simple example for your reference:

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
  "elmType": "div",
  "txtContent": "@currentField",
  "style": {
    "background-color": "=if(Number(@currentField)==0,'',if(@currentField < addDays(@now, -180),'#808080','#0b6623'))",
    "color": "=if(Number(@currentField)==0,'','#FFFFFF')",
    "padding-left": "10px"
  }
}

Here I have used Number(@currentField) != 0 to check if SharePoint date column is blank or not. You can find other ways to check if SharePoint date & time column is empty or not at: SharePoint JSON formatting: Check if date & time column is blank/empty.

You can adjust above JSON as per your requirements to add more conditions or to change the colors/styling, etc.

Output:

enter image description here

1
  • 1
    Amazing- your solution works. Thanks!
    – Bialy778
    Commented Apr 4 at 13:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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