:: My another contributions in Delphi

»Delphi3000.com
posting and uploading delphi articles
[currently as Top 66 uploader]


»Komunitas Delphi Indonesia
starting as a member

Saturday, November 25, 2006

:: Displaying Month Names in Indonesian or Other Language Using FormatDateTime Function

» FormatDateTime Function

Category:

» DateTime & System

Question/Problem/Abstract:
» How we can display long month names in a specific language, eg Indonesia
So, the result will be like this : today is 25 Nopember 2006

Answer:

Actually, that's quiet simple to do that
You only need to replace the default (Delphi) "LongMonthNames" constants with the preferable any language you desire.

For this example, I want to display a specific date into Indonesian Date Format (dd MMMM yyyy)

Which,
» January is Januari in Indonesian
» February is Februari
» March is Maret
» April is April
» May is Mei
» June is Juni
» July is Juli
» August is Agustus
» September is September (unchanged)
» October is Oktober
» November is Nopember
» December is Desember

The example code would be like this :
-------------------------------------

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, StdCtrls;

type
TForm1 = class(TForm)
DateTimePicker1: TDateTimePicker;
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

// English:
// this variable ("namaBulan") is used to replace month names' string
// from English into Indonesian
// this variable is used in "replaceLongMonthNamesWithIndonesian" procedure

// dipakai untuk menggantikan string nama bulan dr bhs Inggris ke Indonesia
// digunakan dalam procedure "replaceLongMonthNamesWithIndonesian"
namaBulan: array[1..12] of string =
( 'Januari', 'Februari', 'Maret', 'April', 'Mei', 'Juni', 'Juli',
'Agustus', 'September', 'Oktober', 'Nopember', 'Desember');

// You can change the string constant above with your language choice

implementation

{$R *.dfm}

procedure replaceLongMonthNamesWithIndonesian;
var i : integer;
begin
for i:=1 to High(namaBulan) do
LongMonthNames[i] := namaBulan[i];
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
replaceLongMonthNamesWithIndonesian;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessageFmt(
'The date in Indonesian format (dd mmmm yyyy) : %s',
[FormatDateTime('dd MMMM yyyy', DateTimePicker1.Date)]);
end;

end.

-------------------------------------
// end of the code

if you want to change the short name of a month with some other language either, you need to change the "ShortMonthNames" like we did above


Enjoy it

ps : the original code is posted @delphi3000.com [http://www.delphi3000.com/articles/article_4642.asp] by me either


0 comments: