[JSON] Date formatting Sharepoint

Franjuju Posted messages 12 Status Member -  
Franjuju Posted messages 12 Status Member -
Bonjour à tous,

I've been searching for several days and I find few tutorials regarding my problem. Indeed, I have a SharePoint list with an expiration date for each of my rows. My goal is to highlight the row in orange if the expiration date is within 90 days from today, in red if the expiration date is after today, and in green for the rest (if the expiration date is more than 90 days from today). I know I need to modify this formula (below) provided by default for the column, but I can't manage to do it at all.


{
"elmType": "div",
"style": {
"padding": "0 4px"
},
"attributes": {
"class": {
"operator": ":",
"operands": [
{
"operator": "==",
"operands": [
"@currentField",
""
]
},
"",
{
"operator": ":",
"operands": [
{
"operator": "<",
"operands": [
{
"operator": "Date()",
"operands": [
{
"operator": "toDateString()",
"operands": [
"@currentField"
]
}
]
},
{
"operator": "Date()",
"operands": [
{
"operator": "toDateString()",
"operands": [
"@now"
]
}
]
}
]
},
"sp-css-backgroundColor-warningBackground",
{
"operator": ":",
"operands": [
{
"operator": "==",
"operands": [
{
"operator": "Date()",
"operands": [
{
"operator": "toDateString()",
"operands": [
"@currentField"
]
}
]
},
{
"operator": "Date()",
"operands": [
{
"operator": "toDateString()",
"operands": [
"@now"
]
}
]
}
]
},
"sp-css-backgroundColor-successBackground",
{
"operator": ":",
"operands": [
{
"operator": ">",
"operands": [
{
"operator": "Date()",
"operands": [
{
"operator": "toDateString()",
"operands": [
"@currentField"
]
}
]
},
{
"operator": "Date()",
"operands": [
{
"operator": "toDateString()",
"operands": [
"@now"
]
}
]
}
]
},
"sp-css-backgroundColor-errorBackground",
""
]
}
]
}
]
}
]
}
},
"txtContent": "@currentField"
}


Thank you in advance for any help you can provide! :)

1 answer

  1. zucrezel
     
    Hello,

    An example found at https://docs.microsoft.com/en-us/sharepoint/dev/declarative-customization/column-formatting

    To compare a date/time field value to another date constant, use the Date() method to convert a string into a date. The following example colors the active field red if the DueDate is before 03/22/2017.

    { "$schema": "https://developer.microsoft.com/json-schemas/sp/column-formatting.schema.json", "elmType": "div", "txtContent": "@currentField", "style": { "color": { "operator": "?", "operands": [ { "operator": "<=", "operands": [ "[$DueDate]", { "operator": "Date()", "operands": [ "3/22/2017" ] } ] }, "#ff0000", "" ] } } }
    0
    1. Franjuju Posted messages 12 Status Member
       
      I already tried with this formula, trying to adapt it to my case but I didn't succeed and it doesn't cover exactly everything I want to do... Do you know how to adapt it to my case? When I try to set the correct columns and do according to today's date, it erases my column

      Thank you for your help.
      0