How do I use JSON Conditional Formatting in a SharePoint Online List to color-code dates 7, 30, 90, 180 days or more out?

Crystal D. Wood 0 Reputation points
2024-09-13T21:37:21.61+00:00

I have a SharePoint List that tracks when new Government Proposals are released. The $Deadline column is the RFP Release date, and the $Stage column shows whether or not we're pursuing the proposal. I color-coded the $Deadline column to know how soon the RFP would be released (1 week, 1 month, 3 months, 6 months) using the OOTB formatting to get it to work. Unfortunately, the OOTB option only gives you a "fixed date" which essentially "hard codes" the dates into the JSON (which means I have to go in every few days and manually change them in order for the color-coding to be correct).

I tried "Advanced mode" to look at the JSON code, but I don't understand JSON enough to convert the operands date string part to do the math of adding 7 days, 30 days, 90 days, 180 days...

I even looked up the whole "milliseconds" thing, but still, HOW do I get that into the JSON?

7 d = 604800000 ms

30 d = 2592000000 ms

90 d = 7776000000 ms

180 d = 15552000000 ms

OVERDUE: "sp-css-backgroundColor-BgCoral sp-css-borderColor-CoralFont sp-css-color-CoralFont"

1 WEEK: "sp-css-backgroundColor-BgPeach sp-css-borderColor-PeachFont sp-css-color-PeachFont"

1 MONTH: "sp-css-backgroundColor-BgGold sp-css-color-GoldFont"

3 MONTHS: "sp-css-backgroundColor-BgMintGreen sp-css-borderColor-MintGreenFont sp-css-color-MintGreenFont"

6 MONTHS: "sp-css-backgroundColor-BgCyan sp-css-color-CyanFont"

(anything else): "sp-css-backgroundColor-BgLightGray sp-css-borderColor-LightGrayFont sp-css-color-LightGrayFont"

SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
2,980 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Ling Zhou_MSFT 17,150 Reputation points Microsoft Vendor
    2024-09-16T02:39:24.97+00:00

    Hi @Crystal D. Wood,

    Thank you for posting in this community.

    You can use the column formatting JSON below to calculate the difference in days between today and the deadline and modify the style of the columns. We use @now to get today's date dynamically, so we don't need to manually modify today's date.

    {
      "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
      "elmType": "div",
      "attributes": {
        "class": "=if((floor((Number([$DeadLine]-Number(@now)))/(1000*60*60*24)))<0,'sp-css-backgroundColor-BgCoral sp-css-borderColor-CoralFont sp-css-color-CoralFont',if((floor((Number([$DeadLine]-Number(@now)))/(1000*60*60*24)))<=7,'sp-css-backgroundColor-BgPeach sp-css-borderColor-PeachFont sp-css-color-PeachFont',if((floor((Number([$DeadLine]-Number(@now)))/(1000*60*60*24)))<=30,'sp-css-backgroundColor-BgGold sp-css-color-GoldFont',if((floor((Number([$DeadLine]-Number(@now)))/(1000*60*60*24)))<=90,'sp-css-backgroundColor-BgMintGreen sp-css-borderColor-MintGreenFont sp-css-color-MintGreenFont',if((floor((Number([$DeadLine]-Number(@now)))/(1000*60*60*24)))<=180,'sp-css-backgroundColor-BgCyan sp-css-color-CyanFont','sp-css-backgroundColor-BgLightGray sp-css-borderColor-LightGrayFont sp-css-color-LightGrayFont')))))"
      },
      "txtContent": "@currentField"
    }
    

    Here is my test result:

    User's image

    Our core formula for calculating days is floor((Number([$DeadLine]-Number(@now)))/(1000*60*60*24)).

    To break down the calculation into pieces:

    • Number(…) will convert a date into a number in milliseconds.
    • Number([$DeadLine]-Number(@now) will take DeadLine's date in milliseconds and subtract from today’s date in milliseconds.
    • (10006060*24) will calculate duration of a day in milliseconds: 1000 milliseconds * 60 seconds * 60 minutes * 24 hours.
    • floor((Number([$DeadLine]-Number(@now)))/(10006060*24)) will take the difference between dates, divide it by duration of a day, and round the final number down to a full number.

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. Ling Zhou_MSFT 17,150 Reputation points Microsoft Vendor
    2024-09-19T07:09:09.92+00:00

    Hi @Crystal D. Wood,

    Thanks for the documentation, it's amazing.

    For RFP date column:

    1.Select your RFP date column >column settings >Formatting this column >Advanced mode.

    User's image

    User's image

    Please paste the following JSON code. Where your logic is the value between dates, so I'm not quite sure if it's >, <, >=, or <=. If it doesn't fit, you can make changes yourself.

    {
      "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
      "elmType": "div",
      "attributes": {
        "class": "=if(([$Stage] == '5. Post Subm Activities' || [$Stage] == '6. Closed'),'',if((([$Status]=='Capture'||[$Status]=='Live'||[$Status]=='Temporary Hold')&&[$Deadline]),if([$Deadline]<Date(@now,toDateString()),'sp-css-backgroundColor-BgCoral sp-css-borderColor-CoralFont sp-css-color-CoralFont',if([$Deadline]>Date(@now,toDateString())&&[$Deadline]<=Date((@now+ 7 * 24*60*60*1000),toDateString()),'sp-css-backgroundColor-BgPeach sp-css-borderColor-PeachFont sp-css-color-PeachFont',if([$Deadline]>Date((@now+ 7 * 24*60*60*1000),toDateString())&&[$Deadline]<=Date((@now+ 30 * 24*60*60*1000),toDateString()),'sp-css-backgroundColor-BgGold sp-css-color-GoldFont',if([$Deadline]>Date((@now+ 30 * 24*60*60*1000),toDateString())&&[$Deadline]<=Date((@now+ 90 * 24*60*60*1000),toDateString()),'sp-css-backgroundColor-BgMintGreen sp-css-borderColor-MintGreenFont sp-css-color-MintGreenFont',if([$Deadline]>Date((@now+ 90 * 24*60*60*1000),toDateString())&&[$Deadline]<=Date((@now+ 180 * 24*60*60*1000),toDateString()),'sp-css-backgroundColor-BgCyan sp-css-color-CyanFont','sp-css-backgroundColor-BgLightGray sp-css-borderColor-LightGrayFont sp-css-color-LightGrayFont'))))),'sp-css-backgroundColor-BgLightGray sp-css-borderColor-LightGrayFont sp-css-color-LightGrayFont'))"
      },
      "txtContent": "[$Deadline.displayValue]"
    }
    

    Descriptions:

    (@now + 7 * 24 * 60 * 60 * 1000): get the date after calculating today + N days.

    Date((@now+ 90 * 24*60*60*1000),toDateString()): get the date that doesn't include the time.

    For Submission Date Column:

    1.Select your Submission Date column >column settings >Formatting this column >Advanced mode.

    2.Please paste the following JSON code. Where your logic is the value between dates, so I'm not quite sure if it's >, <, >=, or <=. If it doesn't fit, you can make changes yourself.

    {
      "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
      "elmType": "div",
      "attributes": {
        "class": "=if(([$Stage] == '5. Post Subm Activities' || [$Stage] == '6. Closed'),'',if((([$Status]=='Capture'||[$Status]=='Live'||[$Status]=='Temporary Hold')&&[$SubmissionDate]),if([$SubmissionDate]<Date(@now,toDateString()),'sp-css-backgroundColor-BgCoral sp-css-borderColor-CoralFont sp-css-color-CoralFont',if([$SubmissionDate]>Date(@now,toDateString())&&[$SubmissionDate]<=Date((@now+ 7 * 24*60*60*1000),toDateString()),'sp-css-backgroundColor-BgPeach sp-css-borderColor-PeachFont sp-css-color-PeachFont',if([$SubmissionDate]>Date((@now+ 7 * 24*60*60*1000),toDateString())&&[$SubmissionDate]<=Date((@now+ 30 * 24*60*60*1000),toDateString()),'sp-css-backgroundColor-BgGold sp-css-color-GoldFont',if([$SubmissionDate]>Date((@now+ 30 * 24*60*60*1000),toDateString())&&[$SubmissionDate]<=Date((@now+ 90 * 24*60*60*1000),toDateString()),'sp-css-backgroundColor-BgMintGreen sp-css-borderColor-MintGreenFont sp-css-color-MintGreenFont',if([$SubmissionDate]>Date((@now+ 90 * 24*60*60*1000),toDateString())&&[$SubmissionDate]<=Date((@now+ 180 * 24*60*60*1000),toDateString()),'sp-css-backgroundColor-BgCyan sp-css-color-CyanFont','sp-css-backgroundColor-BgLightGray sp-css-borderColor-LightGrayFont sp-css-color-LightGrayFont'))))),'sp-css-backgroundColor-BgLightGray sp-css-borderColor-LightGrayFont sp-css-color-LightGrayFont'))"
      },
      "txtContent": "[$SubmissionDate.displayValue]"
    }
    

    For DaysSinceSubmitted column. I don't quite understand the logic and formatting requirements of this column, could you please provide more details.


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.