- 超级版主
- 1228894
- 26975
- 25
- 8050 朵
- 36129 个
- 4746 个
- 421340
- 2012-07-18
|
1#
t
T
发表于 2022-07-23 10:09
|
|只看楼主
如何实现时间转秒:思路1:split代码- t = "3天1小时20分17秒"
- 天 = split(t, "天")(0)
- 小时 = GetStrAB(t, "天", "小时")
- 分=GetStrAB(t, "小时", "分")
- 秒 = GetStrAB(t, "分", "秒")
- TracePrint 天*3600*24+小时*3600+分*60+秒&"秒"
- Function GetStrAB(Str, StrA, StrB)
- If InStr(Str,StrA)>0 And InStr(Str,StrB)>0 Then GetStrAB=Split(Split(Str,StrA)(1),StrB)(0)
- End Function
复制代码 调试输出- t = "3天1小时20分17秒"
- 天 = split(t, "天")(0)
- 小时 = GetStrAB(t, "天", "小时")
- 分=GetStrAB(t, "小时", "分")
- 秒 = GetStrAB(t, "分", "秒")
- TracePrint 天*3600*24+小时*3600+分*60+秒&"秒"
- Function GetStrAB(Str, StrA, StrB)
- If InStr(Str,StrA)>0 And InStr(Str,StrB)>0 Then GetStrAB=Split(Split(Str,StrA)(1),StrB)(0)
- End Function
复制代码 思路2"正则+split代码:- str = "3天1小时20分17秒"
- Set regEx = New RegExp
- regEx.IgnoreCase = True
- Execute "regEx.Global = True"
- regEx.pattern = "\d+"
- Set Matches = regEx.Execute(str)
- ReDim Rlt(Matches.Count)
- For Each Match in Matches
- Rlt(i) = Match.Value
- i = i + 1
- Next
- TracePrint Join(rlt, "|")
- TracePrint rlt(0)*3600*24+rlt(1)*3600+rlt(2)*60+rlt(3)&"秒"
复制代码调试输出: - 脚本 N7.Q ,第14行:3|1|20|17|
- 脚本 N7.Q ,第15行:264017
复制代码思路3:Replace+eval 代码: - t = "3天1小时20分17秒"
- t = Replace(t, "天", "*3600*24+")
- t = Replace(t, "小时", "*3600+")
- t = Replace(t, "分", "*60+")
- t = Replace(t, "秒", "")
- TracePrint t
- TracePrint eval(t)
复制代码调试输出: - 脚本 N7.Q ,第6行:3*3600*24+1*3600+20*60+17
- 脚本 N7.Q ,第7行:264017
复制代码作者什么也没留下
|