如何将ISO 8601字符串转换为Delphi TDate? [英] How do I convert an ISO 8601 string to a Delphi TDate?

查看:153
本文介绍了如何将ISO 8601字符串转换为Delphi TDate?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用以下方式轻松地将Delphi TDate转换为ISO 8601格式:

  DateTimeToString(result,'yyyy-mm- dd',myDate); 

进行逆转换的惯用方法是什么?



显然,我可以手动执行硬方式解析字符串并对结果进行编码,但这似乎是一个糟糕的选择。

解决方案

为什么要重新发明轮? >

XML使用ISO 8601进行日期和日期时间存储。



Delphi已经内置了支持,因为Delphi 6在 XSBuiltIns 单元中。



这个答案解释了DateTime的这个,这是Date仅使用 TXSDate 类:

  with TXSDate.Create()do 
try
AsDate:= Date; //从TDateTime转换
DateString:= NativeToXS; //转换为WideString
finally
自由;
结束

与TXSDate.Create()do
try
XSToNative(DateString); //从WideString
转换Date:= AsDate; //转换为TDateTime
finally
自由;
结束


I can convert a Delphi TDate to ISO 8601 format easily using this:

DateTimeToString(result, 'yyyy-mm-dd', myDate);

What's the idiomatic way to do the inverse conversion? StringToDateTime() doesn't seem to exist.

Obviously I can do it the "hard" way by manually parsing the string and encoding the result, but that seems a poor choice.

解决方案

why re-invent the wheel?

XML uses ISO 8601 for date and date-time storage.

Delphi has had built-in support for that since Delphi 6 in the XSBuiltIns unit.

This answer explains how for DateTime, this is for Date only using the TXSDate class:

with TXSDate.Create() do
  try
    AsDate := Date; // convert from TDateTime
    DateString := NativeToXS; // convert to WideString
  finally
    Free;
  end;

with TXSDate.Create() do
  try
    XSToNative(DateString); // convert from WideString
    Date := AsDate; // convert to TDateTime
  finally
    Free;
  end;

这篇关于如何将ISO 8601字符串转换为Delphi TDate?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆