If statement for dates

Im having some trouble with a if statement to check for dates. I’m iterating through a list of dates that are formatted like 9/30/20 where there is no 0 is front of the 9 for the month of September or any other month. Im trying to write a if statement that looks for each month of 2020. Any idea how I would write that if statment? the dates will all be the last day of each month. This is the format of the dates "12/31/2020, 11/30/2020, 10/31/2020, 9/30/2020, 8/31/2020, 7/31/2020, 6/30/2020, 5/31/2020, 4/30/2020, 3/31/2020, 2/29/2020, 1/31/2020

@NATHAN_MORA - Could you please elaborate your query ?

So far, this is my understanding
you are iterating through date, one at a time?? right?

Say 1/31/2020 …in a If statement what you would like evaluate against this date??

Right, I have a list with a bunch of dates and i need to click on the dates that are January - December of 2020 like in my post. If contains 1/31/2020 then click or if contains 9/30/2020 then click and so on.

This is what the table im iterating through looks like. Just not sure how i would write the if statement for the dates.

if

Hi @NATHAN_MORA ,

Here you go. :slight_smile:

If Datetime.ParseExact("11/30/2020", "MM/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture).Year.ToString.Equals("2020")

Shraddah,
Thank you for this I will check it tomorrow, could you explain the logic so I can understand?

Hi @NATHAN_MORA ,

Datetime.ParseExact("11/30/2020", "MM/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture) converts your Date String to Date Time like (30/11/2020 0:00:00 AM)

We require year for our condition so we are extracting it using Year method and then we are converting extracted year to String .

Once we have year (String) we check if it equals to “2020”.

Other simple alternative way :

If "11/30/2020".Split("/"c).Last.Equals("2020")

Here we are splitting Date String by “/”, extracting last item (comtains year) from splitted string & checking if it equals iwth “2020”.

NOTE : In both cases, I’m assuming your date is of String data type.

So this will check to to see if the dates I’m iterating through equal or contain every month of 2020?

Yes, both these methods will check if your date belongs to 2020.

One last question, if the date is formatted like 1/21/2020 instead of 01/21/2020 this would still work?

2nd method (Split method) will work.

For 1st method to work:
If Datetime.ParseExact("11/3/2020", "MM/d/yyyy", System.Globalization.CultureInfo.InvariantCulture).Year.ToString.Equals("2020")

Go with 2nd method as you don’t have to work with Datetime data type & it’s more convenient.

Shraddha,
For my if statement i used a get attribute activity first with the result of that get attribute being called resulthref. So for the if statement I’m writing resulthref.tostring.contains(“11/30/2020”.Split("/"c).Last.Equals(“2020”). Im getting a end of expression error. Can you help ?

Hi,
It should be If resulthref.ToString.Split("/"c).Last.Equals("2020")
assuming your date is stored in resulhref variable.

Shraddha,
I think he might need to be a date variable. I have attached a picture of a message box from my get attribute activity. you can see how the date is formatted of the message box in the attached picture. im trying to get the if statement to say “if this attribute has 20 in the year column then” Any ideas?

variblereturn

@NATHAN_MORA ,
Can you extract just date from this text in some variable ?

Let’s say varMyDate = 4/30/20 (d/MM/yy)

If varMyDate.Split("/"c).Last.Equals("20")