1

I want to format a column if there is a date in the column, otherwise leave it blank.

This is the code:

{ "$schema": "https://developer.microsoft.com/json-schemas/sp/column-formatting.schema.json", "debugMode": true, "elmType": "div", "attributes": { "class": "=if(@currentField == '','','sp-field-severity--good')" }, "children": [ { "elmType": "span", "style": { "display": "inline-block", "padding": "0 4px" } }, { "elmType": "span", "txtContent": "@currentField" } ] }

No matter if there is a date or not, the field is highlighted. I don't want the highlight if blank. What am I doing wrong?

enter image description here

0

You can change your expression to look at the displayValue portion of the date for a better way to check for blanks:

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/column-formatting.schema.json",
  "debugMode": true,
  "elmType": "div",
  "attributes": {
    "class": "=if(@currentField.displayValue == '','','sp-field-severity--good')"
  },
  "children": [
    {
      "elmType": "span",
      "style": {
        "display": "inline-block",
        "padding": "0 4px"
      }
    },
    {
      "elmType": "span",
      "txtContent": "@currentField"
    }
  ]
}
0

You can use the below JSON code to format column.

{
    "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
    "elmType": "div",
    "txtContent": "=if(@currentField, @currentField, 'Null')",
    "attributes": {
        "class": "=if(@currentField, 'sp-field-severity--good', '')"
    }
}

enter image description here

========================= Updated Answer ========================

Please use the below JSON:

{
    "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
    "elmType": "div",
    "txtContent": "=if(@currentField, toDateString(@currentField), 'Null')",
    "attributes": {
        "class": "=if(@currentField, 'sp-field-severity--good', '')"
    }
}

enter image description here

2
  • Thank you, this worked! But...how do I show just the date and not the time?
    – llrinct
    Aug 23 at 11:58
  • Hi llrinct. I have updated answer under initial My Answer. Hope this can help you to solve issue. Have a nice day! Aug 24 at 10:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.