Difference between revisions of "TDateTime"

From Lazarus wiki
Jump to navigationJump to search
Line 7: Line 7:
 
[[Unit]] SysUtils [[Function|function]] Now retrieves the current system date and time.
 
[[Unit]] SysUtils [[Function|function]] Now retrieves the current system date and time.
  
<source>
+
<syntaxhighlight lang="pascal">
 
  function Now : TDateTime;   
 
  function Now : TDateTime;   
</source>
+
</syntaxhighlight>
  
 
== Adding and subtracting TDateTime ==
 
== Adding and subtracting TDateTime ==
Line 15: Line 15:
 
Unit DateUtils function DaysBetween tell number of whole days between two DateTime values.
 
Unit DateUtils function DaysBetween tell number of whole days between two DateTime values.
  
<source>
+
<syntaxhighlight lang="pascal">
 
   function DaysBetween ( const ToDate, FromDate : TDateTime ) : Integer;
 
   function DaysBetween ( const ToDate, FromDate : TDateTime ) : Integer;
</source>
+
</syntaxhighlight>
  
 
Unit DateUtils function WeeksBetween tell number of whole weeks between two DateTime values.
 
Unit DateUtils function WeeksBetween tell number of whole weeks between two DateTime values.
  
<source>
+
<syntaxhighlight lang="pascal">
 
   function WeeksBetween( const ToDate, FromDate : TDateTime ):Integer;
 
   function WeeksBetween( const ToDate, FromDate : TDateTime ):Integer;
</source>
+
</syntaxhighlight>
  
  
  
<source>
+
<syntaxhighlight lang="pascal">
  
 
program DateProject1;
 
program DateProject1;
Line 47: Line 47:
 
end.  
 
end.  
 
 
</source>
+
</syntaxhighlight>
 
    
 
    
 
== Set Date ==
 
== Set Date ==
Line 53: Line 53:
 
Unit SysUtils function EncodeDate to set the date.
 
Unit SysUtils function EncodeDate to set the date.
  
<source>
+
<syntaxhighlight lang="pascal">
 
   function EncodeDate ( const Year, Month, Day : Word ) : TDateTime;
 
   function EncodeDate ( const Year, Month, Day : Word ) : TDateTime;
</source>
+
</syntaxhighlight>
  
 
== Compare two TDateTime ==
 
== Compare two TDateTime ==
  
  
<source>
+
<syntaxhighlight lang="pascal">
  
 
program CompareTwoDateTime;
 
program CompareTwoDateTime;
Line 79: Line 79:
 
   ReadLn;
 
   ReadLn;
 
end.   
 
end.   
</source>
+
</syntaxhighlight>
  
 
== See also ==
 
== See also ==

Revision as of 15:13, 8 July 2019

English (en) suomi (fi)

TDateTime is a combination of a date and a time. Attributes: year, month, day, hour, minute, second, and microsecond.

Get current time

Unit SysUtils function Now retrieves the current system date and time.

 function Now : TDateTime;

Adding and subtracting TDateTime

Unit DateUtils function DaysBetween tell number of whole days between two DateTime values.

  function DaysBetween ( const ToDate, FromDate : TDateTime ) : Integer;

Unit DateUtils function WeeksBetween tell number of whole weeks between two DateTime values.

  function WeeksBetween( const ToDate, FromDate : TDateTime ):Integer;


program DateProject1;

uses
  SysUtils,DateUtils;
const
  DateFormatChars = 'dd"/"mm"/"yyyy';
var
  DateTime1, DateTime2: TDateTime;
begin
  DateTime1 := now;
  DateTime2 := DateTime1 + 4*7;       //28 days later
  WriteLn('Current date is '+ FormatDateTime( DateFormatChars, DateTime1 ));
  WriteLn('28 days later date is '+ FormatDateTime( DateFormatChars, DateTime2 ));
  WriteLn('Number of days is '+ ( DaysBetween( DateTime1 ,DateTime2)).ToString );
  WriteLn('Number of weeks is '+ ( WeeksBetween( DateTime1 ,DateTime2)).ToString );
  ReadLn;
end.

Set Date

Unit SysUtils function EncodeDate to set the date.

  function EncodeDate ( const Year, Month, Day : Word ) : TDateTime;

Compare two TDateTime

program CompareTwoDateTime;

uses
  SysUtils, DateUtils;
var
   firstDate, secondDate: TDateTime;
begin
  firstDate := EncodeDate(2000, 2, 29);
  secondDate := EncodeDate(2018, 8, 11);
  if DaysBetween( firstDate, secondDate) = 0
    then WriteLn('Both dates are same')
    else
      if firstDate < secondDate
        then WriteLn('First date is earlier')
        else WriteLn('First date is later');
  ReadLn;
end.

See also