SOLVED

JSON formatting a list view in SharePoint

Brass Contributor

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.

1 Reply
best response confirmed by kevingeorget (Brass Contributor)
Solution

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 best response

Accepted Solutions
best response confirmed by kevingeorget (Brass Contributor)
Solution

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',''))"
}
}

View solution in original post