R_o_n_
Mar 08, 2024Copper Contributor
Column formatting & dates
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.
(dates are European)
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')" } }
Output:
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.