如何在日期末尾添加序数后缀?

发布于 2024-12-10 02:40:41 字数 588 浏览 1 评论 0原文

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

我尝试使用 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;

I'm using Delphi BDS2006 how can I format the date (01/10/2011) to look something like
1st Oct 2011

I tried using the
ShowMessage(FormatDateTime('ddd mmm yyyy', now));

the message I get is Sat Oct 2011

ddd gives me Satand not 1st

Similar way I want to add st,nd,rd,th to the Dates

Is there a built in procedure or function to do this or I have to manually check for the date and assign the suffix to it

I'm currently using this

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;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

愛上了 2024-12-17 02:40:41

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;

There's nothing built in to Delphi to do that form of day. You will have to do it yourself. Like this:

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;
遇见了你 2024-12-17 02:40:41

这是与区域设置无关的英语版本。 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;

Here's the locale independent English version of the same. GetShortMonth is there because ShortMonthNames takes the month abbreviations from the locale settings.

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文