"09:07:29:12:37:41-09:07:29:12:40:42"
我解的时候按":"和"-"解出两个时间
但有时不符合要求例如
"09::7:29:12:37:41-09:07:29:12:40:42"
"09:<7:29:12:37:41-09:07:29:12:40:42"
这两种解出来都不会不对了,我想问,除了自己一个字符一个字符的判断还有其它方便一些的方法吗?
13 个解决方案
#1
正则式是不是比较好?
#2
分析数据源,找出各种可能性,化归(具体手段例如:替换::为: 以及 替换:<为:)
#3
直接扫描,取出来分析!
#4
Dim strSource As String, strTmp As String
Dim strDates() As String, strItems() As String
Dim i As Integer, j As Integer
strSource = "09:07:29:12:37:41-09:07:29:12:40:42"
strDates = Split(strSource, "-")
If UBound(strDates) <> 1 Then
MsgBox "日期分隔符错误"
Exit Sub
End If
For i = 0 To 1
strItems = Split(strDates(i), ":")
If UBound(strItems) <> 5 Then
MsgBox "第 " & i + 1 & " 个日期内部分隔符错误"
Exit Sub
End If
strTmp = "20" & strItems(0) & "-" & strItems(1) & "-" & strItems(2) & " "
strTmp = strTmp & strItems(3) & ":" & strItems(4) & ":" & strItems(5)
If Not IsDate(strTmp) Then
MsgBox "第 " & i + 1 & " 个日期格式错误"
Else
Debug.Print strTmp
End If
Next i
Dim strDates() As String, strItems() As String
Dim i As Integer, j As Integer
strSource = "09:07:29:12:37:41-09:07:29:12:40:42"
strDates = Split(strSource, "-")
If UBound(strDates) <> 1 Then
MsgBox "日期分隔符错误"
Exit Sub
End If
For i = 0 To 1
strItems = Split(strDates(i), ":")
If UBound(strItems) <> 5 Then
MsgBox "第 " & i + 1 & " 个日期内部分隔符错误"
Exit Sub
End If
strTmp = "20" & strItems(0) & "-" & strItems(1) & "-" & strItems(2) & " "
strTmp = strTmp & strItems(3) & ":" & strItems(4) & ":" & strItems(5)
If Not IsDate(strTmp) Then
MsgBox "第 " & i + 1 & " 个日期格式错误"
Else
Debug.Print strTmp
End If
Next i
#5
时间字符串定义上有点自找麻烦。如果是 "09-07-29 12:37:41>09-07-29 12:40:42" 会省很多事。
#6
一个个字符分析:
Option Explicit
Function GetDateTime(ByVal s As String) As Date
Dim i As Long
Dim tmp
Dim arr(5)
Do
tmp = Mid(s, 1, 1)
If IsNumeric(tmp) And tmp <> 0 Then
tmp = Val(s)
arr(i) = tmp
i = i + 1
End If
s = Replace(s, tmp, "", 1, 1)
Loop While Len(s)
GetDateTime = DateSerial(CInt("20" & arr(0)), arr(1), arr(2)) & _
Space(1) & _
TimeSerial(arr(3), arr(4), arr(5))
End Function
Private Sub Command1_Click()
Dim s As String
Dim i As Long
Dim a
s = "09:07:29:12:37:41-09:07:29:12:40:42"
a = Split(s, "-")
For i = 0 To UBound(a)
Debug.Print GetDateTime(a(i)) & vbTab;
Next
Debug.Print
s = "09::7:29:12:37:41-09:07:29:12:40:42"
a = Split(s, "-")
For i = 0 To UBound(a)
Debug.Print GetDateTime(a(i)) & vbTab;
Next
Debug.Print
s = "09: <7:29:12:37:41-09:07:29:12:40:42"
a = Split(s, "-")
For i = 0 To UBound(a)
Debug.Print GetDateTime(a(i)) & vbTab;
Next
Debug.Print
End Sub
#7
改下:
Function GetDateTime(ByVal s As String) As Date
Dim i As Long
Dim tmp
Dim arr(5)
Do
tmp = Mid(s, 1, 1)
If IsNumeric(tmp) And tmp <> 0 Then
tmp = Val(s)
arr(i) = tmp
i = i + 1
End If
s = Replace(s, tmp, "", 1, 1)
Loop While Len(s)
GetDateTime = DateSerial(CInt("20" & Format(arr(0), "00")), arr(1), arr(2)) & _
Space(1) & _
TimeSerial(arr(3), arr(4), arr(5))
End Function
#8
这里有人找你领钱:http://topic.csdn.net/u/20090724/07/3271eda7-6703-42d4-bee3-6bc53ab156cc.html?35796
#9
呵呵,东方我去了,不过那个东东我不会.....
#10
大家都很日情啊,其实有个比较好的办法:你就按照你标准的处理,然后遇到异常的就挑出来输出到一个专门的文件,这样电脑处理完了再人脑处理。特殊的应该是比较少的吧
#11
.............
#12
用正则吧
#13
思想:
1、检查有没有:"<",如果有,那么用 “:”替代它。
2、检查有没有 “::” ,如果有,那么用 “:”替代它
3、检查有没有 “-”符号,如果有,那么以它为界限分割字符串,就能得到你想要的两个字符串
1、检查有没有:"<",如果有,那么用 “:”替代它。
2、检查有没有 “::” ,如果有,那么用 “:”替代它
3、检查有没有 “-”符号,如果有,那么以它为界限分割字符串,就能得到你想要的两个字符串
#1
正则式是不是比较好?
#2
分析数据源,找出各种可能性,化归(具体手段例如:替换::为: 以及 替换:<为:)
#3
直接扫描,取出来分析!
#4
Dim strSource As String, strTmp As String
Dim strDates() As String, strItems() As String
Dim i As Integer, j As Integer
strSource = "09:07:29:12:37:41-09:07:29:12:40:42"
strDates = Split(strSource, "-")
If UBound(strDates) <> 1 Then
MsgBox "日期分隔符错误"
Exit Sub
End If
For i = 0 To 1
strItems = Split(strDates(i), ":")
If UBound(strItems) <> 5 Then
MsgBox "第 " & i + 1 & " 个日期内部分隔符错误"
Exit Sub
End If
strTmp = "20" & strItems(0) & "-" & strItems(1) & "-" & strItems(2) & " "
strTmp = strTmp & strItems(3) & ":" & strItems(4) & ":" & strItems(5)
If Not IsDate(strTmp) Then
MsgBox "第 " & i + 1 & " 个日期格式错误"
Else
Debug.Print strTmp
End If
Next i
Dim strDates() As String, strItems() As String
Dim i As Integer, j As Integer
strSource = "09:07:29:12:37:41-09:07:29:12:40:42"
strDates = Split(strSource, "-")
If UBound(strDates) <> 1 Then
MsgBox "日期分隔符错误"
Exit Sub
End If
For i = 0 To 1
strItems = Split(strDates(i), ":")
If UBound(strItems) <> 5 Then
MsgBox "第 " & i + 1 & " 个日期内部分隔符错误"
Exit Sub
End If
strTmp = "20" & strItems(0) & "-" & strItems(1) & "-" & strItems(2) & " "
strTmp = strTmp & strItems(3) & ":" & strItems(4) & ":" & strItems(5)
If Not IsDate(strTmp) Then
MsgBox "第 " & i + 1 & " 个日期格式错误"
Else
Debug.Print strTmp
End If
Next i
#5
时间字符串定义上有点自找麻烦。如果是 "09-07-29 12:37:41>09-07-29 12:40:42" 会省很多事。
#6
一个个字符分析:
Option Explicit
Function GetDateTime(ByVal s As String) As Date
Dim i As Long
Dim tmp
Dim arr(5)
Do
tmp = Mid(s, 1, 1)
If IsNumeric(tmp) And tmp <> 0 Then
tmp = Val(s)
arr(i) = tmp
i = i + 1
End If
s = Replace(s, tmp, "", 1, 1)
Loop While Len(s)
GetDateTime = DateSerial(CInt("20" & arr(0)), arr(1), arr(2)) & _
Space(1) & _
TimeSerial(arr(3), arr(4), arr(5))
End Function
Private Sub Command1_Click()
Dim s As String
Dim i As Long
Dim a
s = "09:07:29:12:37:41-09:07:29:12:40:42"
a = Split(s, "-")
For i = 0 To UBound(a)
Debug.Print GetDateTime(a(i)) & vbTab;
Next
Debug.Print
s = "09::7:29:12:37:41-09:07:29:12:40:42"
a = Split(s, "-")
For i = 0 To UBound(a)
Debug.Print GetDateTime(a(i)) & vbTab;
Next
Debug.Print
s = "09: <7:29:12:37:41-09:07:29:12:40:42"
a = Split(s, "-")
For i = 0 To UBound(a)
Debug.Print GetDateTime(a(i)) & vbTab;
Next
Debug.Print
End Sub
#7
改下:
Function GetDateTime(ByVal s As String) As Date
Dim i As Long
Dim tmp
Dim arr(5)
Do
tmp = Mid(s, 1, 1)
If IsNumeric(tmp) And tmp <> 0 Then
tmp = Val(s)
arr(i) = tmp
i = i + 1
End If
s = Replace(s, tmp, "", 1, 1)
Loop While Len(s)
GetDateTime = DateSerial(CInt("20" & Format(arr(0), "00")), arr(1), arr(2)) & _
Space(1) & _
TimeSerial(arr(3), arr(4), arr(5))
End Function
#8
这里有人找你领钱:http://topic.csdn.net/u/20090724/07/3271eda7-6703-42d4-bee3-6bc53ab156cc.html?35796
#9
呵呵,东方我去了,不过那个东东我不会.....
#10
大家都很日情啊,其实有个比较好的办法:你就按照你标准的处理,然后遇到异常的就挑出来输出到一个专门的文件,这样电脑处理完了再人脑处理。特殊的应该是比较少的吧
#11
.............
#12
用正则吧
#13
思想:
1、检查有没有:"<",如果有,那么用 “:”替代它。
2、检查有没有 “::” ,如果有,那么用 “:”替代它
3、检查有没有 “-”符号,如果有,那么以它为界限分割字符串,就能得到你想要的两个字符串
1、检查有没有:"<",如果有,那么用 “:”替代它。
2、检查有没有 “::” ,如果有,那么用 “:”替代它
3、检查有没有 “-”符号,如果有,那么以它为界限分割字符串,就能得到你想要的两个字符串