How to calculate the difference between two dates in UiPath

In this article, we are going to discuss calculating the difference between the two dates.

This step is more useful while designing automation for the business process which based on file automation.

In general also when you validate any date to system DateTime and to get the difference between them to get into the next step.

So, let’s dive into the example and see how we can achieve this using UiPath.

Firstly, we need to create two variables that hold the value for Date1 and Date2.

uipath

Here:

  • firstDate – This is our Date1 of variable type string.
  • secondDate – This is our Date2 of variable type string.

Now for calculating the difference between these two dates, we are using a method that is DateDiff.

The syntax for this looks like below:

DateDiff(DateInterval.Day,firstDate,secondDate)

In our example, the syntax looks like below:

uipath

Here:

  • datedifference – This is a variable where it stores the difference between two dates.

In the above syntax, we are looking only for Day difference between the two dates.

There are few more differences that we can achieve by using this syntax those are mentioned below:

  • Day
  • Hour
  • Minute
  • Month
  • Second
  • Year

These are some of the differences that we can get for two dates.

The output looks like below for above example:

uipath

The difference between the two dates is 29 Days for the above-mentioned Dates.

In this example we have taken two string variables.

In order to use this DateDiff method, the two variables should be in DateTime format.

That is the reason why we converted both string variables to DateTime.

If you directly have two DateTime variables then no need of using Convert.ToDateTime for variables.

While using this method lot of times I see people ask about they receive an error while converting a string variable to a DateTime variable using the parse method.

This is also a method for DateTime conversion for a variable.

Happy Learning!

Like this post then let your friends know about this-:

Leave a Reply

Your email address will not be published. Required fields are marked *