Get the current, previous, and next year in UiPath and print them on the console. We will do calculations in integer and print in string.
Prerequisites
Get Current Year in UiPath
Add a new Assign
activity. Create a new variable currentYear
of Integer data type. To create a new variable, just click Cntrl+K on the keyboard.

You will be seeing an error on the other side of the Assign activity because we have not initialized the variable with a value. Assign the currentYear
variable with the below value.
DateTime.Now.Year

Add a new Write Line
activity. This is used to print the current year string value on the output console.

Run the process and see the output in the console.

Get Previous Year in UiPath
Add another Assign
activity. Create a new variable previousYear
with an Integer data type.

Assign the previousYear
with the below value. Based on the logic, current year – 1 will give me the previous year’s value. Here also, we use the same logic.
DateTime.Now.AddYears(-1).Year

Add a new Write Line activity and print the previous year’s value on the console.

Run the process and see both the current and previous year values on the output console.

Get Next Year in UiPath
Add another Assign
activity. Create a new variable nextYear
with an Integer data type.

Assign the nextYear
with the below value. Based on the logic, the current year + 1 will give me the next year’s value. Here also, we use the same logic.
DateTime.Now.AddYears(1).Year

Add Write Line activity and run the process to print the next year’s value.

In the output console, we can see all the 3 values i.e., current, previous, and next year.

Improvements
Instead of writing the formulas and calling the DateTime again and again, you can store the current date in a variable of data type DateTime. Use this variable to add the years respectively. This is a cleaner and better way to do it.
I have shown you 3 different individual components so that they are independent of each other and can be reused anywhere in the code without passing extra variables as arguments for multi flows.
You must be logged in to post a comment.