字符串转换为日期时间型:(50分)

  • 主题发起人 主题发起人 masterDelphi401
  • 开始时间 开始时间
M

masterDelphi401

Unregistered / Unconfirmed
GUEST, unregistred user!
如何将200202231135转换为日期时间,非常感谢!!!
 
这个?!自己写程序转换吧。
//先将2002 02 23 11 35转换成2002-02-23 11:35,然后再转换
ShowMessage(DateTimeToStr(StrToDateTime('2002-02-23 11:35')));
 
procedure MyStringToDate(Value: string): TDateTime;
var
y, m, d, h, mm: Word;
begin
y:=StrToInt(Copy(Value, 1, 4));
Delete(Value, 1, 4);
m:=StrToInt(Copy(Value, 1, 2));
Delete(Value, 1, 2);
d:=StrToInt(Copy(Value, 1, 2));
Delete(Value, 1, 2);
h:=StrToInt(Copy(Value, 1, 2));
Delete(Value, 1, 2);
mm:=StrToInt(Value);
Result:=EncodeDate(y, m, d)+EncodeTime(h, mm, 0, 0)
end;
 

to LeeChange:
你的函数有个小问题,呵呵,procedure是不允许有Result的,另外函数的最后一句
你忘了加分号了呀。
to masterDelphi401:
用我改造过的这个函数,能够解决你的问题。
function sTot(Value: string): TDateTime;
var
y, m, d, h, mm: Word;
begin
y:=StrToInt(Copy(Value, 1, 4));
Delete(Value, 1, 4);
m:=StrToInt(Copy(Value, 1, 2));
Delete(Value, 1, 2);
d:=StrToInt(Copy(Value, 1, 2));
Delete(Value, 1, 2);
h:=StrToInt(Copy(Value, 1, 2));
Delete(Value, 1, 2);
mm:=StrToInt(Value);
sTot :=EncodeDate(y, m, d)+EncodeTime(h, mm, 0, 0);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
//调用自定义的转换函数sTot
showMessage(datetimeToStr(sTot('200202231259')));
end;

请为LeeChange和小唐加分吧,呵呵,在此先谢过了。

 
改写LeeChange的过程如下:
function TForm1.Mtstrtodate(value: string): TdateTime;
var
y, m, d, h, mm: Word;
begin
y:=StrToInt(Copy(Value, 1, 4));
Delete(Value, 1, 4);
m:=StrToInt(Copy(Value, 1, 2));
Delete(Value, 1, 2);
d:=StrToInt(Copy(Value, 1, 2));
Delete(Value, 1, 2);
h:=StrToInt(Copy(Value, 1, 2));
Delete(Value, 1, 2);
mm:=StrToInt(Value);
Result:=EncodeDate(y, m, d)+EncodeTime(h, mm, 0, 0) ;

end;
 
如果可以写成这样就好办了。'2002-02-23-11-35'
 
关键是转成这样的
2002-02-23 11:35:00
一切的代码的目录就围着这个转:)
 
这样就可以实现上面两位的要求了,呵呵,
(sTot是我上面贴过的函数)
showMessage(formatdatetime('yyyy-mm-dd hh:mm:ss',sTot('200202231259')));

(我现在穷得很,只有50分了,帮忙快点给分好吧?)
 
后退
顶部