1

I would like to realize the following:

If the date in a column is more than 40 days old, the background should turn orange. If the date is more than 80 days in the past, the background should turn red. Otherwise the background should turn green.

The following code should actually be correct. No errors are displayed. However, all dates that are more than 40 days in the past turn orange. No date turns red or green.

Perhaps someone can help me here.

{
"elmType": "div",
"txtContent": "@currentField",
"style":{
"background-color":
{"operator": "?", "operands":
 [
  {
   "operator": "<=",
   "operands":
    ["@currentField",
     {"operator": "-",
      "operands": ["@now",4320000000]
     }
    ]
  },"#ff8a00",
  {                          
   "operator": "<=",
   "operands":
    ["@currentField",
     {"operator": "-",
      "operands": ["@now",6912000000]
     }
    ]
  },"#ff0000",
    "#00ff00"
  ]
 }            
}
}

1 Answer 1

0

Try using SharePoint JSON column formatting like below for your date column:

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/v1/column-formatting.schema.json",
  "elmType": "div",
  "txtContent": "[$StartDate.displayValue]",
  "style": {
    "padding-left": "15px",
    "color": "white",
    "background-color": {
      "operator": "?",
      "operands": [
        {
          "operator": "<=",
          "operands": [
            "[$StartDate]",
            {
              "operator": "-",
              "operands": [
                "@now",
                6912000000
              ]
            }
          ]
        },
        "#ff0000",
        {
          "operator": "?",
          "operands": [
            {
              "operator": "<=",
              "operands": [
                "[$StartDate]",
                {
                  "operator": "-",
                  "operands": [
                    "@now",
                    4320000000
                  ]
                }
              ]
            },
            "#ff8a00",
            "#00ff00"
          ]
        }
      ]
    }
  }
}

Where [$StartDate] is an internal name of your date column in SharePoint list in this format: [$InternalNameOfColumn].

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?.

4
  • Output: imgur.com/PzRY9DZ Commented Jan 30 at 14:59
  • I'm close to the finish line. The backgrounds color correctly depending on the date. However, the fields are now empty and no longer show the date. If I remove ".displayValue" from "txtContent": "[$StartDate.displayValue]", the date including time is displayed. I don't want to see the time. I'm starting to feel like a bit of an idiot. That can not be that hard.
    – Bollo
    Commented Jan 31 at 6:17
  • Ok, problem solved. If I swap "[$StartDate.displayValue]" for "@currentField", the date is there again. Thanks for help
    – Bollo
    Commented Jan 31 at 9:07
  • Yes, @currentField should also work as you are applying formatting for same date field and trying to show the value of same date field. Commented Jan 31 at 9:12

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.