3

So I got this code, it makes the background green when it has passed the set taskduedate. But I want it to show red background when it has passed 2 hours after taskduedate, it that possible?

{
      "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
      "elmType": "div",
      "debugMode": true,
      "txtContent": "@currentField",
      "attributes": {
        "class": "=if(Number(@currentField) != 0, if([$TaskDueDate] <= @now, 'sp-css-backgroundColor-successBackground50', 'sp-css-backgroundColor-blockingBackground50'), '')"
      }
    }

All with red circles should be red when the clock is 19:00

enter image description here

2

Give this a try. It will made the background green if the due date hasn't passed yet and will make it normal for up to 2 hours after it has passed, then red after 2 hours after the due date.

    {
      "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
      "elmType": "div",
      "debugMode": true,
      "txtContent": "@currentField",
      "attributes": {
        "class": "=if(Number(@currentField) != 0, if([$TaskDueDate] <= @now, 'sp-css-backgroundColor-successBackground50', if(Number(@now) >= (Number([$TaskDueDate])+1000*60*60*2),'sp-css-backgroundColor-blockingBackground50', '')), '')"
      }
    }
| improve this answer | |
  • This wont work, it makes everything green even if the time in the column has passed 2 hours. So I got a column that says "2020-04-09 15:23" and the time right now is "2020-04-09 19:05 " then the column should be red because it was over 2 hours ago. Or else it should be green. I appreciate yout help! – 288088 Apr 9 at 17:06
  • Is the timezone of the site collection match that of the client (you)? The value of [$TaskDueDate] without formatting will reflect the site collection's timezone. However, it will be converted to the client's timezone in formatting as will @now. This can make things pretty confusing. – theChrisKent Apr 9 at 19:00
  • Yes, it works with other formats. – 288088 Apr 9 at 19:06
  • I've edited my question with a picture – 288088 Apr 9 at 19:59
0

You could try the below json code:

{
      "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
      "elmType": "div",
      "debugMode": true,
      "txtContent": "@currentField",
      "attributes": {
        "class": "=if(Number(@currentField) != 0, if([$TaskDueDate] <= @now + 7200000, 'sp-css-backgroundColor-blockingBackground50', if([$TaskDueDate] <= @now,'sp-css-backgroundColor-successBackground50','')), '')"
      }
 }
| improve this answer | |

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.