字符串与日期转换的转换问题

micky_xu 2002-06-27 12:49:51
字符串'Apr 01, 2001'如何转换成TDateTime类型,是否有直接的转换函数?
谢谢!
...全文
74 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
qybao 2002-06-27
  • 打赏
  • 举报
回复
字符串'Apr 01, 2001'是javascript的日期表示法,在delphi里估计没有现成的转换,自己写个过程转换吧。
luoweicaisd 2002-06-27
  • 打赏
  • 举报
回复
用strtodate是不行的,格式不一样,可能要自已写了,不知有没有简单一点的方法
hammer_shi 2002-06-27
  • 打赏
  • 举报
回复
方法:
1、要是你的日期是数据库取出的,那么从数据库取出时先convert(char(),字段,/*101-112随你取个但要保证delphi能处理,如:112为YYYYMMDD格式,这样可以直接用d中的转换函数*/)转化。
2.将Delphi日历控件格式设置成:YYYY-MM-DD格式,这样比较好处理。要是以后安装在客户机上,也要有相应的格式。


PS:要是你自己写你说得那种格式的日期的话,估计是浪费!
王集鹄 2002-06-27
  • 打赏
  • 举报
回复
function StrToDateEx(mStr: string): TDate;
const
cShortMonthNames: array[1..12] of string = (
'Jan', 'Feb', 'Mar', 'Apr',
'May', 'Jun', 'Jul', 'Aug',
'Sep', 'Oct', 'Nov', 'Dec');
var
I: Integer;
vOldShortDateFormat: string;
begin
for I := 1 to 12 do
mStr := StringReplace(mStr, cShortMonthNames[I], IntToStr(I) + '-',
[rfIgnoreCase]);
mStr := StringReplace(mStr, ',', '-', [rfReplaceAll]);
mStr := StringReplace(mStr, ' ', '', [rfReplaceAll]);
vOldShortDateFormat := ShortDateFormat;
ShortDateFormat := 'mm-dd-yyyy';
try
Result := StrToDate(mStr);
finally
ShortDateFormat := vOldShortDateFormat;
end;
end; { StrToDateEx }

procedure TForm1.Button1Click(Sender: TObject);
begin
Caption := DateToStr(StrToDateEx('Apr 01, 2001'));
end;
王集鹄 2002-06-27
  • 打赏
  • 举报
回复
function StrToDateEx(mStr: string): TDate;
const
cShortMonthNames: array[1..12] of string = (
'Jan', 'Feb', 'Mar', 'Apr',
'May', 'Jun', 'Jul', 'Aug',
'Sep', 'Oct', 'Nov', 'Dec');
var
I: Integer;
vOldShortDateFormat: string;
begin
for I := 1 to 12 do
mStr := StringReplace(mStr, cShortMonthNames[I], IntToStr(I) + '-',
[rfIgnoreCase]);
mStr := StringReplace(mStr, ',', '-', [rfReplaceAll]);
mStr := StringReplace(mStr, ' ', '', [rfReplaceAll]);
vOldShortDateFormat := ShortDateFormat;
ShortDateFormat := 'mm-dd-yyyy';
try
Result := StrToDate(mStr);
finally
ShortDateFormat := vOldShortDateFormat;
end;
end; { StrToDateEx }

procedure TForm1.Button1Click(Sender: TObject);
begin
Caption := DateToStr(StrToDateEx('Apr 01, 2001'));
end;
surfguy 2002-06-27
  • 打赏
  • 举报
回复
自己写代码吧
dreamfan 2002-06-27
  • 打赏
  • 举报
回复
就是啊。现成的是没有了

5,388

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧