JSON on date field in Sharepoint List

Copper Contributor

Hello

Is anyone able to help me with the JSON code to format a date field in Sharepoint?

 

I want to format a certain column with dates in as follows:

 

If the date is equal to todays date or any date prior (up to 7 days) then turn the background of the date field red.

 

If the date field is 8 -14 days prior to todays date then turn the background orange. 

 

If the date field is 15+ days prior to todays date then turn the background green.

 

Thanks

2 Replies

@Flaminia This should get you what you are looking for

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
  "elmType": "div",
  "txtContent": "@currentField",
  "style": {
    "color": "white",
    "background-color": "=if(toLocaleDateString(@currentField+1296000000) <= toLocaleDateString(@now), 'green', if(toLocaleDateString(@currentField+604800000) <= toLocaleDateString(@now) , 'orange', 'red'))"
  }
}

You can learn more from here: https://docs.microsoft.com/en-us/sharepoint/dev/declarative-customization/column-formatting#apply-fo...

 

Good Luck!

 

Please click Mark as Best Response & Like if my post helped you to answer or resolve your issue. This will help others to find the correct solution easily. It also closes the item. If the post was useful in other ways, please consider giving it Like.

Thank you so much @Don Kirkham 

 

I think that's almost there. I gave it a go but it seems to have colour coded the dates quite randomly!

Flaminia_0-1646743126288.png