The TimetoString
function gives the current time as shown below:
program CurrentTime;uses sysutils;beginwriteln ('Current time : ',TimeToStr(Time));end.
The above code returns the expected output below:
Current time : 08:30:43
The Date
function gives the current date. The return value is of TDateTime
format that requires decoding as shown below:
Program CurrentDate;uses sysutils;varYY,MM,DD : Word;beginwriteln ('Date : ',Date);DeCodeDate (Date,YY,MM,DD);writeln (format ('Today is (DD/MM/YY): %d/%d/%d ',[dd,mm,yy]));end.
The above code returns the expected output below:
Date : 4.4369000000000000E+004
Today is (DD/MM/YY): 22/6/2021
The Now
function gives both the current time and date as shown below:
program CurrentDateTime;uses sysutils;beginwriteln ('Date and Time : ',DateTimeToStr(Now));end.
The above code returns the expected output below:
Date and Time : 22-6-21 08:39:00
The table below shows describes various date and time functions:
Function | Description |
---|---|
function DateTimeToFileDate(DateTime: TDateTime):LongInt; |
Converts DateTime type to file date. |
function DateTimeToStr( DateTime: TDateTime):; |
Constructs string representation of DateTime |
function DateTimeToStr(DateTime: TDateTime; const FormatSettings: TFormatSettings):; |
Constructs string representation of DateTime |
procedure DateTimeToString(out Result: ;const FormatStr: ;const DateTime: TDateTime); |
Constructs string representation of DateTime |
procedure DateTimeToString(out Result: ; const FormatStr: ; const DateTime: TDateTime; const FormatSettings: TFormatSettings); |
Constructs string representation of DateTime |
procedure DateTimeToSystemTime(DateTime: TDateTime; out SystemTime: TSystemTime); |
Converts DateTime to system time |
function DateTimeToTimeStamp( DateTime: TDateTime):TTimeStamp; |
Converts DateTime to timestamp |
function DateToStr(Date: TDateTime):; |
Constructs string representation of date |
function DateToStr(Date: TDateTime; const FormatSettings: TFormatSettings):; |
Constructs string representation of date |
function Date: TDateTime; |
Gets current date |
function DayOfWeek(DateTime: TDateTime):Integer; |
Gets day of week |
procedure DecodeDate(Date: TDateTime; out Year: Word; out Month: Word; out Day: Word); |
Decodes DateTime to year month and day |
procedure DecodeTime(Time: TDateTime; out Hour: Word; out Minute: Word; out Second: Word; out MilliSecond: Word); |
Decodes DateTime to hours, minutes and seconds |
function EncodeDate(Year: Word; Month: Word; Day: Word):TDateTime; |
Encodes year, day and month to DateTime |
function EncodeTime(Hour: Word; Minute: Word; Second: Word; MilliSecond: Word):TDateTime; |
Encodes hours, minutes and seconds to DateTime |
function FormatDateTime(const FormatStr: ; DateTime: TDateTime):; |
Returns string representation of DateTime |
function FormatDateTime(const FormatStr: ; DateTime: TDateTime; const FormatSettings: TFormatSettings):; |
Returns string representation of DateTime |
function IncMonth(const DateTime: TDateTime; NumberOfMonths: Integer = 1):TDateTime; |
Adds 1 to month |
function IsLeapYear(Year: Word):Boolean; |
Determines if year is leap year |
function MSecsToTimeStamp(MSecs: Comp):TTimeStamp; |
Converts number of milliseconds to timestamp |
function Now: TDateTime; |
Gets current date and time |
function StrToDateTime(const S:):TDateTime; |
Converts string to DateTime |
function StrToDateTime(const s: ShortString; const FormatSettings: TFormatSettings):TDateTime; |
Converts string to DateTime |
function StrToDateTime(const s: AnsiString; const FormatSettings: TFormatSettings):TDateTime; |
Converts string to DateTime |
function StrToDate(const S: ShortString):TDateTime; |
Converts string to date |
function StrToDate(const S: Ansistring):TDateTime; |
Converts string to date |
function StrToDate(const S: ShortString; separator: Char):TDateTime; |
Converts string to date |
function StrToDate(const S: AnsiString; separator: Char):TDateTime; |
Converts string to date |
function StrToDate(const S: ShortString; const useformat: ; separator: Char):TDateTime; |
Converts string to date |
function StrToDate(const S: AnsiString; const useformat: ; separator: Char):TDateTime; |
Converts string to date |
function StrToDate(const S: PChar; Len: Integer; const useformat: ; separator: Char = #0):TDateTime; |
Converts string to date |
function StrToTime(const S: Shortstring):TDateTime; |
Converts string to time |
function StrToTime(const S: Ansistring):TDateTime; |
Converts string to time |
function StrToTime(const S: ShortString; separator: Char):TDateTime; |
Converts string to time |
function StrToTime(const S: AnsiString; separator: Char):TDateTime; |
Converts string to time |
function StrToTime(const S: ; FormatSettings: TFormatSettings):TDateTime; |
Converts string to time |
function StrToTime(const S: PChar; Len: Integer; separator: Char = #0):TDateTime; |
Converts string to time |
function SystemTimeToDateTime(const SystemTime: TSystemTime):TDateTime; |
Converts system time to datetime |
function TimeStampToDateTime(const TimeStamp: TTimeStamp):TDateTime; |
Converts time stamp to DateTime |
function TimeStampToMSecs(const TimeStamp: TTimeStamp):comp; |
Converts Timestamp to number of milliseconds |
function TimeToStr(Time: TDateTime):; |
Returns string representation of Time |
function TimeToStr(Time: TDateTime; const FormatSettings: TFormatSettings):; |
Returns string representation of Time |
function Time: TDateTime; |
Get current time |
The following example shows the usage of different date and time functions:
Program DateTime;uses sysutils;varyear, month, day, hr, min, sec, ms: Word;Procedure TestYear (Y : Word);beginwriteln ('This year is a leap year? ', IsLeapYear(Y));end;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(LongDayNames[DayOfWeek(Date)]);writeln (Format ('Month: %d',[month]));writeln (Format ('Year: %d',[year]));TestYear(year);Writeln ('6 months ago it was: ',DateToStr(IncMonth(Date, -6)));Writeln ('6 months from now it will be: ' ,DateToStr(IncMonth(Date ,6)));writeln;writeln('Time: ');DecodeTime(Time,hr, min, sec, ms);writeln (format('Hour: %d',[hr]));writeln (format('Minutes: %d',[min]));writeln (format('Seconds: %d',[sec]));end.
Free Resources