用strtodate是不行的,格式不一样,可能要自已写了,不知有没有简单一点的方法

解决方案 »

  1.   

    字符串'Apr 01, 2001'是javascript的日期表示法,在delphi里估计没有现成的转换,自己写个过程转换吧。
      

  2.   

    function StrToDateEx(mStr: string): TDate;
    const
      cShortMonthNames: array[1..12] of string = (
        'Jan', 'Feb', 'Mar', 'Apr',
        'May', 'Jun', 'Jul', 'Aug',
        'Sep', 'Oct', 'Nov', 'Dec');
    var
      I: Integer;
      vOldShortDateFormat: string;
    begin
      for I := 1 to 12 do
        mStr := StringReplace(mStr, cShortMonthNames[I], IntToStr(I) + '-',
          [rfIgnoreCase]);
      mStr := StringReplace(mStr, ',', '-', [rfReplaceAll]);
      mStr := StringReplace(mStr, ' ', '', [rfReplaceAll]);
      vOldShortDateFormat := ShortDateFormat;
      ShortDateFormat := 'mm-dd-yyyy';
      try
        Result := StrToDate(mStr);
      finally
        ShortDateFormat := vOldShortDateFormat;
      end;
    end; { StrToDateEx }procedure TForm1.Button1Click(Sender: TObject);
    begin
      Caption := DateToStr(StrToDateEx('Apr 01, 2001'));
    end;
      

  3.   

    function StrToDateEx(mStr: string): TDate;
    const
      cShortMonthNames: array[1..12] of string = (
        'Jan', 'Feb', 'Mar', 'Apr',
        'May', 'Jun', 'Jul', 'Aug',
        'Sep', 'Oct', 'Nov', 'Dec');
    var
      I: Integer;
      vOldShortDateFormat: string;
    begin
      for I := 1 to 12 do
        mStr := StringReplace(mStr, cShortMonthNames[I], IntToStr(I) + '-',
          [rfIgnoreCase]);
      mStr := StringReplace(mStr, ',', '-', [rfReplaceAll]);
      mStr := StringReplace(mStr, ' ', '', [rfReplaceAll]);
      vOldShortDateFormat := ShortDateFormat;
      ShortDateFormat := 'mm-dd-yyyy';
      try
        Result := StrToDate(mStr);
      finally
        ShortDateFormat := vOldShortDateFormat;
      end;
    end; { StrToDateEx }procedure TForm1.Button1Click(Sender: TObject);
    begin
      Caption := DateToStr(StrToDateEx('Apr 01, 2001'));
    end;
      

  4.   

    方法:
    1、要是你的日期是数据库取出的,那么从数据库取出时先convert(char(),字段,/*101-112随你取个但要保证delphi能处理,如:112为YYYYMMDD格式,这样可以直接用d中的转换函数*/)转化。
    2.将Delphi日历控件格式设置成:YYYY-MM-DD格式,这样比较好处理。要是以后安装在客户机上,也要有相应的格式。
    PS:要是你自己写你说得那种格式的日期的话,估计是浪费!