0

我正在使用 Delphi BDS2006 如何格式化日期(01/10/2011)看起来像 1st Oct 2011

我尝试使用 ShowMessage(FormatDateTime('ddd mmm yyyy', now));

我得到的信息是Sat Oct 2011

ddd给我Sat而不是1st

我想添加st,nd,rd,th到日期的类似方式

是否有内置程序或功能来执行此操作,或者我必须手动检查日期并为其分配后缀

我目前正在使用这个

case dayof(now)mod 10 of
 1 : days:=inttostr(dayof(dob))+'st';
 2 : days:=inttostr(dayof(dob))+'nd';
 3 : days:=inttostr(dayof(dob))+'rd';
 else days:=inttostr(dayof(dob))+'th';

 end;
4

2 回答 2

5

Delphi 没有内置任何东西来做这种形式的日子。你必须自己做。像这样:

function DayStr(const Day: Word): string;
begin
  case Day of
  1,21,31:
    Result := 'st';  
  2,22:
    Result := 'nd';  
  3,23:
    Result := 'rd';  
  else
    Result := 'th';  
  end;
  Result := IntToStr(Day)+Result;
end;
于 2011-10-13T12:03:51.903 回答
4

这是与语言环境无关的英语版本。GetShortMonth 在那里是因为ShortMonthNames从语言环境设置中获取月份的缩写。

function GetOrdinalSuffix(const Value: Integer): string;
begin
  case Value of
    1, 21, 31: Result := 'st';
    2, 22: Result := 'nd';
    3, 23: Result := 'rd';
  else
    Result := 'th';
  end;
end;

function GetShortMonth(const Value: Integer): string;
begin
  case Value of
    1: Result := 'Jan';
    2: Result := 'Feb';
    3: Result := 'Mar';
    4: Result := 'Apr';
    5: Result := 'May';
    6: Result := 'Jun';
    7: Result := 'Jul';
    8: Result := 'Aug';
    9: Result := 'Sep';
    10: Result := 'Oct';
    11: Result := 'Nov';
    12: Result := 'Dec';
  end;
end;

procedure TForm1.DateTimePicker1Change(Sender: TObject);
var
  Day: Word;
  Month: Word;
  Year: Word;
begin
  DecodeDate(DateTimePicker1.Date, Year, Month, Day);
  ShowMessage(Format('%d%s %s %d', [Day, GetOrdinalSuffix(Day), GetShortMonth(Month), Year]));
end;
于 2011-10-13T12:22:25.303 回答