📜  Pascal-日期和时间

📅  最后修改于: 2020-11-03 16:22:00             🧑  作者: Mango


您编写的大多数软件都需要实现某种形式的日期函数,以返回当前日期和时间。日期是日常生活中的重要组成部分,因此无需思考就可以轻松地与他们合作。 Pascal还提供了用于日期算术的强大工具,使操作日期变得容易。但是,对于不同的编译器,这些函数的实际名称和工作方式有所不同。

获取当前日期和时间

Pascal的TimeToString函数以冒号(:)分隔的形式为您提供当前时间。以下示例显示如何获取当前时间-

program TimeDemo;
uses sysutils;

begin
   writeln ('Current time : ',TimeToStr(Time));
end.

编译并执行上述代码后,将产生以下结果-

Current time : 18:33:08

Date函数以TDateTime格式返回当前日期。 TDateTime是一个双精度值,需要一些解码和格式化。以下程序演示了如何在程序中使用它来显示当前日期-

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]));
end.

编译并执行上述代码后,将产生以下结果-

Date: 4.111300000000000E+004
Today is (DD/MM/YY):23/7/2012

Now函数返回当前日期和时间-

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

编译并执行上述代码后,将产生以下结果-

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

Free Pascal提供了一个简单的时间戳结构TTimeStamp ,其格式如下-

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

各种日期和时间功能

免费Pascal提供以下日期和时间功能-

Sr.No. Function Name & Description
1

function DateTimeToFileDate(DateTime: TDateTime):LongInt;

Converts DateTime type to file date.

2

function DateTimeToStr( DateTime: TDateTime):;

Constructs string representation of DateTime

3

function DateTimeToStr(DateTime: TDateTime; const FormatSettings: TFormatSettings):;

Constructs string representation of DateTime

4

procedure DateTimeToString(out Result: ;const FormatStr: ;const DateTime: TDateTime);

Constructs string representation of DateTime

5

procedure DateTimeToString(out Result: ; const FormatStr: ; const DateTime: TDateTime; const FormatSettings: TFormatSettings);

Constructs string representation of DateTime

6

procedure DateTimeToSystemTime(DateTime: TDateTime; out SystemTime: TSystemTime);

Converts DateTime to system time

7

function DateTimeToTimeStamp( DateTime: TDateTime):TTimeStamp;Converts DateTime to timestamp

8

function DateToStr(Date: TDateTime):;

Constructs string representation of date

9

function DateToStr(Date: TDateTime; const FormatSettings: TFormatSettings):;

Constructs string representation of date

10

function Date: TDateTime;

Gets current date

11

function DayOfWeek(DateTime: TDateTime):Integer;

Gets day of week

12

procedure DecodeDate(Date: TDateTime; out Year: Word; out Month: Word; out Day: Word);

Decodes DateTime to year month and day

13

procedure DecodeTime(Time: TDateTime; out Hour: Word; out Minute: Word; out Second: Word; out MilliSecond: Word);

Decodes DateTime to hours, minutes and seconds

14

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

Encodes year, day and month to DateTime

15

function EncodeTime(Hour: Word; Minute: Word; Second: Word; MilliSecond: Word):TDateTime;

Encodes hours, minutes and seconds to DateTime

16

function FormatDateTime(const FormatStr: ; DateTime: TDateTime):;

Returns string representation of DateTime

17

function FormatDateTime(const FormatStr: ; DateTime: TDateTime; const FormatSettings: TFormatSettings):;

Returns string representation of DateTime

18

function IncMonth(const DateTime: TDateTime; NumberOfMonths: Integer = 1):TDateTime;

Adds 1 to month

19

function IsLeapYear(Year: Word):Boolean;

Determines if year is leap year

20

function MSecsToTimeStamp(MSecs: Comp):TTimeStamp;

Converts number of milliseconds to timestamp

21

function Now: TDateTime;

Gets current date and time

22

function StrToDateTime(const S:):TDateTime;

Converts string to DateTime

23

function StrToDateTime(const s: ShortString; const FormatSettings: TFormatSettings):TDateTime;

Converts string to DateTime

24

function StrToDateTime(const s: AnsiString; const FormatSettings: TFormatSettings):TDateTime;

Converts string to DateTime

25

function StrToDate(const S: ShortString):TDateTime;

Converts string to date

26

function StrToDate(const S: Ansistring):TDateTime;

Converts string to date

27

function StrToDate(const S: ShortString; separator: Char):TDateTime;

Converts string to date

28

function StrToDate(const S: AnsiString; separator: Char):TDateTime;

Converts string to date

29

function StrToDate(const S: ShortString; const useformat: ; separator: Char):TDateTime;

Converts string to date

30

function StrToDate(const S: AnsiString; const useformat: ; separator: Char):TDateTime;

Converts string to date

31

function StrToDate(const S: PChar; Len: Integer; const useformat: ; separator: Char = #0):TDateTime;

Converts string to date

32

function StrToTime(const S: Shortstring):TDateTime;

Converts string to time

33

function StrToTime(const S: Ansistring):TDateTime;

Converts string to time

34

function StrToTime(const S: ShortString; separator: Char):TDateTime;

Converts string to time

35

function StrToTime(const S: AnsiString; separator: Char):TDateTime;

Converts string to time

36

function StrToTime(const S: ; FormatSettings: TFormatSettings):TDateTime;

Converts string to time

37

function StrToTime(const S: PChar; Len: Integer; separator: Char = #0):TDateTime;

Converts string to time

38

function SystemTimeToDateTime(const SystemTime: TSystemTime):TDateTime;

Converts system time to datetime

39

function TimeStampToDateTime(const TimeStamp: TTimeStamp):TDateTime;

Converts time stamp to DateTime

40

function TimeStampToMSecs(const TimeStamp: TTimeStamp):comp;

Converts Timestamp to number of milliseconds

41

function TimeToStr(Time: TDateTime):;

Returns string representation of Time

42

function TimeToStr(Time: TDateTime; const FormatSettings: TFormatSettings):;

Returns string representation of Time

43

function Time: TDateTime;

Get current time

以下示例说明了上述某些功能的用法-

Program DatenTimeDemo;
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.

编译并执行上述代码后,将产生以下结果:

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