Hi,
I am trying to calculate working days in my application, where weekends (Saturday and Sunday) should be excluded. Additionally, if the user selects half-days, those should also be deducted from the total.
How can I implement this in my application?
Hi @ATUL TIWARI ,
Unfortunately, your OML includes a core module that we don’t have access to (LeaveApplication Core). Because of that, we can’t modify it, as it currently has errors on our side.
Also, could you clarify what period you want to use for calculating the working days? And are these the actual working days or days deducted?
I am developing a form where users can select a start date and an end date. Based on this date range, I need to calculate the total number of working days, excluding Saturdays and Sundays, regardless of the month.
After calculating the total working days, I also need to check if the “Half Day” option is selected. If it is true, then 0.5 day should be deducted from the total count.
Finally, the calculated total number of days should be stored in the database when the form is submitted.
Like Siya mentioned, the approach is basically a classic while StartDate <= EndDate, where you calculate the total inside the loop. In OS, you simulate this using an If node.
The flow is:
I created this logic in a client action called CalculateTotalDays, and I added in your logic flow for RequestOnClick.
@ATUL TIWARI : Use an ad hoc loop to iterate from the start date to the end date. For each date, check the result of DayOfWeek(). If it returns 0 (Sunday) or 6 (Saturday), skip that date; otherwise, include it in the total.
@ATUL TIWARI : Thanks @Mihai Melencu for the implementation. btw logic for half-days should be relookedinto. To me it looks like if half-days should be for all days not just one day i.e if some one applied leave for a whole but marked as half days then the total should be 2.5 instead of 4.5 days. @ATUL TIWARI : Please confirm this.
Although looping works, and will be fast enough for any modern computer, it can be done smarter. Since weekends are predictable, it's possible to calculate the number of weekend days within a certain period once you know the first day of that period. I'll leave it as an exercise to the reader to come up with the exact formula :).
You can calculate working days by looping from start date to end date and counting only weekdays (excluding Saturday and Sunday using WeekDay()). Then subtract 0.5 for each half-day selected (start/end). This approach is simple, reliable, and handles edge cases like weekends and partial days correctly.
This answer has been given already 3 days ago, and you used AI to generate the answer. Please firsat read what anwers have been given before answering yourself, and don't post if the answer was already posted. (And ideally also don't use AI!)
Killan yes i didn't read the all previous answer, I will check from next time but its was not AI generated its something which is implemented in my POC App.
I just used AI to rephrase answer wording.
Its good that you are evaluating closely. But requesting you to don't mark used as AI without confirming.Thank you !
Hi Shreyans,
I removed the AI tag, but note that "rephrasing with AI" often leads to answers indistinguishable from an AI answer. Especially the last sentence, "This approach is simple, reliable, and handles edge cases like weekends and partial days correctly" is so typical of a ChatGPT or other LLM response, that it's hard to believe it's just a "rephrase". So you might want to be careful letting an LLM redirect your words.
It's better to speak somewhat incorrect English, than to be taken for AI!
Yup, Thanks and ok sure i will try that to be natural and please ensure before marking AI or anything because it leads to downs votes also that's why btw No issue with so, and ok thanks for advice Thank you !
Hi,I'm also working on a small scheduling app, and I'd like to ask if there's a better or faster way than using a loop.
For example, I'm calculating the number of working days for a long-serving employee.
Computers are nowadays more than fast enough to calculate it using a loop within a fraction of a second, even if it's years and years you want to calculate. You shouldn't optimize before you know it's needed!
Agree with @Kilian Hekhuis. However if some one is really need a formulae here it is
weekdays = DecimalToInteger((DiffDays(StartDate, EndDate) + 1) / 7) * 5 + Max(0,Min(5, DayOfWeek(StartDate) + Mod(DiffDays(StartDate, EndDate) + 1, 7)) - Min(5, DayOfWeek(StartDate)))
weekdays = full weeks (×5) + weekdays from remaining days.
Now that's what I call a formula :D.
Hi @ATUL TIWARI
For the difference workdays between two dates you can try the forge
https://www.outsystems.com/forge/component-overview/16516/dateplus
Best regards