Sign in Join
Wiki copytemporary

TDateTime: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: TDateTime

OldNewLine
1+TDateTime is stored as a double, the integer part representing days and the fractional part being fraction of a day. The integer part is the number of days that have passed since December 30, 1899, and can be a negative number. The fractional part reflects the fraction of a 24-hour day without regard to the sign of the TDateTime value, so care must be taken when computing negative TDateTime values with a fractional part.\
2+Methods exist in Sysutils (datetimeroutines) that allow reading, writing and calculating with TDateTime, converting to and from a number of formats.\
3+The following table displays examples of TDateTime values and their corresponding dates and times:
4+
5+| Double Value | 'dd-mm-yyyy hh:nn:ss' |
6+|--------------|-------------------------------------|
7+| -1.75 | 29-12-1899 18:00:00 |
8+| -1.5 | 29-12-1899 12:00:00 |
9+| -1.25 | 29-12-1899 06:00:00 |
10+| 0 | 30-12-1899 00:00:00 |
11+| 1.25 | 31-12-1899 06:00:00 |
12+| 2.25 | 01-01-1900 06:00:00 |
13+| 25569 | 01-01-1970 00:00:00 (Unix Epoch) |
14+| 36152.5 | 23-12-1998 12:00:00 (FPC 1.0 Beta2) |
15+| 36526 | 01-01-2000 00:00:00 |
16+| 44927 | 01-01-2023 00:00:00 |
17+
18+## Get current time
19+
20+[[Unit]] SysUtils [[Function|function]] Now retrieves the current system date and time.
21+
22+``` pascal
23+ function Now : TDateTime;
24+```
25+
26+## Adding and subtracting TDateTime
27+
28+Unit DateUtils function DaysBetween tell number of whole days between two DateTime values.
29+
30+``` pascal
31+ function DaysBetween ( const ToDate, FromDate : TDateTime ) : Integer;
32+```
33+
34+Unit DateUtils function WeeksBetween tell number of whole weeks between two DateTime values.
35+
36+``` pascal
37+ function WeeksBetween( const ToDate, FromDate : TDateTime ):Integer;
38+```
39+
40+``` pascal
41+
42+program DateProject1;
43+
44+uses
45+ SysUtils,DateUtils;
46+const
47+ DateFormatChars = 'dd"/"mm"/"yyyy';
48+var
49+ DateTime1, DateTime2: TDateTime;
50+begin
51+ DateTime1 := now;
52+ DateTime2 := DateTime1 + 4*7; //28 days later
53+ WriteLn('Current date is '+ FormatDateTime( DateFormatChars, DateTime1 ));
54+ WriteLn('28 days later date is '+ FormatDateTime( DateFormatChars, DateTime2 ));
55+ WriteLn('Number of days is '+ ( DaysBetween( DateTime1 ,DateTime2)).ToString );
56+ WriteLn('Number of weeks is '+ ( WeeksBetween( DateTime1 ,DateTime2)).ToString );
57+ ReadLn;
58+end.
59+
60+```
61+
62+## Set Date
63+
64+Unit SysUtils function EncodeDate to set the date.
65+
66+``` pascal
67+ function EncodeDate ( const Year, Month, Day : Word ) : TDateTime;
68+```
69+
70+## Compare two TDateTime
71+
72+``` pascal
73+
74+program CompareTwoDateTime;
75+
76+uses
77+ SysUtils, DateUtils;
78+var
79+ firstDate, secondDate: TDateTime;
80+begin
81+ firstDate := EncodeDate(2000, 2, 29);
82+ secondDate := EncodeDate(2018, 8, 11);
83+ if DaysBetween( firstDate, secondDate) = 0
84+ then WriteLn('Both dates are same')
85+ else
86+ if firstDate < secondDate
87+ then WriteLn('First date is earlier')
88+ else WriteLn('First date is later');
89+ ReadLn;
90+end.
91+```
92+
93+## See also
94+
95+- Date and time format [[ISO 8601]]
96+- [[RFC 1123 Time Format]]
97+- [datetimeroutines](https://www.freepascal.org/docs-html/rtl/sysutils/datetimeroutines.html) in the [[sysutils|sysUtils unit]]
98+
99+---
100+*From the Free Pascal wiki, page "TDateTime", as it stood on 2023-01-29 after a change by Mario13, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*

Back to history