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

标签 delphi

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

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

我需要找到包含日期的字符串并将其格式化为 MM/DD/YYYY。 为了实现这一点,我运行 for 循环并传递 TryStrToDate 中的每个字符串。如果字符串是日期类型,它将返回 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;

最佳答案

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

正如雷米评论的那样,有一个选项参数 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;

输出将显示:

Testing GB
Found Date: 05 October 2017

Testing US
Found Date: 10 May 2017

Testing with modified ShortDate Format
5/10/2017 is not a valid date

关于delphi - 如何在Delphi中格式化MM/DD/YYYY日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47657635/

相关文章:

c# - Delphi 调用非托管 dll 工作正常,但 C# 不

delphi - Indy TCPClient OnDisconnect 事件不起作用

delphi - 提供缓冲区来接收 Out LPWSTR 值

c++ - 堆积面积计算

delphi - 如何在同时处理多个位图的同时提高性能?

Delphi DLL 未卸载,可能是由于仍在分配的 GDI

delphi - 从 Delphi 2010 IDE 打印格式化源代码

delphi - 从 Anydac 移动到 Firedac 时 NULL 丢失

delphi - 清除 Firemonkey TListView 搜索文本

delphi - “监听器”用于检测鼠标图标的更改