Delphi XE5で和暦の始まりの年を取得するには

Delphi XE3では、System.SysUtilsユニットのEraYearOffsets変数で和暦の始まりの年を取得できました。

uses
  System.SysUtils;

var
  I: Integer;

begin
  for I := 1 to MaxEraCount do
  begin
    if EraNames[I].IsEmpty then
      Continue;
    Writeln(Format('%s元年は西暦%d年', [EraNames[I], EraYearOffsets[I]]));
  end;

end.

実行結果

平成元年は西暦1989年
昭和元年は西暦1926年
大正元年は西暦1912年
明治元年は西暦1868年

Delphi XE5ではEraYearOffsets変数が廃止されています。

Delphi XE5で和暦の始まりの年を取得するにはSystem.SysUtilsユニットのTFormatSettingsを使用します。

uses
  System.SysUtils;

var
  FormatSettings: TFormatSettings;
  EraInfo: TFormatSettings.TEraInfo;

begin
  FormatSettings := TFormatSettings.Create;
  for EraInfo in FormatSettings.EraInfo do
  begin
    Writeln(Format('%s元年は西暦%d年', [EraInfo.EraName, EraInfo.EraOffset]));
  end;

end.

実行結果

平成元年は西暦1989年
昭和元年は西暦1926年
大正元年は西暦1912年
明治元年は西暦1868年

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください