有下面一段語句:
procedure TForm1.Button1Click(Sender: TObject);
begin
  try
    strtodate(self.Edit1.Text);
  except
    showmessage('Date error')
  end;end;如果我將本機的日期格式設為“日/月/年”
strtodate()只接受“日/月/年”這種格式的日期
如果我將本機的日期格式設為“年/月/日”
strtodate()不接受“日/月/年”這種格式的日期
怎樣能使strtodate()都能接受這兩種日期格式呢?SQL中,將“日/月/年”格式的日期insert 到smalldatetime類型的字段是不是一定不行?

解决方案 »

  1.   

    strtodate(self.Edit1.Text);都出錯怎么去formatdatetime呢?
      

  2.   

    给你一段 代码 procedure TCommLoginFrm.GetLocaleDateTimeInfo;
    var
      pc: pChar;
      arr: array[0..30] of char;
    begin
      pc := new(pchar);
      GetLocaleInfo(GetSystemDefaultLCID, LOCALE_SDATE, pc, 5);
      CommPubDataU.GLocalDateSep := pc; // 日期的间隔符号  GetLocaleInfo(GetSystemDefaultLCID, LOCALE_STIME, pc, 5);
      CommPubDataU.GLocalTimeSep := pc; // 时间的间隔符号  GetLocaleInfo(GetSystemDefaultLCID, LOCALE_SSHORTDATE, arr, 30);
      CommPubDataU.GLocalYMDFormat := arr; // 年月日的形式  GetLocaleInfo(GetSystemDefaultLCID, LOCALE_STIMEFORMAT, arr, 30);
      CommPubDataU.GLocalHMFormat := arr;  // 分秒 的形式
    end;你获得后 用function FormatDateTime(const Format: string; DateTime: TDateTime): string; // Format传入 GLocalYMDFormat 即可...
      

  3.   

    我在想樓主是不是說讓輸入的格式是:日/月/年.
    其實你的機子可以用:年/月/日,但是你的輸入框的edit格式設為:日/月/年就可以了
      

  4.   

    我的目的是讓用戶輸入任何格式的日期,程序都不會彈‘date error’,
    就是strtodate(self.Edit1.Text);這句話能通過
      

  5.   

    Formatdatetime('dd-mm-yyyy',now());
    Formatdatetime('yyyy-mm-dd',now());