Share via

JSON code for colouring dates

Anonymous
2024-02-29T15:09:40+00:00

Hi

I’m trying to colour code dates in a SharePoint list.

My column is a calculated column which is calculating a maintenance date based off the last completed maintenance.

I’m trying to code red if overdue

Yellow if due this year (or within next 12 months)

Green if it’s not due for 12+ months

So far tried this based on years - would I be better working in months?

{ "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json", "elmType": "div", "txtContent": "@currentField", "style": { "color": "=if(@currentField <= @now, 'red', if(YEAR(@currentField) = YEAR(@now), 'yellow', if(YEAR(@currentField) > YEAR(@now) + 1, 'green', '')))", "font-weight": "bold" } }

Microsoft 365 and Office | SharePoint | For business | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

1 answer

Sort by: Most helpful
  1. Anonymous
    2024-02-29T20:03:14+00:00

    Hi Zoe,

    Thanks for posting in the community. We are happy to help you.

    Based on my test, please check if the following JSON code helps.

    {

      "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",

      "elmType": "div",

      "txtContent": "@currentField",

      "style": {

        "color": "=if(@currentField == '', '', if(@currentField < @now, 'red', if(@currentField <= @now + 31536000000, 'yellow', 'green')))",

        "font-weight": "bold"

      }

    }

    If the above JSON code doesn't work well, since your query is related to JSON code, and we have a specific channel Microsoft Q&A Community coping with JSON code questions, I would like to suggest you post a new thread (assign the tag SharePoint) there for expert help.

    Also, we welcome community members to share ideas about the situation if one has related experiences.

    Sincerely, 

    George | Microsoft Community Moderator

    Was this answer helpful?

    0 comments No comments