饗庭さん、M&Sさん、こんにちは。
細川です。
M&Sさんの方法が一番良いと思いますが、ShortDateFormat などは最近の Delphi では、 FormatSettings
に纏まりました。
FormatSettings.ShortDateFormat などとして扱います。
なお、個人的には以下の関数(String2Date)で変換しています。
参考になれば、幸いです。
-----------------------------------------------------------------------------
interface
function String2Date(
const iStr: String;
const iFormat: String = 'yyyy/mm/dd hh:nn:ss'): TDateTime;
implementation
function TryStrToDateTimeEx(
const iStr: String;
out oDateTime: TDateTime): Boolean;
label
Last;
const
DateSeps: array [0.. 1] of Char = ('/', '-');
TimeSeps: array [0.. 1] of Char = (':', '.');
var
FS: TFormatSettings;
i, j: Integer;
begin
FS := TFormatSettings.Create;
for i := Low(DateSeps) to High(DateSeps) do begin
FS.DateSeparator := DateSeps[i];
for j := Low(TimeSeps) to High(TimeSeps) do begin
FS.TimeSeparator := TimeSeps[j];
Result := TryStrToDateTime(iStr, oDateTime, FS);
if (Result) then
goto Last;
end;
end;
Last:
end;
function String2Date(
const iStr: String;
const iFormat: String = 'yyyy/mm/dd hh:nn:ss'): TDateTime;
var
KeepShortDateFormat: String;
begin
Result := 0;
if (iStr = '') then
Exit;
KeepShortDateFormat := FormatSettings.ShortDateFormat;
FormatSettings.ShortDateFormat := iFormat;
if (not TryStrToDateTimeEx(iStr, Result)) then
Result := 0;
FormatSettings.ShortDateFormat := KeepShortDateFormat;
end;
-----------------------------------------------------------------------------
Regards,
HOSOKAWA Jun / embarcadero MVP for Delphi
[S/G] SERIALGAMES Inc.
TEL: 03-5812-0980
FAX: 03-5812-0970
---------------------------------------------------------------
このメールには、本来の宛先の方のみに限定された機密情報が含まれて
いる場合がございます。お心あたりのない場合は、送信者にご連絡のうえ、
このメールを削除してくださいますようお願い申し上げます。
PLEASE READ:This e-mail is confidential and intended for
the named recipient only. If you are not an intended recipient,
please notify the sender and delete this e-mail.
---------------------------------------------------------------
MLホームページ:
http://www.freeml.com/delphi-users
----------------------------------------------------------------------
freemlを運営するGMOメディア株式会社では、
2017年新卒採用の募集をしております。
募集の職種は下記3職種
エンジニア職/Webデザイナー職/営業企画職
興味をお持ちの方はマイナビよりエントリーお願いします
http://ad.freeml.com/cgi-bin/sa.cgi?id=nKa5a
------------------------------------------------------[freeml byGMO]--