Forum Discussion
Get current date in SharePoint
To achieve a dynamic "not due," "overdue," or "due" status based on the current date in SharePoint Online without using Power Automate, you can use JavaScript in SharePoint's column formatting feature. Here is a step-by-step guide:
- Create a Choice Column for Status:
- Create a new choice column (e.g., "Status") in your SharePoint list where you want to track the statuses.
- Add Date Column:
- Ensure you have a date column (e.g., "Due Date") in the list that contains the date to which you want to compare the current date.
- Update Column Formatting:
- Go to your SharePoint list.
- Click on the gear icon (settings) in the upper-right corner.
- Choose "List settings."
- Under "Columns," click on the name of the "Status" column.
- Edit Column Formatting:
- Scroll down to the "Column Formatting" section.
- Click on the "Edit" button.
- Enter JavaScript Code:
- In the "Format column" pane, you can enter JavaScript code to dynamically set the "Status" column value based on the "Due Date" column and the current date. Here is a sample code snippet:
{
"$schema": "https://developer.microsoft.com/json-schemas/sp/v2/column-formatting.schema.json",
"elmType": "div",
"children": [
{
"elmType": "span",
"style": {
"color": "=if([$DueDate] <= @now, 'red', if([$DueDate] < @now + 3, 'orange', 'green'))"
},
"txtContent": "=if([$DueDate] <= @now, 'Overdue', if([$DueDate] < @now + 3, 'Due Soon', 'Not Due'))"
}
]
}This code checks if the "Due Date" is less than or equal to the current date (@now). If so, it sets the text to "Overdue" and the color to red. If the "Due Date" is within the next 3 days, it sets the text to "Due Soon" and the color to orange. Otherwise, it sets the text to "Not Due" and the color to green.
- Save and Apply Formatting:
- Click the "Save" button.
- Click the "OK" button to apply the column formatting.
Now, when you view your SharePoint list, the "Status" column will dynamically update based on the "Due Date" column and the current date. Please note that this approach relies on client-side rendering, so it will work for views in SharePoint Online. However, users may need to refresh the page to see the status update as the date changes.The text and steps were created with the help of AI.
My answers are voluntary and without guarantee!
Hope this will help you.
Was the answer useful? Mark them as helpful and like it!
This will help all forum participants.
thanks for your fast reply.
I forget to mention, that the status column shoud be able to filter the results and I guess your approach will not provide filter functionality, right?