在Delphi中将月份名称转换为数字?

在Delphi中将月份名称转换为数字?,delphi,date-parsing,Delphi,Date Parsing,是否有一些内置的Delphi(XE2)/Windows方法将月份名称转换为数字1-12;不要在(t格式设置)LongMonthNames[]我自己?中循环,您可以使用StrUtils中的索引,如果未找到字符串,则返回-1 Caption := IntToStr( IndexStr(FormatSettings.LongMonthNames[7], FormatSettings.LongMonthNames) + 1); 编辑: 为了避免转换和区分大小写的问题,您可以使用IndexText,

是否有一些内置的Delphi(XE2)/Windows方法将月份名称转换为数字1-12;不要在
(t格式设置)LongMonthNames[]
我自己?

中循环,您可以使用
StrUtils中的
索引,如果未找到字符串,则返回
-1

Caption := IntToStr(
  IndexStr(FormatSettings.LongMonthNames[7], FormatSettings.LongMonthNames) + 1);
编辑:
为了避免转换和区分大小写的问题,您可以使用
IndexText
,如下所示:

Function GetMonthNumber(Const Month:String):Integer; overload;
begin
   Result := IndexText(Month,FormatSettings.LongMonthNames)+1
end;

您可以从
StrUtils
中使用
IndexStr
,如果未找到字符串,则返回
-1
,例如

Caption := IntToStr(
  IndexStr(FormatSettings.LongMonthNames[7], FormatSettings.LongMonthNames) + 1);
编辑:
为了避免转换和区分大小写的问题,您可以使用
IndexText
,如下所示:

Function GetMonthNumber(Const Month:String):Integer; overload;
begin
   Result := IndexText(Month,FormatSettings.LongMonthNames)+1
end;

我找不到一个方法,但我写了一个

好的,我将此函数更改为其他格式设置

function GetMonthNumberofName(AMonth: String): Integer; overload;
function GetMonthNumberofName(AMonth: String; AFormatSettings: TFormatSettings): Integer; overload;

function GetMonthNumberofName(AMonth: String): Integer;
begin
  Result:= GetMonthNumberofName(AMonth, System.SysUtils.FormatSettings);
end;

function GetMonthNumberofName(AMonth: String; AFormatSettings: TFormatSettings): Integer;
var
  intLoop: Integer;
begin
  Result:= -1;
  if (not AMonth.IsEmpty) then
  begin
    for intLoop := Low(AFormatSettings.LongMonthNames) to High(AFormatSettings.LongMonthNames) do
    begin
      if SameText(AFormatSettings.LongMonthNames[intLoop], AMonth) then
      begin
        Result:= Intloop;
        Exit
      end;
    end;
  end;
end;
使用系统格式设置调用函数

GetMonthNumberofName('may');
或使用格式化设置

procedure TForm1.Button4Click(Sender: TObject);
var
  iMonth: Integer;
  oSettings:TFormatSettings;
begin
  // Ned
  // oSettings:= TFormatSettings.Create(2067);
  // Fr
  // oSettings:= TFormatSettings.Create(1036);
  // Eng
  oSettings:= TFormatSettings.Create(2057);
  iMonth:= GetMonthNumberofName(self.Edit1.Text, oSettings);
  showmessage(IntToStr(iMonth));
end;

我找不到一个方法,但我写了一个

好的,我将此函数更改为其他格式设置

function GetMonthNumberofName(AMonth: String): Integer; overload;
function GetMonthNumberofName(AMonth: String; AFormatSettings: TFormatSettings): Integer; overload;

function GetMonthNumberofName(AMonth: String): Integer;
begin
  Result:= GetMonthNumberofName(AMonth, System.SysUtils.FormatSettings);
end;

function GetMonthNumberofName(AMonth: String; AFormatSettings: TFormatSettings): Integer;
var
  intLoop: Integer;
begin
  Result:= -1;
  if (not AMonth.IsEmpty) then
  begin
    for intLoop := Low(AFormatSettings.LongMonthNames) to High(AFormatSettings.LongMonthNames) do
    begin
      if SameText(AFormatSettings.LongMonthNames[intLoop], AMonth) then
      begin
        Result:= Intloop;
        Exit
      end;
    end;
  end;
end;
使用系统格式设置调用函数

GetMonthNumberofName('may');
或使用格式化设置

procedure TForm1.Button4Click(Sender: TObject);
var
  iMonth: Integer;
  oSettings:TFormatSettings;
begin
  // Ned
  // oSettings:= TFormatSettings.Create(2067);
  // Fr
  // oSettings:= TFormatSettings.Create(1036);
  // Eng
  oSettings:= TFormatSettings.Create(2057);
  iMonth:= GetMonthNumberofName(self.Edit1.Text, oSettings);
  showmessage(IntToStr(iMonth));
end;

我很确定这个没有标准函数…我很确定这个没有标准函数…(或者ANSIText表示不区分大小写)。我不会这样做,因为我必须强制转换StringANSIString,我的代码在所有括号中都变得不可读;-)我会给这个答案打分,但不会把它标为正确答案。谢谢你的努力。(或者ANSIText对案例不敏感)。我不会这样做,因为我必须强制转换StringANSIString,我的代码在所有括号中都变得不可读;-)我会给这个答案打分,但不会把它标为正确答案。谢谢你的努力。我可能会做一个不区分大小写的比较,使用SameText而不是“=”。另外,如果这个函数的重载版本采用了TFormatSettings参数呢?@dummzeuch:我不理解重载该函数的原因?函数GetMonthNumberofName(AMonth:String):Integer;超载;函数GetMonthNumberofName(AMonth:String;AFormatSettings:TFormatSettings):整数;超载;第二种变体使用给定的AFormatSettings参数而不是System.SysUtils.FormatSettings,以满足程序需要为不同于操作系统中配置的语言环境进行转换的情况。(很抱歉,注释不允许换行。)我可能会使用SameText而不是“=”进行不区分大小写的比较。另外,如果这个函数的重载版本采用了TFormatSettings参数呢?@dummzeuch:我不理解重载该函数的原因?函数GetMonthNumberofName(AMonth:String):Integer;超载;函数GetMonthNumberofName(AMonth:String;AFormatSettings:TFormatSettings):整数;超载;第二种变体使用给定的AFormatSettings参数而不是System.SysUtils.FormatSettings,以满足程序需要为不同于操作系统中配置的语言环境进行转换的情况。(很抱歉,评论不允许换行。)