Date in string: Check if date within previous x years

Hi

I have a date in string
Edate=2018.Jul

Example:
Now is Dec 2020. I want to check if Edate is within 2018-2019, but exclude Dec 2020

How do I do so?

Thanks!

Hi @Anonymous2

Convert the string variable into datetime variable and then simple relation operators will do your job.

Thanks & Regards,
Anand Jaiswal

1 Like

Hi

I have never worked with datetime variable. Could u provide concrete steps to convert to datetime variable and the relation operators to do the test?

Thank you

Hi @Anonymous2,

Try something like this:
String D1 = “10-Jul-2018”
DateTime D2 = Cdate(D1)
if D2>=01-Jan-2018 and D2<=31-Dec-2019

Thanks & Regards,
Anand Jaiswal

1 Like

Hi,

My date string is strictly in this format:
2018.Jul

And Comparison dates are also in same format?

Hi

I guess I can use some manipulation to convert
Edate=2018.Jul
into
test=Cdate(“01-Jul-2018”)

But the current year I want to compare always vary
year= System.Date.Now.Year.ToString

If condition
test>=01-Jan-(year-2) and test<31-Dec-year

The if condition doesn’t work. Uipath state Jan & Dec are not declared variables. It can’t recognise “01-Jan-(year-2)” and “01-Dec-year” as dates

Thank you

Hi @Anonymous2,

test = Cdate(Edate) will also work.

Are you putting condition within double quotes? If yes then I think you need to convert those strings into datetime variable as well.

Hi

“01-Jan-(year-2)” and “01-Dec-year” don’t work even if there are not within double quotation in the IF condition

I tried converting to date
startdate=Cdate(“01-Jan-(year-2)”)
but I get the error that I can’t convert from string to date.

Thank you

Hi,

I think Year is variable here, Don’t pass year within double quotes.

Try like this: “01-Jan”+year or in the same fashion where you have used variable as year

Thanks,

Anand

1 Like