Topic Options
#57732 - 11/04/20 04:21 PM VB if statement not working
Sami786 Offline
OL Expert

Registered: 01/29/14
Posts: 393
Loc: Home
Hi, Can someone look at my code and let me know how to fix it please.

I'm comparing two dates to set a expired flag.

11\11\2020 = Day_A, Month_A
28\11\2020 = Day_B, Month_B

I need to figure out the Expired and Not_Expired flag. If B is bigger the A then it should be set to Not_Expired, else it's Expired.

if ( Month_B > Month_A ) or ( Month_B = Month_A ) and (Day_B > Day_A) then
Flag = "Not_Expired"
Else
Flag = "Expired"
End IF
End IF

Watch.SetVariable "DateFlag", Flag

DateFlag should be showing "Not_Expired" but it's showing Expired! I wonder if if you can have or and operators in the same if statement, how would you do this if you were me smile

your help is appreciated.
THANKS
_________________________
Peace

Top
#57733 - 11/04/20 04:50 PM Re: VB if statement not working [Re: Sami786]
Jean-Cédric Offline
OL Expert

Registered: 10/03/16
Posts: 650
Loc: Québec, Canada
if ( Month_B > Month_A ) or (( Month_B = Month_A ) and (Day_B > Day_A)) then
Flag = "Not_Expired"
Else
Flag = "Expired"
End IF
End IF


Edited by Jean-Cédric (11/04/20 04:52 PM)
_________________________
♪♫♪♫
99 frigging bugs in my code
99 frigging bugs
Take one down
Code around
127 frigging bugs in my code
♪♫♪♫

Top
#57734 - 11/04/20 05:08 PM Re: VB if statement not working [Re: Sami786]
jim3108 Online   content
OL Expert

Registered: 04/19/10
Posts: 311
Loc: London, UK
You could do this with DateDiff in VBScript.

Code:
' Script to check difference in two dates
' J. Stacey (Point Zero Solutions)
' 2020-11-04
'
'

Option Explicit

Dim date1, date2, dayDiff, monthDiff
Dim sFlag

date1 = "11/11/2020"
date2 = "28/11/2020"

dayDiff = DateDiff("d", date1, date2)
monthDiff = DateDiff("m", date1, date2)

If ((monthDiff > 0) Or ((monthDiff = 0) And (dayDiff > 0))) Then
   sFlag = "Expired"
   Watch.Log "Month difference: " & DateDiff("m", date1, date2), 2
   Watch.Log "Day difference: " & DateDiff("d", date1, date2), 2
Else
   sFlag = "Not_Expired"
   Watch.Log "Not expired", 2
End If

Watch.SetVariable "DateFlag", sFlag
Watch.Log "%{DateFlag} set to: " & sFlag, 2

If you want to load the dates from the workflow, change the variable assignments to:
Code:
date1 = Watch.GetVariable("Date1")
date2 = Watch.GetVariable("Date2")

Hope this helps.

Top
#57735 - 11/05/20 09:28 AM Re: VB if statement not working [Re: Sami786]
Sami786 Offline
OL Expert

Registered: 01/29/14
Posts: 393
Loc: Home
Thank you Jean and Jim,
they both working as expected with many thanks!
_________________________
Peace

Top
#57741 - 11/06/20 03:04 PM Re: VB if statement not working [Re: Sami786]
Sami786 Offline
OL Expert

Registered: 01/29/14
Posts: 393
Loc: Home
Hi Jim, I'm using your script and would like to save the results of the below line into two local venerable, but i'm failing. can you please help me to derive the values into a variable instead of the message box, so this way I can use a different letter head due to different dates.

Watch.Log "Month difference: " & DateDiff("m", date1, date2), 2
Watch.Log "Day difference: " & DateDiff("d", date1, date2), 2

I tried but it's failing

Watch.SetVariable "Day_Difference", DateDiff("m", date1, date2), 2
Watch.SetVariable "Month_Difference", DateDiff("m", date1, date2), 2
_________________________
Peace

Top
#57744 - 11/06/20 03:51 PM Re: VB if statement not working [Re: Sami786]
Sami786 Offline
OL Expert

Registered: 01/29/14
Posts: 393
Loc: Home
I got it smile


Watch.SetVariable "Day_Difference", DayDiff
Watch.SetVariable "Month_Difference", monthDiff

Thanks
_________________________
Peace

Top