Pascal - 日期和時間



您編寫的許多軟體都需要實現某種形式的日期函式來返回當前日期和時間。日期在日常生活中如此常見,以至於在不加思考的情況下就能輕鬆地使用它們。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;

各種日期和時間函式

Free Pascal 提供以下日期和時間函式:

序號 函式名稱和描述
1

function DateTimeToFileDate(DateTime: TDateTime):LongInt;

將 DateTime 型別轉換為檔案日期。

2

function DateTimeToStr( DateTime: TDateTime):;

構造 DateTime 的字串表示形式

3

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

構造 DateTime 的字串表示形式

4

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

構造 DateTime 的字串表示形式

5

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

構造 DateTime 的字串表示形式

6

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

將 DateTime 轉換為系統時間

7

function DateTimeToTimeStamp( DateTime: TDateTime):TTimeStamp; 將 DateTime 轉換為時間戳

8

function DateToStr(Date: TDateTime):;

構造日期的字串表示形式

9

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

構造日期的字串表示形式

10

function Date: TDateTime;

獲取當前日期

11

function DayOfWeek(DateTime: TDateTime):Integer;

獲取星期幾

12

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

將 DateTime 解碼為年、月和日

13

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

將 DateTime 解碼為小時、分鐘和秒

14

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

將年、日和月編碼為 DateTime

15

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

將小時、分鐘和秒編碼為 DateTime

16

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

返回 DateTime 的字串表示形式

17

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

返回 DateTime 的字串表示形式

18

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

將月份加 1

19

function IsLeapYear(Year: Word):Boolean;

確定年份是否為閏年

20

function MSecsToTimeStamp(MSecs: Comp):TTimeStamp;

將毫秒數轉換為時間戳

21

function Now: TDateTime;

獲取當前日期和時間

22

function StrToDateTime(const S:):TDateTime;

將字串轉換為 DateTime

23

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

將字串轉換為 DateTime

24

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

將字串轉換為 DateTime

25

function StrToDate(const S: ShortString):TDateTime;

將字串轉換為日期

26

function StrToDate(const S: Ansistring):TDateTime;

將字串轉換為日期

27

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

將字串轉換為日期

28

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

將字串轉換為日期

29

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

將字串轉換為日期

30

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

將字串轉換為日期

31

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

將字串轉換為日期

32

function StrToTime(const S: Shortstring):TDateTime;

將字串轉換為時間

33

function StrToTime(const S: Ansistring):TDateTime;

將字串轉換為時間

34

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

將字串轉換為時間

35

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

將字串轉換為時間

36

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

將字串轉換為時間

37

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

將字串轉換為時間

38

function SystemTimeToDateTime(const SystemTime: TSystemTime):TDateTime;

將系統時間轉換為 datetime

39

function TimeStampToDateTime(const TimeStamp: TTimeStamp):TDateTime;

將時間戳轉換為 DateTime

40

function TimeStampToMSecs(const TimeStamp: TTimeStamp):comp;

將時間戳轉換為毫秒數

41

function TimeToStr(Time: TDateTime):;

返回時間的字串表示形式

42

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

返回時間的字串表示形式

43

function Time: TDateTime;

獲取當前時間

以下示例說明了如何使用上述一些函式:

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
廣告