Difference between revisions of "TDateTime"
From Free Pascal wiki
Jump to navigationJump to search (refine the header a little) |
|||
(3 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
{{TDateTime}} | {{TDateTime}} | ||
− | + | ||
− | TDateTime is a | + | TDateTime is stored as a double, the integer part representing days and the fractional part being fraction of a day. Methods exist in Sysutils (datetimeroutines) that allow reading, writing and calculating with TDateTime, converting to and from a number of formats. |
== Get current time == | == Get current time == | ||
Line 85: | Line 85: | ||
* Date and time format [[ISO 8601]] | * Date and time format [[ISO 8601]] | ||
* [[RFC 1123 Time Format]] | * [[RFC 1123 Time Format]] | ||
+ | * {{Doc|package=RTL|unit=sysutils|identifier=datetimeroutines|Date and time routines}} in the [[sysutils|<syntaxhighlight lang="pascal" inline>sysUtils</syntaxhighlight> unit]] |
Revision as of 01:36, 27 September 2021
│
English (en) │
suomi (fi) │
TDateTime is stored as a double, the integer part representing days and the fractional part being fraction of a day. Methods exist in Sysutils (datetimeroutines) that allow reading, writing and calculating with TDateTime, converting to and from a number of formats.
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
- Date and time format ISO 8601
- RFC 1123 Time Format
- datetimeroutines in the
sysUtils
unit