Pascal-date-time

提供:Dev Guides
移動先:案内検索

パスカル-日付と時刻

作成するソフトウェアのほとんどは、現在の日付と時刻を返す何らかの形式の日付関数を実装する必要があります。 日付は日常生活の非常に重要な部分であるため、考えずに日付を使用することは簡単です。 Pascalは、日付の操作を簡単にする日付計算用の強力なツールも提供します。 ただし、これらの関数の実際の名前と動作は、コンパイラーによって異なります。

現在の日付と時刻を取得する

PascalのTimeToString関数は、コロン(:)区切り形式で現在の時刻を提供します。 次の例は、現在の時刻を取得する方法を示しています-

program TimeDemo;
uses sysutils;

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

上記のコードをコンパイルして実行すると、次の結果が生成されます-

Current time : 18:33:08
*Date* 関数は、 *TDateTime* 形式で現在の日付を返します。 TDateTimeはdouble値であり、デコードとフォーマットが必要です。 次のプログラムは、プログラムでそれを使用して現在の日付を表示する方法を示しています-
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;

さまざまな日付と時刻の関数

無料のパスカルは、次の日付と時刻の機能を提供します-

Sr.No. Function Name & Description
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;Converts DateTime to timestamp
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;

システム時刻を日時に変換します

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