如何转换字符串至DateTime格式
有没什么函数可以转换字符串至DateTime格式的?如,转换20120407165907为2012-04-07 16:59:07!
谢谢! 没有现成的函数转换, 可以自己用 StringMid 建立一个函数吧?
关键是要转换的字符串怎么生成的? 用正则替换一次就行了。Local $Str = '20120407165907'
Local $Test = StringRegExpReplace($str, '(\d{4})(\d\d)(\d\d)(\d\d)(\d\d)(\d\d)', '$1-$2-$3 $4:$5:$6')
MsgBox(0, '替换结果', $Test) 回复 3# afan
谢谢!正则不会,自己简单写了个函数...汗...还是正则好{:face (356):}Func ChangeStringToDate($String)
;string格式 20120407165853
;year
$year=StringLeft($String,4)
;month
$mon=StringMid($String,5,2)
;day
$mday=StringMid($String,7,2)
;hour
$hour=StringMid($String,9,2)
;min
$min=StringMid($String,11,2)
;sec
$sec=StringRight($String,2)
$tFile = _Date_Time_EncodeFileTime($mon, $mday, $year, $hour, $min, $sec)
Return(_Date_Time_FileTimeToStr($tFile,1))
EndFunc 本帖最后由 半芯竹 于 2012-4-7 20:57 编辑
Local $a='20120407165907'
MsgBox(0,0,StringFormat("%04s-%02s-%02s %02s:%02s:%02s\n", StringMid ($a,1,4), StringMid ($a,5,2), StringMid ($a,7,2),StringMid ($a,9,2),StringMid ($a,11,2),StringMid ($a,13,2)))
我的是麻烦了点。。。呵呵。还能看懂。。。。 自己写一个
#Include <Date.au3>
$time = MyStrtoDateTime("20120407165907")
If "error" = $time Then
MsgBox(16, "", "失败")
Else
MsgBox(0, "", $time)
EndIf
Func MyStrtoDateTime($mystr)
Local $ret = ""
Local $len = StringLen($mystr)
If 14 = $len Then
Local $array = StringToASCIIArray($mystr)
Local $i
For $i = 0 To UBound($array)-1
If $array[$i]<Asc("0") Or $array[$i]>Asc("9") Then ExitLoop
Next
If 0 = $i - UBound($array) Then
For $i = 0 To UBound($array)-1
$array[$i] -= Asc("0")
Next
If $array*10+$array<1 Then
$ret = "error - 月份最小是1"
ElseIf $array*10+$array>12 Then
$ret = "error - 月份最大是12"
ElseIf $array*10+$array<1 Then
$ret = "error - 号数最小是1"
ElseIf $array*10+$array>31 Then
$ret = "error - 号数最大是12"
ElseIf $array*10+$array>23 Then
$ret = "error - 小时最大是23"
ElseIf $array*10+$array>59 Then
$ret = "error - 分钟最大是59"
ElseIf $array*10+$array>59 Then
$ret = "error - 秒数最大是59"
ElseIf ( (2 = $array*10+$array) And $array*10+$array> _
28+_DateIsLeapYear($array*1000+$array*100+$array*10+$array) ) Then
$ret = "error - " & $array*1000+$array*100+$array*10+$array & _
"的2月只有" & 28+_DateIsLeapYear($array*1000+$array*100+$array*10+$array) & "天!"
Else
$ret &= StringMid($mystr, 1, 4)
$ret &= "-" & StringMid($mystr, 5, 2)
$ret &= "-" & StringMid($mystr, 7, 2)
$ret &= " " & StringMid($mystr, 9, 2)
$ret &= ":" & StringMid($mystr, 11, 2)
$ret &= ":" & StringMid($mystr, 13, 2)
EndIf
Else
$ret = "error - 有字母标点等非数字字符"
EndIf
Else
$ret = "error - 空字符串或长度不等14"
EndIf
Return $ret
EndFunc
a版的正则简单有效,学习了!{:face (239):} 对比之下,看出正则的强大之处! 用正则替换一次就行了。
afan 发表于 2012-4-7 20:13 http://www.autoitx.com/images/common/back.gif
{:1_206:}
A版的正则简洁。一行搞定 正规表达式强大呀 题楼主感谢回帖的朋友,学习了
页:
[1]