SOLVED

Column formatting & dates

Copper Contributor

Can anyone support in this little piece of column formatting?

 

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
  "elmType": "div",
  "txtContent": "=toLocaleDateString(Date((getMonth([$DateOfBirth])+1) + '/' + getDate([$DateOfBirth]) + '/' + getYear(@now))) + ' | ' + toLocaleDateString(@now)",
  "style": {
    "color": "=if(toLocaleDateString(Date((getMonth([$DateOfBirth])+1) + '/' + getDate([$DateOfBirth]) + '/' + getYear(@now))) < toLocaleDateString(@now), 'red', 'green')"
  }
}

 

The idea is to show a column with the birthday of this year and for development purposes, the current date is shown next to it. $DateOfBirth is a date field.

 

The problem in this code, is the if-statement for the color of the text. All dates are shown in the same color. Even though two of them should show a different color.

R_o_n__0-1709931707935.png

(dates are European)

2 Replies
best response confirmed by R_o_n_ (Copper Contributor)
Solution

@R_o_n_ Try using JSON like: 

 

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
  "elmType": "div",
  "txtContent": "=toLocaleDateString(Date((getMonth([$DateOfBirth])+1) + '/' + getDate([$DateOfBirth]) + '/' + getYear(@now))) + ' | ' + toLocaleDateString(@now)",
  "style": {
    "color": "=if(Date((getMonth([$DateOfBirth])+1) + '/' + getDate([$DateOfBirth]) + '/' + getYear(@now)) < @now, 'red', 'green')"
  }
}

 

Outputdate-format.JPG


Please click Mark as Best Response & Like if my post helped you to solve your issue. This will help others to find the correct solution easily. It also closes the item. If the post was useful in other ways, please consider giving it Like.

Perfect! A small difference can have a big impact! Thank you very much!
1 best response

Accepted Solutions
best response confirmed by R_o_n_ (Copper Contributor)
Solution

@R_o_n_ Try using JSON like: 

 

{
  "$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
  "elmType": "div",
  "txtContent": "=toLocaleDateString(Date((getMonth([$DateOfBirth])+1) + '/' + getDate([$DateOfBirth]) + '/' + getYear(@now))) + ' | ' + toLocaleDateString(@now)",
  "style": {
    "color": "=if(Date((getMonth([$DateOfBirth])+1) + '/' + getDate([$DateOfBirth]) + '/' + getYear(@now)) < @now, 'red', 'green')"
  }
}

 

Outputdate-format.JPG


Please click Mark as Best Response & Like if my post helped you to solve your issue. This will help others to find the correct solution easily. It also closes the item. If the post was useful in other ways, please consider giving it Like.

View solution in original post