Link to home
Start Free TrialLog in
Avatar of Khan Consultoria
Khan ConsultoriaFlag for Brazil

asked on

Place a date in possible days

Hello guys,

I need help to create a function, I am trying it, but I confess that I am lost.

The function works with date, then imagine you have a formatted date like 20/12/2019  (dd/mm/yyyy)

I want to add 40 days on it or any days ahead (30, 70, 84, etc days), until here it is fine for me and I can do that, very easy.

Now, this part is my problem, I have a list of possible day that my date must be, for example 3,6 ( Tuesday and Friday), if my date
after added 40 days ahead gets on Saturday or Sunday or Monday, it must be placed on Tuesday.
If my date is on 4, 5 ( Wednesday or Thursday) it must be placed in Friday;

The rule of my string possibles days can be change, today I work with 3,5 but in the future, it must be in 2,7.

I thank a lot any help
Alex
Avatar of ste5an
ste5an
Flag of Germany image

First of all: Visualization (formatting) has nothing to do with the data type and thus the problem.

Then: What is your used language?

But for your problem: Almost any system has a dateadd method. Thus add your number of days. Then you need to test the week day, depending on that, you need to add the difference to the next Tuesday or Friday.
Avatar of Khan Consultoria

ASKER

My language is Portuguese and the formatted date of my country is dd/mm/yyyy
*cough*

I was talking about your used programming language.. Delphi and Java are not the same.

And for a function doing date calculus, formatting does not matter. Cause such a function will solely operate on a date data type. And data type and formatting (visualization) are two different things.
ste5an,

I did that, but I haven't reached the result with all possibilities, take a look at and maybe you can give me a clue on my function:


ValidDays = '3,5'

function TForm1.DateDayWeekAfterxDays(xdate: TdateTime; xDaysAhead: Integer; ValidDays: string): TdateTime;
Var
   i, dayD, day1, day2, daysAdd : Integer;
  dt: TdateTime;
  ListDias : TStringList;
begin
  dt   := xdate + xDaysAhead;
  dayD := DayOfWeek( dt );
  day1 := StrToInt( Copy(ValidDays, 1,1) );
  day2 := StrToInt( Copy(ValidDays, 3,1) );

  sDateEdit2.date := dt;

  if (dayD <= day1) then
  Begin
    daysAdd := abs( dayD - day1 );
    Result  := dt + daysAdd;
    Exit
  End;

  if (dayD > day1) and (dayD <= day2) then
  Begin
    daysAdd := abs( day1 - day2 );
    Result  := dt + daysAdd;
    Exit
  End;

  if (dayD > day1) and (dayD > day2) then
  Begin
    daysAdd := abs( dayD - day1 ) + abs( dayD - day2 );
    Result  := dt + daysAdd;
    Exit
  End;

end;

Open in new window

I am using Delphi language, but an example in Java is welcome too.
E.g.

program Project1;

{$APPTYPE CONSOLE}

uses
  SysUtils;

function AddDays(const CDate: TDateTime; const CDaysToAdd: Integer; const CValidDays: array of Integer): TDateTime;
var
  count: Integer;
  isValidDay: Boolean;
begin
  Result := CDate + CDaysToAdd;
  isValidDay := False;
  for Count := Low(CValidDays) to High(CValidDays) do
    isValidDay := (CValidDays[Count] = DayOfWeek(Result));

  if not IsValidDay then
    for Count := Low(CValidDays) to High(CValidDays) do
      if (CValidDays[Count] > DayOfWeek(Result)) then
      begin
        Result := Result - DayOfWeek(Result) + CValidDays[Count];
        break;
      end;
end;

var
  date: TDateTime;
begin
  date := AddDays(Now(), 13, [3, 6]);
  WriteLn(FormatDateTime('yyyy-mm-dd', date));
  WriteLn('Done.');
  ReadLn;
end.

Open in new window

Hi, Ste5an

I tried your example, but it didn't work fine:

date := AddDays( StrToDate('22/12/2019' , 40, [3, 5]); the result was: 31/01/2019  and 31 is Friday and not Thursday.

I tried with other dates too, some works and some not.
ASKER CERTIFIED SOLUTION
Avatar of ste5an
ste5an
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks a lot for your help, your example gave me a direction