如何在Delphi中以MM / DD / YYYY格式化日期

时间:2017-12-05 15:52:53

标签: delphi

我有如下所述的字符串数组。

Param : array[1..5] of string = ('US','Dollor','27/03/2017','IND','INR');

我需要找到包含日期的字符串,并将其格式化为MM / DD / YYYY。 为了实现这一点,我正在运行循环并在TryStrToDate中传递每个字符串。如果string是日期类型,它将返回true,然后我将以所需格式格式化该字符串。我的代码如下

 for i := 1 to ParamCount do
  begin
   if TryStrToDate(Param[i],DateValue)=true then
   begin
    Param[i] := DateToStr(StrToDate(Param[i]));
    ShowMessage(Param[i]);
   end;
 end;

Param []中的日期在DD / MM / YYYY中,因此TryStrToDate无法理解。我该如何更改代码? 在此最终结果应如下

Param : array[1..5] of string = ('US','Dollor','03/27/2017','IND','INR');

使用Mark的解决方案如下:

  GetLocaleFormatSettings(ALocID, AFormatSettings);
  AFormatSettings.ShortDateFormat := 'DD/MM/YYYY';
  for i := 1 to ParamCount do
  begin
    if TryStrToDate(param[i], DateValue, AFormatSettings) = False then
      Continue;
    try
      DateVal := StrToDate(param[i], AFormatSettings);
      param[i] := DateToStr(DateVal);
      Continue;
    except
      Continue;
    end;
  end;

1 个答案:

答案 0 :(得分:5)

TryStrToDate是一个Delphi库函数,它将尝试将字符串转换为TDateTime类型。在我的代码中,我想接受一些TryStrToDate函数不允许的格式。要在第一个方法失败时转换这些日期,我调用VarToDateTime。这称为支持其他格式的变体函数。

正如Remy评论的那样,TFormatSettings类型的选项参数允许您对接受的格式进行一些控制。我仍然需要使用VarToDateTime来转换拼写出月份的日期而不是使用数值。

var
  DateValue: TDateTime;

for i:= 1 to ParamCount do
begin
  // If we don't have a valid date see if it looks like a different
  // normal date field.
  if TryStrToDate(Param[i], DateValue) = false then
    continue; 

  // If the simple TryStrToDate does not work use the Variants function,
  // there is no "Try" version so catch the error here.
  try
    DateValue := VarToDateTime(Param[i]);
  except
    continue;
  end;

  // Use/Reformat DateValue here
 Param[i] := FormatDateTime('MM/DD/YYYY', DateValue);
end;

有关使用TFormatSettings的其他信息
如果日期被解释为首先列出日期或月份,则TFormatSettings参数可以更改。如果未指定格式设置,则将使用操作系统默认值。 TryStrToDate查看短日期格式字符串以确定预期的订单。它查看第一个匹配字母的字符串格式,以决定可接受的格式:

case Chr(Ord(DateFormat[I]) and $DF) of
  'E': Result := doYMD;
  'Y': Result := doYMD;
  'M': Result := doMDY;
  'D': Result := doDMY;

这是一个可以向您展示差异的测试程序。您可以根据区域设置创建FormatSettings,也可以直接设置短日期格式。

procedure TForm7.Button3Click(Sender: TObject);
var
  s: string;
  d: TDateTime;
  FormatUS: TFormatSettings;
  FormatGB: TFormatSettings;
begin
  s := '5/10/2017';

  Memo1.Lines.Append('Testing GB');
  FormatGB := TFormatSettings.Create('en-GB');
  if TryStrToDate(s, d, FormatGB) = false then
  begin
    Memo1.Lines.Append(s + ' is not a valid date');
  end
  else
  begin
    Memo1.Lines.Append('Found Date: ' + FormatDateTime('dd MMMM YYYY', d));
    // will print: Found Date: 05 October 2017
  end;

  Memo1.Lines.Append('');
  Memo1.Lines.Append('Testing US');
  FormatUS := TFormatSettings.Create('en-US');
  if TryStrToDate(s, d, FormatUS) = false then
  begin
    Memo1.Lines.Append(s + ' is not a valid date');
  end
  else
  begin
    Memo1.Lines.Append('Found Date: ' + FormatDateTime('dd MMMM YYYY', d));
  end;

  Memo1.Lines.Append('');
  Memo1.Lines.Append('Testing with modified ShortDate Format');
  FormatUS.ShortDateFormat := 'yyyy/mm/dd';
  if TryStrToDate(s, d, FormatUS) = false then
  begin
    Memo1.Lines.Append(s + ' is not a valid date');
  end
  else
  begin
    Memo1.Lines.Append('Found Date: ' + FormatDateTime('dd MMMM YYYY', d));
  end;

end;

输出将显示:

  

测试GB
  发现日期:2017年10月5日

     

测试美国
  发现日期:2017年5月10日

     

使用修改后的ShortDate格式进行测试
  5/10/2017不是有效日期