主页C++ Builder 资料C++ Builder 参考手册System 日期和时间StrToDateTime
C++ Builder 串口控件
C++ Builder 编程技巧
C++ Builder 操作指南
C++ Builder 参考手册
基础知识
cfloat 浮点数
cmath 数学函数
cstdlib 标准库函数
System 字符串
System 日期和时间
 • TDate
 • TTime
 • TDateTime
 • TDateTimeBase
 • Now
 • UnixToDateTime
 • DateTimeToUnix
 • DateTimeToStr
 • DateToStr
 • DecodeDate
 • DecodeTime
 • DecodeDateTime
 • FormatDateTime
 • StrToDate
 • StrToDateDef
 • StrToDateTime
 • StrToDateTimeDef
 • StrToTime
 • StrToTimeDef
 • TimeToStr
System.Math.hpp 数学函数
其他数据类型
VCL 基础类
VCL 应用程序
Pictures 图片
Graphics 绘图
Additional 控件
System 控件
A ~ Z 字母顺序排列的目录
网友留言/技术支持
StrToDateTime - 字符串转为日期时间,转换失败抛出异常

函数原型:

TDateTime __fastcall StrToDateTime(const UnicodeString S);
TDateTime __fastcall StrToDateTime(const UnicodeString S, const TFormatSettings &AFormatSettings);

头文件:

#include <System.SysUtils.hpp> (XE2 之后) #include <SysUtils.hpp> (XE 之前)

参数:

S: 字符串,需要符合参数 AFormatSettings 或全局对象 FormatSettings 指定的格式。
AFormatSettings: TFormatSettings 类型的格式信息。

返回值:

返回值为 TDateTime 类型的日期时间。
如果没有 AFormatSettings 参数,按照全局对象 FormatSettings 指定的格式,
如果有 AFormatSettings 参数,按照这个参数指定的格式,把字符串转为日期。
如果参数 S 与指定的格式不符,会抛出异常 EConvertError
如果年只有两位数,采用 TwoDigitYearCenturyWindow 指定的年的范围

 

例1:使用默认格式

默认格式,即全局对象 FormatSettings 指定的格式

TDateTime dt = StrToDateTime(L"2017/06/30 23:45:56");

运行结果:

dt 等于 2017 年 6 月 30 日 23 时 45 分 56 秒

 

例2:使用参数给定格式

LCID lcid = MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US); // 英语,美国
TFormatSettings fs = TFormatSettings::Create(lcid);       // 按照这个地区和语言的格式
TDateTime dt = StrToDateTime(L"06/30/2017 23:45:56", fs);

运行结果:

dt 等于 2017 年 6 月 30 日 23 时 45 分 56 秒
◤上一页:StrToDateDef下一页:StrToDateTimeDef

C++ 爱好者 -- Victor Chen 的个人网站 www.cppfans.com 辽ICP备11016859号