Solved

I need a function that could return me the day worked.

Posted on 2008-06-11
2
156 Views
Last Modified: 2010-04-21
Hello guys

I need a function that could return me a quantity of days without weekend, since my first day of the month.

Thanks
0
Comment
Question by:hidrau
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 26

Accepted Solution

by:
Russell Libby earned 500 total points
ID: 21763859
Give this a try, I believe it is pretty well tested. I wasn't sure if you were counting today in the result, or not (eg, if you consider today-yesterday = 2 days, or 1 day), so the function allows you to specify that.

As to the function, it determines how many weeks (full 7 days) have passed. That part is easy, because 7 days = 5. The remainer needs to be figured out based on day of the week that that the StartDate falls. (determine if the day count wraps into a weekend).

Regards,
Russell

----


function DaysWorkedSince(StartDate: TDateTime; IncludeToday: Boolean = False): Integer;
var  dwDay:         Integer;
     dwTotal:       Integer;
     dwWeeks:       Integer;
     dwDays:        Integer;
begin

  // Get the day of week for the starting day (0 = Sunday and 6 = Saturday)
  dwDay:=DateTimeToTimeStamp(StartDate).Date mod 7;

  // Subtract today and start date
  dwTotal:=Trunc(Now - StartDate);

  // Update by 1 if today should be included
  if IncludeToday then Inc(dwTotal);

  // Calculate the number of total weeks and remainder days
  dwWeeks:=dwTotal div 7;
  dwDays:=dwTotal mod 7;

  // Weeks = 5 working days
  result:=dwWeeks * 5;

  // Check remainder days
  if (dwDays > 0) then
  begin
     // Now figure in the days based on the starting date
     case dwDay of
        0  :  Dec(dwDays);
        1  :  if (dwDays > 5) then dwDays:=5;
        2  :  if (dwDays > 4) then dwDays:=4;
        3  :
        begin
           if (dwDays = 6) then
              dwDays:=4
           else if (dwDays > 3) then
              dwDays:=3;
        end;
        4  :
        begin
           if (dwDays > 3) then
              Dec(dwDays, 2)
           else if (dwDays = 3) then
              dwDays:=2;
        end;
        5  :
        begin
           if (dwDays > 2) then
              Dec(dwDays, 2)
           else if (dwDays = 2) then
              dwDays:=1;
        end;
        6  :
        begin
           if (dwDays > 2) then
              Dec(dwDays, 2)
           else
              dwDays:=0;
        end;
     end;
     // Update result
     Inc(result, dwDays);
  end;

end;

  ShowMessage(IntToStr(DaysWorkedSince(StrToDate('5/1/2008'), True)));
0
 
LVL 1

Author Closing Comment

by:hidrau
ID: 31466282
thanks

I tested and it is working.
If I have any problem I'll let you know
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question