根據時間日期格式從字串中解析日期時間

2021-03-31 08:56:30 字數 2010 閱讀 1189

根據時間日期格式從字串中解析日期時間

function strtodtfmt(const s, fmt: string; dft: tdatetime): tdatetime;

function strtodtfmt(const s, fmt: string; dft: tdatetime): tdatetime;

varpts: 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;

C 時間 日期格式大全

有時候我們要對時間進行轉換,達到不同的顯示效果 預設格式為 2005 6 6 14 33 34 如果要換成成200506,06 2005,2005 6 6或更多的該怎麼辦呢?我們要用到 datetime.tostring的方法 string,iformatprovider using system ...

格式化時間日期函式

實現以下格式模式 英文可以改為中文 格式模式 說明 d 月中的某一天。一位數的日期沒有前導零。dd 月中的某一天。一位數的日期有一個前導零。ddd 週中某天的縮寫名稱,定義範圍 sun mon tue wed thu fri sat dddd 週中某天的完整名稱,定義範圍 sunday monday...

時間日期格式化用法

另附上時間格式化詳細用法 格式字元 關聯屬性 說明 d shortdatepattern d longdatepattern f 完整日期和時間 長日期和短時間 f fulldatetimepattern 長日期和長時間 g 常規 短日期和短時間 g 常規 短日期和長時間 m m monthdayp...

時間 日期格式化技巧

實際應用中,經常需要,2016 09 09 這種格式的字串日期格式 轉換方式 1.在生產日期格式時,在個位數字前補零,例如一些日期的js外掛。2.在拿到資料後,後臺統一處理。後臺格式化處理 函式 sprintf 資料格式,資料 資料格式 返回百分比符號 b 二進位制數 c 依照 ascii 值的字元...

js時間日期格式化

對date的擴充套件,將 date 轉化為指定格式的string 月 m 日 d 小時 h 分 m 秒 s 季度 q 可以用 1 2 個佔位符,年 y 可以用 1 4 個佔位符,毫秒 s 只能用 1 個佔位符 是 1 3 位的數字 例子 new date format yyyy mm dd hh m...