Search code examples
delphidateformatdatetime

How can I put an ordinal suffix on the end of a date?


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;

Solution

  • 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;