VBS异常处理例子

'####################################################################################################################
'此部分为固定初始化,请勿随意修改
On Error Resume Next
Dim result
Dim params()
Dim paramCount
Dim application, SapGuiAuto, connection, session
'获取Input参数
'params(0): 正负天数
'params(1): 格式化
paramCount = WScript.Arguments.Count
If paramCount > 0 Then
    ReDim params(paramCount)
    For i = 0 to paramCount - 1
        params(i) = WScript.Arguments.item(i)
    Next
End If
'####################################################################################################################
'请在此处编写业务逻辑代码
'若需要抛出业务异常,请在result中添加"异常"关键词

distance = params(0)
format = params(1)

targetDate = DateAdd("d", distance, Date)
'获取年月日
strYear = Year(targetDate)
strMonth = Month(targetDate)
If Len(strMonth) < 2 Then
    strMonth = "0" & strMonth
End If
strDay = Day(targetDate)
If Len(strDay) < 2 Then
    strDay = "0" & strDay
End If

'根据格式进行格式化

Select Case format
    Case "yyyyMMdd"
        result = strYear & strMonth & strDay
    Case "yyyy-MM-dd"
        result = strYear & "-" & strMonth & "-" & strDay
    Case "yyyy/MM/dd"
        result = strYear & "/" & strMonth & "/" & strDay
End Select


'####################################################################################################################
'此部分为固定结尾,请勿随意修改
If Err.Number <> 0 Then
'发生运行异常时,将执行以下语句
result = "VBS运行异常: " + Err.Description
Err.Clear
End If

WScript.StdOut.WriteLine result
'####################################################################################################################

    

 

posted @ 2022-04-13 12:18  拷贝达人  阅读(193)  评论(0编辑  收藏  举报