1

I am having an issue when trying to format my date column on a list. I want the background color to turn red if the date is less than or equal to Today – 10 days, else its green.

I cant see how to figure it out.

Here is the JSON:

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column- 
    formatting.schema.json",
  "elmType": "div",
  "txtContent": "@currentField",
  "style": {
    "background-color": "if(@currentField >= @now + 864000000,'#f7cac9','#f7cac9')"
    }
}

I have tried to use Date(@now,toDateString(), but it doesn't work.

1 Answer 1

0

Use this JSON

{
  "$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, -10),'red','green'),'')",
    "color": "white",
    "font-weight": "bold",
    "padding-left": "15px"
  }
}

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

Also, if you want to refer the date column value in JSON using column name, you will have to use the internal name of column. You can get the internal name of your column by following this article: How to find the Internal name of columns in SharePoint Online?

Output:

enter image description here

Documentation: SharePoint JSON formatting

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.