Forum Discussion

StephDH246's avatar
StephDH246
Copper Contributor
Dec 15, 2022
Solved

How to conditionally format a date column that is two weeks old?

Hi Sharepoint boffins,   I am trying to work how how to get a JSON conditional formatting code to work that will highlight a date cell that is two weeks' old and based on another cell being empty. ...
  • SvenSieverding's avatar
    Dec 15, 2022

    StephDH246 

    If you want to get the number of milliseconds since 01.01.1970, you need the expression

    Number(Date([$Created]))

    So if you want to select all Dates before two weeks ago you could use the formula

    Number(Date([$Created]))<(Number(Date(@now))-1209600000

     (1209600000 = 14 * 24 * 60 *60 *1000).

    You can access other fields in the view. If you have a field "CellToBeEmpty" and that field is in the view, then you can access the value of the field like this : [$CellToBeEmpty]


    With two nested "if" statements, your json would look like this

    {
      "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
      "elmType": "div",
      "style": {
        "box-sizing": "border-box",
        "padding": "0 2px",
        "overflow": "hidden",
        "text-overflow": "ellipsis"
      },
      "attributes": {
        "class": "=if([$CellToBeEmpty],'',if(Number(Date([$Created]))<(Number(Date(@now))-1209600000),'sp-css-backgroundColor-BgRed sp-css-borderColor-CoralFont sp-field-fontSizeSmall sp-css-color-WhiteText',''))"
      },
      "txtContent": "[$Created.displayValue]"
    }




Resources