Forum Discussion

kevingeorget's avatar
kevingeorget
Brass Contributor
Oct 24, 2019
Solved

JSON formatting a list view in SharePoint

Hi SharePoint Users,
I need to format a Status column (Single line of text field) based on a Date- Time column for a SP List view. 
The logic-
1. If [DueDate]<Today, then color is Bright Red
2. If [DueDate]=Today, then color is Light red
3. IF [DueDate]>Today, then color is black
I tried the simple logic as below, but since both @currentfield and @now takes the date-time value, i'm not getting the desired results.

 

{
"$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
"elmType": "div",
"txtContent": "@currentField",
"style": {
"color": "=if([$DueDate] <@now '#ff0000',if([$DueDate] ==@now,'#e89b9b',''))"
}
}
Please help.

  • After some research, I've found a solution.

    We can user the toLocaleDateString() to extract just the date.

    so my expression would be-


    {
    "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
    "elmType": "div",
    "txtContent": "@currentField",
    "style": {
    "color": "=if(toLocaleDateString[$DueDate]<toLocaleDateString(@now), '#ff0000',if(toLocaleDateString[$DueDate]==toLocaleDateString(@now),'#ffcb0d',''))"
    }
    }

1 Reply

  • kevingeorget's avatar
    kevingeorget
    Brass Contributor

    After some research, I've found a solution.

    We can user the toLocaleDateString() to extract just the date.

    so my expression would be-


    {
    "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
    "elmType": "div",
    "txtContent": "@currentField",
    "style": {
    "color": "=if(toLocaleDateString[$DueDate]<toLocaleDateString(@now), '#ff0000',if(toLocaleDateString[$DueDate]==toLocaleDateString(@now),'#ffcb0d',''))"
    }
    }

Resources