Place a date in possible days

Khan Consultoria
Khan Consultoria used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
ste5anSenior Developer

Commented:
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.

Author

Commented:
My language is Portuguese and the formatted date of my country is dd/mm/yyyy
ste5anSenior Developer

Commented:
*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.

Author

Commented:
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

Author

Commented:
I am using Delphi language, but an example in Java is welcome too.
ste5anSenior Developer

Commented:
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

Author

Commented:
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.
Senior Developer
Commented:
Just debug it, I've missed the week transition:

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];
        exit;
      end;

  if not IsValidDay then
    Result := Result - DayOfWeek(Result) + CValidDays[0] + 7;
end;

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

Open in new window


p.s. use the CODE button or [code][/code] tags to embed code into your posts. Edit them. This makes reading simpler. And use EncodeDate(), otherwise your samples depend on locales.

Author

Commented:
Thanks a lot for your help, your example gave me a direction

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial