SOLVED

JSON formatting a list view in SharePoint

%3CLINGO-SUB%20id%3D%22lingo-sub-941854%22%20slang%3D%22en-US%22%3EJSON%20formatting%20a%20list%20view%20in%20SharePoint%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-941854%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20SharePoint%20Users%2C%3CBR%20%2F%3EI%20need%20to%20format%20a%20Status%20column%20(Single%20line%20of%20text%20field)%20based%20on%20a%20Date-%20Time%20column%20for%20a%20SP%20List%20view.%26nbsp%3B%3CBR%20%2F%3EThe%20logic-%3CBR%20%2F%3E1.%20If%20%5BDueDate%5D%3CTODAY%3E%3C%2FTODAY%3E2.%20If%20%5BDueDate%5D%3DToday%2C%20then%20color%20is%20Light%20red%3CBR%20%2F%3E3.%20IF%20%5BDueDate%5D%26gt%3BToday%2C%20then%20color%20is%20black%3CBR%20%2F%3EI%20tried%20the%20simple%20logic%20as%20below%2C%20but%20since%20both%20%40currentfield%20and%20%40now%20takes%20the%20date-time%20value%2C%20i'm%20not%20getting%20the%20desired%20results.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%7B%3CBR%20%2F%3E%22%24schema%22%3A%20%22%3CA%20href%3D%22https%3A%2F%2Fdeveloper.microsoft.com%2Fjson-schemas%2Fsp%2Fv2%2Fcolumn-formatting.schema.json%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdeveloper.microsoft.com%2Fjson-schemas%2Fsp%2Fv2%2Fcolumn-formatting.schema.json%3C%2FA%3E%22%2C%3CBR%20%2F%3E%22elmType%22%3A%20%22div%22%2C%3CBR%20%2F%3E%22txtContent%22%3A%20%22%40currentField%22%2C%3CBR%20%2F%3E%22style%22%3A%20%7B%3CBR%20%2F%3E%22color%22%3A%20%22%3Dif(%5B%24DueDate%5D%20%26lt%3B%40now%20'%23ff0000'%2Cif(%5B%24DueDate%5D%20%3D%3D%40now%2C'%23e89b9b'%2C''))%22%3CBR%20%2F%3E%7D%3CBR%20%2F%3E%7D%3CBR%20%2F%3EPlease%20help.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-947251%22%20slang%3D%22en-US%22%3ERe%3A%20JSON%20formatting%20a%20list%20view%20in%20SharePoint%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-947251%22%20slang%3D%22en-US%22%3E%3CP%3EAfter%20some%20research%2C%20I've%20found%20a%20solution.%3C%2FP%3E%3CP%3EWe%20can%20user%20the%20%3CSTRONG%3EtoLocaleDateString()%20%3C%2FSTRONG%3Eto%20extract%20just%20the%20date.%3C%2FP%3E%3CP%3Eso%20my%20expression%20would%20be-%3C%2FP%3E%3CP%3E%3CBR%20%2F%3E%3CEM%3E%7B%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3E%22%24schema%22%3A%20%22%3CA%20href%3D%22https%3A%2F%2Fdeveloper.microsoft.com%2Fjson-schemas%2Fsp%2Fv2%2Fcolumn-formatting.schema.json%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehttps%3A%2F%2Fdeveloper.microsoft.com%2Fjson-schemas%2Fsp%2Fv2%2Fcolumn-formatting.schema.json%3C%2FA%3E%22%2C%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3E%22elmType%22%3A%20%22div%22%2C%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3E%22txtContent%22%3A%20%22%40currentField%22%2C%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3E%22style%22%3A%20%7B%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3E%22color%22%3A%20%22%3Dif(%3CFONT%20color%3D%22%230000FF%22%3EtoLocaleDateString%5B%24DueDate%5D%3CTOLOCALEDATESTRING%3E%2C%20'%23ff0000'%2Cif(%3CFONT%20color%3D%22%230000FF%22%3EtoLocaleDateString%5B%24DueDate%5D%3D%3DtoLocaleDateString(%40now)%3C%2FFONT%3E%2C'%23ffcb0d'%2C''))%22%3C%2FTOLOCALEDATESTRING%3E%3C%2FFONT%3E%3C%2FEM%3E%3CFONT%3E%3CBR%20%2F%3E%3CEM%3E%7D%3C%2FEM%3E%3CBR%20%2F%3E%3CEM%3E%7D%3C%2FEM%3E%3C%2FFONT%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E
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 (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',''))"
}
}