2015-05-03 30 views
1

我主要是通過自己學習qtp和vbs我需要qtp/vbs server 2008中的函數,改變dd和mm的位置並將其轉換爲一個數字字符串以便qtp將其插入到測試中的應用程序中,這是我想出的,但該函數在末尾沒有返回任何內容將變量與多個值進行比較並使用「和」&「或」如果「條件vbscript

'5)DATEFORMATFUNC 「功能是獲取當前系統日期,增加了一天,並把它轉換成字符串 」可用作在UAT‘預訂機票’的日期參數 功能DATEFORMATFUNC

'Defining variables 
    Dim currentday, currentmonth, currentyear, oday, omonth, oyear, NextDayDate, LeapYear 

    '* Checking if current year is leap year and transforming answer to numeric variant wher 1=True and 0=False 
    If DatePart("yyyy", Now) mod 4=0 then 
     LeapYear = 1 
     Else LeapYear = 0 
    End If 


    '* getting current day of month 
    currentday = DatePart("d", Now) 
    '* getting current month of year 
    currentmonth = DatePart("m", Now)  
    '* reporting to get it into "test results" 
    Reporter.ReportEvent micDone, "Current month of year is " & currentmonth, currentmonth 

    '* getting last two numbers of current year 
    currentyear = Right((DatePart("yyyy", Now)),2) 
    '* reporting to get it into "test results" 
    Reporter.ReportEvent micDone, "Current year is " & currentyear, currentyear 

    '* If currentday is 31(when 31 days in month) then date becomes the first day of next month 
    If currentmonth = 1 or 3 or 5 or 7 or 8 or 10 and currentday = 31 Then 
     omonth = currentmonth +1 and oday = 1 

    '* If currentday is less then 31(when 31 days in month) then adding 1 day to currentday 
    ElseIf currentmonth = 1 or 3 or 5 or 7 or 8 or 10 or 12 and currentday < 31 Then 
     oday = currentday + 1 

    '* if current day is 31 of december then date becomes first of january next year 
    ElseIf currentmonth = 12 and currentday = 31 Then 
     omonth = 1 and oday = 1 and oyear = currentyear+1 

    '* if current day is 30 (when 30 days in month) then date becomes first day of next month 
    ElseIf omonth = 4 or 6 or 9 or 11 and oday = 30 Then 
     omonth =currentmonth + 1 and oday = 1 

    '* if current day is less then 30 (when 30 days in month) then adding 1 day to current day 
    ElseIf currentmonth = 4 or 6 or 9 or 11 and currentday < 30 Then 
     oday = currentday +1 

    '* if it is leap year and current day is 28 of February then adding 1 day to current day 
    ElseIf currentmonth = 2 and currentday< 29 and LeapYear = 1 Then 
     oday = currentday +1 

    '* if it is leap year and date is 29 of February then date jumps to first of March 
    ElseIf currentmonth = 2 and currentday = 29 and LeapYear = 1 Then 
     oday = 1 and omonth = 3 

    '* if it is not leap year and current day is 28 of February then date becomes first of March 
    ElseIf currentmonth = 2 and currentday = 28 and LeapYear = 0 Then 
     oday = 1 and omonth = currentmonth + 1 

    '* if it is not leap year and current day is less then 28 then adding 1 day to current day 
    ElseIf currentmonth = 2 and currentday < 28 and LeapYear = 0 Then 
     oday = currentday + 1 

    '* End 
    End If 



    'if the day of the month is a one digit number then concatinating "0" before it 
    If oday < 10 then 
     oday = 0 & oday 
    end If 

    'if the month of the year is a one digit number then concatenating "0" before it 
    If omonth < 10 then 
     omonth = 0 & omonth 
    End If 

    'concatinating the parts of the date in a manner that it can be used as a date parameter (mmddyy) for "Flight Reservation" 
    NextDayDate = omonth & oday & oyear 

    Reporter.ReportEvent micDone, "Current date vs changed date", "Current date is: " & Date & " Changed date is: " & NextDayDate 

    'reporting to get it into "test results" 
    Reporter.ReportEvent micDone, "The parameter inserted to Data Table " , DataTable.Value ("Next_Day_Date", dtGlobalSheet) 

    'inserting parameter to data table 
    DataTable.Value ("Next_Day_Date", dtGlobalSheet) = NextDayDate 
End Function 

回答

0

條件像你

If currentmonth = 1 or 3 or 5 or 7 or 8 or 10 and currentday = 31 Then 

不「工作」,因爲VBScript的邏輯運算符的數字做位運算(參見OrLippert):

>> For i = 0 To 1 
>>  If i = 1 Or i = 2 Then 
>>  WScript.Echo i, "is 1 Or 2" 
>>  End If 
>> Next 
>> 
1 is 1 Or 2 

對於i = 0條件計算爲False Or FalseFalse。但在

>> For i = 0 To 1 
>>  If i = 1 Or 2 Then 
>>  WScript.Echo i, "is 1 Or 2" 
>>  End If 
>> Next 
>> 
0 is 1 Or 2 
1 is 1 Or 2 

i = 0False取爲數字0和位進行或運算,以2,其是2,因此True

在添加大量的currentmonth =你的條件語句,考慮DateAdd():

>> dtToday = Date() 
>> WScript.Echo TypeName(dtToday), dtToday 
>> dtTomorrow = DateAdd("d", 1, dtToday) 
>> WScript.Echo TypeName(dtTomorrow), dtTomorrow 
>> 
Date 03.05.2015 ' german locale 
Date 04.05.2015 

>> For Each dtX In Array(#12/31/2014#, #2/28/2012#, #2/28/2013#) 
>>  WScript.Echo dtX, DateAdd("d", 1, dtX) 
>> Next 
>> 
31.12.2014 01.01.2015 ' german locale 
28.02.2012 29.02.2012 
28.02.2013 01.03.2013 

而且到日期格式MMDDYYYY:

>> dtX = Date() 
>> sX = Right(100 + Month(dtx), 2) & Right(100 + Day(dtX), 2) & Year(dtX) 
>> WScript.Echo TypeName(sX), sX 
>> 
String 05032015 
+0

@ Ekkehard.Homer感謝您抽出時間回答我的問題!我正在使用hp qtp,但無法識別WScript.Echo。 –

+0

@GeorgeShtalmann - 如果你願意學**我的答案,你會意識到WScript.Echo只是用於演示/展示。 DateAdd()和VBScript布爾操作的工作與您的問題相關。 –

相關問題