中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档 | 网通镜像
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 程序开发 > 编程语言 > Delphi > 综合文章
根据时间日期格式从字符串中解析日期时间
作者:未知 时间:2004-10-13 12:12 出处:Blog 责编:chinaitpower
              摘要:暂无
根据时间日期格式从字符串中解析日期时间
function StrToDtFmt(const S, Fmt: String; Dft: TDateTime): TDateTime;

function StrToDtFmt(const S, Fmt: String; Dft: TDateTime): TDateTime;
var
  Pts: array[1..10] of Integer;
  Wds: array[1..10] of Integer;
  Vls: array[1..10] of Word;
  i, j, n, m, k, d: Integer;
  t: String;
  c: Char;
  dt: TDateTime;
begin
  // 只处理数字格式的日期和时间
  i := 1;
  n := 1;    
  t := Trim(AnsiUpperCase(Fmt));
  // 解析格式串
  while i <= Length(t) do
  begin
    case t[i] of
      'Y': Pts[n] := 1;
      'M': Pts[n] := 2;
      'D': Pts[n] := 3;
      'H': Pts[n] := 4;
      'N': Pts[n] := 5;
      'S': Pts[n] := 6;
      'Z': Pts[n] := 7;
      else
      begin
        i := i + 1;
        Continue;
      end;
    end;
    c := t[i];
    i := i + 1;
    m := 1;
    while t[i] = c do
    begin
      Inc(i);
      Inc(m);
    end;
    if t[i] in ['Y','M','D','H','N','S','Z'] then
      Wds[n] := m
    else
      Wds[n] := 0;
    n := n + 1;
    if n > 7 then Break;
  end;
  n := n - 1;
  // 开始转化
  Result := Dft;
  if Length(S) <= 0 then Exit;
  DecodeDate(Result, Vls[1], Vls[2], Vls[3]);
  DecodeTime(Result, Vls[4], Vls[5], Vls[6], Vls[7]);
  m := 1;
  i := 1;
  k := Length(S);
  while m <= n do
  begin
    while not (S[i] in ['0'..'9', #0]) do Inc(i);
    if i > k then Break;
    d := 0;
    j := i;
    while (S[i] in ['0'..'9']) and
      ((Wds[m] <= 0) or (i - j < Wds[m])) do
    begin
      d := d * 10 + Ord(S[i]) - Ord('0');
      i := i + 1;
    end;
    Vls[Pts[m]] := d;
    if i > k then Break;
    m := m + 1;
  end;
  if TryEncodeDate(Vls[1], Vls[2], Vls[3], dt) then
    Result := Int(dt) + Frac(Result);
  if TryEncodeTime(Vls[4], Vls[5], Vls[6], Vls[7], dt) then
    Result := Int(Result) + Frac(dt);
end;
关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有