新增文章
文章标题
分类
C#
云星空
K3 BOS
K3 功能
用友
Oracle
python
SQL
MySql
PHP
HTML
script
windows
Access
影视后期
财务
服务
生活
内容
https://blog.csdn.net/qq_35844043/article/details/116745321 VB6使用正则浅谈 二、正则处理日期格式 思路:正则是一个好东西,那么用正则+Format/Format$实现看看 考虑到有些用户电脑正则相关的文件损坏,无法使用正则,所以先封装获取正则对象的方法,如下: Public Function GetRegExpObj() As Object On Error Resume Next '函数说明:获取一个正则表达式对象 '创建作者:Commas '创建时间:2022-04-28 '修改时间: Set GetRegExpObj = CreateObject("vbscript.regexp") If Err <> 0 Then Call MsgBox(Err.Description) Err.Clear Set GetRegExpObj = Nothing End If End Function 再实现日期格式化方法。如果可以获取到正则对象,那么就用正则实现,否则就用“遍历+IsDate(vDate)试错”进行简单尝试,如下: Public Function FmtDateStr(ByVal vDate As Variant, Optional sFormat As String = "YYYY-MM-DD") As String On Error Resume Next '函数说明:将日期或日期字符串格式化输出日期字符串,默认输出格式为“YYYY-MM-DD”,如“2022-04-28” '创建作者:Commas '创建时间:2022-04-28 '修改时间: '------数据格式说明------ 'vDate:需要格式化的日期或作日期字符串,如“星期四 2022.04.28 12:15:59”、“2022_04_28 12:15:59 星期四” 'sFormat:预期的日期字符串格式,默认为“YYYY-MM-DD” '------数据格式说明------ If TypeName(vDate) = "Date" Then FmtDateStr = Format$(vDate, sFormat) Else Dim re As Object Set re = GetRegExpObj() If re Is Nothing Then '用于兼容电脑正则对象受损的处理过程,简单处理,适用于“日期 星期几”或“星期几 日期”的格式 Dim aryDate() As String, aryTemp() As String, i As Long, j As Long aryDate = Split(vDate, " ") aryTemp = Split("\@.@_@:", "@") For i = 0 To UBound(aryDate) vDate = aryDate(i) For j = 0 To UBound(aryTemp) vDate = Replace(vDate, aryTemp(j), "-") Next j If IsDate(vDate) Then Exit For End If Next i If Not IsDate(vDate) Then FmtDateStr = "" Exit Function End If Else re.Global = True re.IgnoreCase = False '设置是否匹配大小写 re.Pattern = "(\d{4}\D\d{1,2}\D\d{1,2})" Dim clnM As Object Set clnM = re.Execute(vDate) vDate = clnM.Item(0).Value re.Pattern = "\D" vDate = re.Replace(vDate, "-") End If FmtDateStr = Format$(CDate(vDate), sFormat) End If End Function 接着我们来看看实现的效果吧,如下: Option Explicit Private Sub Form_Load() Debug.Print "0" Debug.Print FmtDateStr("2022\04\28 星期三") Debug.Print FmtDateStr("星期三 2022\04\28") Debug.Print "1" Debug.Print FmtDateStr("2022/04/28 星期三") Debug.Print FmtDateStr("星期三 2022/04/28") Debug.Print "2" Debug.Print FmtDateStr("2022-04-28 星期三") Debug.Print FmtDateStr("星期三 2022-04-28") Debug.Print "3" Debug.Print FmtDateStr("2022.04.28 星期三") Debug.Print FmtDateStr("星期三 2022.04.28") Debug.Print "4" Debug.Print FmtDateStr("2022_04_28 星期三") Debug.Print FmtDateStr("星期三 2022_04_28") Debug.Print "5" Debug.Print FmtDateStr("2022/4/28 星期三") Debug.Print FmtDateStr("星期三 2022/4/28") Debug.Print "6" Debug.Print FmtDateStr("2022-4-28 星期三") Debug.Print FmtDateStr("星期三 2022-4-28") Debug.Print "7" Debug.Print FmtDateStr("2022.4.28 星期三") Debug.Print FmtDateStr("星期三 2022.4.28") Debug.Print "8" Debug.Print FmtDateStr("2022_4_28 星期三") Debug.Print FmtDateStr("星期三 2022_4_28") Debug.Print "9" Debug.Print FmtDateStr("星期四 2022-04-28 12:15:59") Debug.Print FmtDateStr(Now) End Sub 运行结果: 0 2022-04-28 2022-04-28 1 2022-04-28 2022-04-28 2 2022-04-28 2022-04-28 3 2022-04-28 2022-04-28 4 2022-04-28 2022-04-28 5 2022-04-28 2022-04-28 6 2022-04-28 2022-04-28 7 2022-04-28 2022-04-28 8 2022-04-28 2022-04-28 9 2022-04-28 2022-04-28 无论是正则,还是非正则处理,都能顺利完成预期结果的工作
返回
保存