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

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

问题描述

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

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

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

进行逆转换的惯用方法是什么?StringToDateTime() 似乎不存在.

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 使用 ISO 8601 进行日期和日期时间存储.

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

从 Delphi 6 开始,Delphi 在 XSBuiltIns 单元.

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

这个答案解释了如何日期时间,这仅适用于日期使用TXSDate 类:

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天全站免登陆