Get Current, Previous, and Next Year in UiPath

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.

create a new variable to get the current date in the Assign activity

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
assigning current year with the value

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

add write line activity to print current year on console

Run the process and see the output in the console.

current year displayed on the output console panel

Get Previous Year in UiPath

Add another Assign activity. Create a new variable previousYear with an Integer data type.

add assign activity for previous year

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
assign value to previous year

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

add writeline activity to print the previous year value

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

console output showing current and previous year values

Get Next Year in UiPath

Add another Assign activity. Create a new variable nextYear with an Integer data type.

add assign activity for next year

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
assign value to next year

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

add write line activity to print the next year value

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

show all the 3 values on the output console that is current, previous and next year using UiPath

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.

Thank you All!!! Hope you find this useful.


Up ↑

%d bloggers like this: