简体   繁体   English

如何在delphi中将字符串转换为日期

[英]How to convert string to date in delphi

When I convert string type to TDateTime I get an error. 当我将string类型转换为TDateTime我收到一个错误。 I'm using VarToDateTime function. 我正在使用VarToDateTime函数。 My date as string is 2018-07-11T13:45:14.363 . 我的字符串日期是2018-07-11T13:45:14.363

var
  s: string;
  v: Variant;
  dt: TDateTime;
begin
  s := '2018-07-11T13:45:14.363';
  v := s;
  dt := VarToDateTime(v);
end;

在此输入图像描述

The success of a conversion from string to TDateTime using VarToDateTime depends on locale settings in the users system. 使用VarToDateTimestring转换为TDateTime VarToDateTime取决于用户系统中的区域设置。 The conversion fails if those settings do not match the string. 如果这些设置与字符串不匹配,则转换失败。 This is the reason why the conversion fails on my system, as also on yours. 这就是我的系统转换失败的原因,也是你的系统转换失败的原因。


The primary option , if you are working with Delphi XE6 or later, is to use function ISO8601ToDate() as suggested by Marc Guillot in another answer 如果您正在使用Delphi XE6或更高版本, 主要选项是使用Marc Guillot建议的函数ISO8601ToDate() 在另一个答案中

If you are workin with Delphi 2010 or later you can use the solution presented here. 如果您正在使用Delphi 2010或更高版本,则可以使用此处提供的解决方案。

Earlier versions than Delphi 2010 choke on the "T" in the input string, and may succeed if the "T" is removed or replaced with a space. 早于Delphi 2010的版本会阻塞输入字符串中的“T”,如果删除“T”或用空格替换,则可能会成功。


Use a conversion function which accepts a TFormatSetting that can be adjusted according to the string to convert. 使用转换函数接受TFormatSetting ,可以根据要转换的字符串进行调整。 Such a function is the following overload of StrToDateTime() (See Embarcadero document ) 这样的函数是StrToDateTime()的以下重载(参见Embarcadero文档

function StrToDateTime(const S: string; const AFormatSettings: TFormatSettings): TDateTime;

Setting AFormatSettings to match the string to convert, ensures that the conversion succeeds: 设置AFormatSettings以匹配要转换的字符串,确保转换成功:

procedure TForm3.Button1Click(Sender: TObject);
var
  fs: TFormatSettings;
  s: string;
  dt: TDateTime;
begin
  fs := TFormatSettings.Create;
  fs.DateSeparator := '-';
  fs.ShortDateFormat := 'yyyy-MM-dd';
  fs.TimeSeparator := ':';
  fs.ShortTimeFormat := 'hh:mm';
  fs.LongTimeFormat := 'hh:mm:ss';

  s := '2018-07-11T13:45:14.363';
  dt := StrToDateTime(s, fs);
end;

These seems to be ISO8601 datetime strings : https://en.wikipedia.org/wiki/ISO_8601#Combined_date_and_time_representations 这些似乎是ISO8601日期时间字符串: https//en.wikipedia.org/wiki/ISO_8601#Combined_date_and_time_representations

So on Delphi XE 6 and later you can use the corresponding conversion function : ISO8601ToDate 因此,在Delphi XE 6及更高版本中,您可以使用相应的转换函数:ISO8601ToDate

http://docwiki.embarcadero.com/Libraries/XE8/en/System.DateUtils.ISO8601ToDate http://docwiki.embarcadero.com/Libraries/XE8/en/System.DateUtils.ISO8601ToDate

But if you are using an older version of Delphi then you can use the XMLTimeToDateTime function on the XSBuiltIns unit to do that conversion (available since Delphi 6). 但是如果您使用的是旧版本的Delphi,则可以使用XSBuiltIns单元上的XMLTimeToDateTime函数进行该转换(自Delphi 6起可用)。

http://docwiki.embarcadero.com/Libraries/Tokyo/en/Soap.XSBuiltIns.XMLTimeToDateTime http://docwiki.embarcadero.com/Libraries/Tokyo/en/Soap.XSBuiltIns.XMLTimeToDateTime

Try the function StrToDateTime which converts a string DateTime into a TDateTime value. 尝试使用StrToDateTime函数将string DateTime转换为TDateTime值。 Note that the datetime format passed should be the current system date/time format or else it will throw an exception. 请注意,传递的日期时间格式应为当前系统日期/时间格式,否则将引发异常。 An example: StrToDateTime('2018-07-11 12:34:56'); 一个例子: StrToDateTime('2018-07-11 12:34:56');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM