How to convert only the date without time to a string in Pascal

1 Answer

0 votes
program DateToStringProgram;

{$mode objfpc}

uses
  SysUtils;

(*
    Function: DateToString
    Purpose : Convert a TDateTime value to a string (YYYY-MM-DD)
*)
function DateToString(const dt: TDateTime): string;
begin
  Result := FormatDateTime('yyyy-mm-dd', dt);  // Only date
end;

(*
    Function: MakeDate
    Purpose : Build a TDateTime from integers (year, month, day)
*)
function MakeDate(y, m, d: Word): TDateTime;
begin
  Result := EncodeDate(y, m, d);
end;

var
  today: TDateTime;
  hardcoded: TDateTime;

begin
  // 1. Today's date
  today := Date;
  WriteLn('Today''s date is: ', DateToString(today));

  // 2. Hard-coded date
  hardcoded := MakeDate(2025, 12, 7);
  WriteLn('Hard-coded date is: ', DateToString(hardcoded));
end.



(* 
run:

Today's date is: 2026-05-30
Hard-coded date is: 2025-12-07

*)

 



answered 9 hours ago by avibootz

Related questions

...