How to add N weeks to the current date in Pascal

1 Answer

0 votes
program AddWeeksProgram;

{$mode objfpc}{$H+}

uses
  SysUtils;

{**
 * Adds a specified number of weeks to the current date and time.
 *
 * @param Weeks The number of weeks to add (supports negative values to subtract).
 * @return TDateTime The resulting future date.
 *}
function AddWeeksToCurrentDate(Weeks: Integer): TDateTime;
begin
  { 
    TDateTime treats 1.0 as a single day. 
    To add N weeks, we add (Weeks * 7) days to the current date/time (Now).
  }
  Result := Now + (Weeks * 7);
end;

var
  WeeksToAdd: Integer;
  FutureDate: TDateTime;
  FormattedDate: String;
  FormattedDateTime: String;

begin
  WeeksToAdd := 4;

  { Calculate the future date/time }
  FutureDate := AddWeeksToCurrentDate(WeeksToAdd);

  { Format the resulting TDateTime into standard human-readable strings }
  FormattedDate := FormatDateTime('YYYY-MM-DD', FutureDate);
  FormattedDateTime := FormatDateTime('YYYY-MM-DD HH:NN:SS', FutureDate);

  { Output the results }
  Writeln('Adding ', WeeksToAdd, ' weeks to the current date:');
  Writeln('Date only     : ', FormattedDate);
  Writeln('Date and Time : ', FormattedDateTime);
end.


(*
run:

Adding 4 weeks to the current date:
Date only     : 2026-12-05
Date and Time : 2026-12-05 16:15:22

*)

 



answered 1 day ago by avibootz
...