Wiki


Pascal - Date & Time

Locked

User Rating: 5 / 5

Star ActiveStar ActiveStar ActiveStar ActiveStar Active
 
Contents[Hide]
Contents[Hide]

Most of the software you write needs implementing some form of date functions returning current date and time. Dates are so much part of everyday life that it becomes easy to work with them without thinking. Pascal also provides powerful tools for date arithmetic that makes manipulating dates easy. However, the actual name and workings of these functions are different for different compilers.

 

1. Getting the Current Date & Time:

Pascal's TimeToString function gives you the current time in a colon(: ) delimited form. The following example shows how to get the current time:

program TimeDemo;
uses sysutils;
begin
   writeln ('Current time : ',TimeToStr(Time));
end.
 

When the above code was compiled and executed, it produced following result:

Current time : 18:33:08

The Date function returns the current date in TDateTime format. The TDateTime is a double value, which needs some decoding and formatting. The following program demonstrates how to use it in your program to display the current date:

Program DateDemo;
uses sysutils;
var
   YY,MM,DD :Word;begin
   writeln ('Date : ',Date);
   DeCodeDate(Date,YY,MM,DD);
   writeln (format ('Today is (DD/MM/YY): %d/%d/%d ',[dd,mm,yy]));
 
  readln; // to see the output
end.
 

When the above code was compiled and executed, it produced following result:

Date: 4.195700000000000E+004
Today is (DD/MM/YY):14/11/2014

The Now function returns the current date and time:

Program DatenTimeDemo;
uses sysutils;
begin
   writeln ('Date and Time at the time of writing : ',DateTimeToStr(Now));
end.
 

When the above code was compiled and executed, it produced following result:

Date and Time at the time of writing : 23/7/2012 18:51:

Free Pascal provides a simple time stamp structure named TTimeStamp, which has the following format:

type TTimeStamp= record
  Time:Integer;
  Date:Integer;
end;
 

 

2. Various Date & Time Functions:

Free Pascal provides the following date and time functions:

S.NFunction Name & Description
1 function DateTimeToFileDate(DateTime: TDateTime):LongInt;
Convert DateTime type to file date.
2 function DateTimeToStr( DateTime: TDateTime):;
Construct string representation of DateTime
3 function DateTimeToStr(DateTime: TDateTime; const FormatSettings: TFormatSettings):;
Construct string representation of DateTime
4 procedure DateTimeToString(out Result: ;const FormatStr: ;const DateTime: TDateTime);
Construct string representation of DateTime
5 procedure DateTimeToString( out Result: ; const FormatStr: ; const DateTime: TDateTime; const FormatSettings: TFormatSettings );
Construct string representation of DateTime
6 procedure DateTimeToSystemTime( DateTime: TDateTime; out SystemTime: TSystemTime );
Convert DateTime to system time
7 function DateTimeToTimeStamp( DateTime: TDateTime):TTimeStamp;Convert DateTime to timestamp
8 function DateToStr( Date: TDateTime ):;
Construct string representation of date
9 function DateToStr( Date: TDateTime; const FormatSettings: TFormatSettings ):;
Construct string representation of date
10 function Date: TDateTime;
Get current date
11 function DayOfWeek( DateTime: TDateTime ):Integer;
Get day of week
12 procedure DecodeDate( Date: TDateTime; out Year: Word; out Month: Word; out Day: Word );
Decode DateTime to year month and day
13 procedure DecodeTime( Time: TDateTime; out Hour: Word; out Minute: Word; out Second: Word; out MilliSecond: Word );
Decode DateTime to hours, minutes and seconds
14 function EncodeDate( Year: Word; Month: Word; Day: Word ):TDateTime;
Encode year, day and month to DateTime
15 function EncodeTime( Hour: Word; Minute: Word; Second: Word; MilliSecond: Word ):TDateTime;
Encode hours, minutes and seconds to DateTime
16 function FormatDateTime( const FormatStr: ; DateTime: TDateTime ):;
Return string representation of DateTime
17 function FormatDateTime( const FormatStr: ; DateTime: TDateTime; const FormatSettings: TFormatSettings ):;
Return string representation of DateTime
18 function IncMonth( const DateTime: TDateTime; NumberOfMonths: Integer = 1 ):TDateTime;
Add 1 to month
19 function IsLeapYear( Year: Word ):Boolean;
Determine if year is leap year
20 function MSecsToTimeStamp( MSecs: Comp ):TTimeStamp;
Convert number of milliseconds to timestamp
21 function Now: TDateTime;
Get current date and time
22 function StrToDateTime( const S: ):TDateTime;
Convert string to DateTime
23 function StrToDateTime( const s: ShortString; const FormatSettings: TFormatSettings ):TDateTime;
Convert string to DateTime
24 function StrToDateTime( const s: AnsiString; const FormatSettings: TFormatSettings ):TDateTime;
Convert string to DateTime
25 function StrToDate( const S: ShortString ):TDateTime;
Convert string to date
26 function StrToDate( const S: Ansistring ):TDateTime;
Convert string to date
27 function StrToDate( const S: ShortString; separator: Char ):TDateTime;
Convert string to date
28 function StrToDate( const S: AnsiString; separator: Char ):TDateTime;
Convert string to date
29 function StrToDate( const S: ShortString; const useformat: ; separator: Char ):TDateTime;
Convert string to date
30 function StrToDate( const S: AnsiString; const useformat: ; separator: Char ):TDateTime;
Convert string to date
31 function StrToDate( const S: PChar; Len: Integer; const useformat: ; separator: Char = #0 ):TDateTime;
Convert string to date
32 function StrToTime( const S: Shortstring ):TDateTime;
Convert string to time
33 function StrToTime( const S: Ansistring ):TDateTime;
Convert string to time
34 function StrToTime( const S: ShortString; separator: Char ):TDateTime;
Convert string to time
35 function StrToTime( const S: AnsiString; separator: Char ):TDateTime;
Convert string to time
36 function StrToTime( const S: ; FormatSettings: TFormatSettings ):TDateTime;
Convert string to time
37 function StrToTime( const S: PChar; Len: Integer; separator: Char = #0 ):TDateTime;
Convert string to time
38 function SystemTimeToDateTime( const SystemTime: TSystemTime ):TDateTime;
Convert system time to datetime
39 function TimeStampToDateTime( const TimeStamp: TTimeStamp ):TDateTime;
Convert time stamp to DateTime
40 function TimeStampToMSecs( const TimeStamp: TTimeStamp ):comp;
Convert Timestamp to number of milliseconds
41 function TimeToStr( Time: TDateTime ):;
Return string representation of Time
42 function TimeToStr( Time: TDateTime; const FormatSettings: TFormatSettings ):;
Return string representation of Time
43 function Time: TDateTime;
Get current time

 

The following example illustrates the use of some of the above functions:

ProgramDatenTimeDemo;
uses sysutils;var
year, month, day, hr, min, sec, ms:Word;begin
   writeln ('Date and Time at the time of writing : ',DateTimeToStr(Now));
   writeln('Today is ',LongDayNames[DayOfWeek(Date)]);
   writeln;
   writeln('Details of Date: ');
   DecodeDate(Date,year,month,day);
   writeln (Format('Day: %d',[day]));
   writeln (Format('Month: %d',[month]));
   writeln (Format('Year: %d',[year]));
   writeln;
   writeln('Details of Time: ');
   DecodeTime(Time,hr, min, sec, ms);
   writeln (format('Hour: %d:',[hr]));
   writeln (format('Minutes: %d:',[min]));
   writeln (format('Seconds: %d:',[sec]));
   writeln (format('Milliseconds: %d:',[hr]));end.

When the above code was compiled and executed, it produced following result:

Date and Time at the time of writing : 7/24/2012 8:26:
Today is Tuesday
Details of Date:
Day:24
Month:7
Year: 2012
Details of Time:
Hour: 8
Minutes: 26
Seconds: 21
Milliseconds: 8