การกำหนดรูปแบบวันเวลาให้เป็นไปตามต้องการใน Delphi


การจัดเวลาใน Delphi จะทำการกำหนดผ่านตัวแปรชนิด TFormatSettings ซึ่งเป็นตัวแปรประเภท Record ซึ่งมีการประกาศไว้ดังนี้

 type

  TFormatSettings = record
    CurrencyFormat: Byte;
    NegCurrFormat: Byte;
    ThousandSeparator: Char;
    DecimalSeparator: Char;
    CurrencyDecimals: Byte;
    DateSeparator: Char;
    TimeSeparator: Char;
    ListSeparator: Char;
    CurrencyString: string;
    ShortDateFormat: string;
    LongDateFormat: string;
    TimeAMString: string;
    TimePMString: string;
    ShortTimeFormat: string;
    LongTimeFormat: string;

    ShortMonthNames: array[1..12] of string;
    LongMonthNames: array[1..12] of string;
    ShortDayNames: array[1..7] of string;
    LongDayNames: array[1..7] of string;
    TwoDigitYearCenturyWindow: Word;
  end;

เราสามารถรับค่าตัวแปรต่างๆ จาก windows ได้โดยเรียก GetLocaleFormatSettings ซึ่งมีรูปแบบการส่ง Parameter ดังนี้

 procedure GetLocaleFormatSettings(LCID: Integer; var FormatSettings: TFormatSettings);

หมายเหตุ ค่า LCID สำหรับประเทศไทยนั้นคือค่า 0x041e หรือ ฐานสิบเท่ากับ 1054 (Ref: http://www.microsoft.com/globaldev/nlsweb/default.mspx?submitted=041E&OS=Windows%20XP%20Service%20Pack%202 )

เมื่อเราได้ค่าตัวแปรต่าง ๆ จาก windows แล้ว หลังจากนั้นเราสามารถเปลี่ยนรูปแบบการแสดงผลและการรับค่าจาก string ให้เป็นไปตามที่เราต้องการได้แล้ว

ตัวอย่าง

var sformat : TFormatSettings
begin
  GetLocaleFormatSettings(1054,sformat); // รับรูปแบบการแสดงผลจาก windows
  sformat.DateSeperator := '-'; //กำหนดเครื่องหมายขั้นระหว่างวันเดือนปีเป็น '-'
  sformat.ShortDateFormat := 'd/m/yyyy'; //กำหนดการแสดงวันแบบสั้นให้อยู่ในรุปแบบ ว/ด/ปปปป
  ShowMessage('Date : ' + DateTimeToStr(now,sformat); //แสดงผล
end;

คำสำคัญ (Tags): #delphi#tdatetime#string
หมายเลขบันทึก: 43903เขียนเมื่อ 10 สิงหาคม 2006 01:17 น. ()แก้ไขเมื่อ 16 มิถุนายน 2012 10:38 น. ()สัญญาอนุญาต: จำนวนที่อ่านจำนวนที่อ่าน:


ความเห็น (0)

ไม่มีความเห็น

พบปัญหาการใช้งานกรุณาแจ้ง LINE ID @gotoknow
ClassStart
ระบบจัดการการเรียนการสอนผ่านอินเทอร์เน็ต
ทั้งเว็บทั้งแอปใช้งานฟรี
ClassStart Books
โครงการหนังสือจากคลาสสตาร์ท