Forum Discussion
Lel_999987
Sep 07, 2023Copper Contributor
Get current date in SharePoint
Hi all, how do I get current date which is updated dynamically when a new day starts? E.g I have a date column and a status column. I want to set the status to "not due", "overdue" or "due" de...
NikolinoDE
Sep 07, 2023Gold Contributor
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.
pjallex
Oct 01, 2024Copper Contributor
Hi NikolinoDE, I've been trying to alter the JavaScript code with no success 🙂
I need today to be 'Due Soon' and today +1 and today +2 to also be 'Due Soon'. I also need today +3 and more to be 'Not Due'. Can you please show me how this JavaScript code will look like?
Thank you.
- pjallexOct 02, 2024Copper ContributorAn update., The script is supposed to give "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."
In my Sharepoint list everything from today and in the furture is set to Not Due in green. It doesn't distinct that today + 2 days should be Due Soon and orange.
So does something need to be changed in the script or in my Sharepoint settings?- pjallexOct 02, 2024Copper ContributorCorrection #2. Everything from tomorrow is set to Not Due in green. Today and all days before today is set to Overdue in red.
- Rob_ElliottOct 03, 2024Bronze Contributor
pjallex this all needs to be done with JSON in your column formatting. JSON stands for Javascript Object Notation. Just calling it Javascript as an earlier poster did is not correct, but then I suspect the answer was looked up on ChatGPT.
Rob
Los Gallardos
Microsoft Power Automate Community Super User.
Principal Consultant, SharePoint and Power Platform WSP Global (and classic 1967 Morris Traveller driver)