# Error when calculate the difference between two dates

**URL:** <https://forum.uipath.com/t/error-when-calculate-the-difference-between-two-dates/739424>\
**Category:** Studio\
**Tags:** activities, system\
**Created:** [May 7, 2024, 2:34pm UTC](https://forum.uipath.com/t/error-when-calculate-the-difference-between-two-dates/739424 "2024-05-07T14:34:48Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ndtran85](https://avatars.discourse-cdn.com/v4/letter/n/a3d4f5/32.png) [@ndtran85](https://forum.uipath.com/u/ndtran85)\
**Post date:** [May 7, 2024, 2:34pm UTC](https://forum.uipath.com/t/error-when-calculate-the-difference-between-two-dates/739424/1 "2024-05-07T14:34:48Z")

</div>

I’m trying to calculate the difference between two dates but I kept getting this error message and couldn’t figure out why. I have been reading from the forum and don’t know what i did wrong. Thank you so much!

 ![image](https://global.discourse-cdn.com/uipath/original/4X/d/b/4/db49cb3bb6843954dc367d851e67ccd77612f9be.png)

 ![image](https://global.discourse-cdn.com/uipath/original/4X/c/5/5/c55540a1fa3e57994bcc5044204a2bed2bcb7c70.png)

---

<div class="post-metadata">

**Author:** ![postwick](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/postwick/32/227083_2.png) [@postwick](https://forum.uipath.com/u/postwick)\
**Post date:** [May 7, 2024, 2:40pm UTC](https://forum.uipath.com/t/error-when-calculate-the-difference-between-two-dates/739424/2 "2024-05-07T14:40:46Z")

</div>

You shouldn’t name a variable DateDiff as DateDiff is a method of the datetime class. Change the name of your variable and see if the error goes away.

---

<div class="post-metadata">

**Author:** ![ndtran85](https://avatars.discourse-cdn.com/v4/letter/n/a3d4f5/32.png) [@ndtran85](https://forum.uipath.com/u/ndtran85)\
**Post date:** [May 7, 2024, 2:51pm UTC](https://forum.uipath.com/t/error-when-calculate-the-difference-between-two-dates/739424/3 "2024-05-07T14:51:46Z")

</div>

Changed it but the error still there

![image](https://global.discourse-cdn.com/uipath/original/4X/0/0/8/0082c12c4a60830dbfac52e42765fa981cd6d1eb.png)

---

<div class="post-metadata">

**Author:** ![treesa.maria](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/treesa.maria/32/562165_2.png) [@treesa.maria](https://forum.uipath.com/u/treesa.maria)\
**Post date:** [May 7, 2024, 2:53pm UTC](https://forum.uipath.com/t/error-when-calculate-the-difference-between-two-dates/739424/4 "2024-05-07T14:53:12Z")

</div>

> [@ndtran85](#):
>
> calculate the difference between two dates

@ndtran85

1. Drag an “Assign” activity onto your workflow.
2. In the “To” field of the Assign activity, create a new variable to store today’s date (let’s call it today). Set the variable type to DateTime.
3. In the “Value” field, use the DateTime.Today property to get today’s date.
4. Drag another “Assign” activity onto your workflow.
5. In the “To” field of the Assign activity, create a new variable to store the other date (let’s call it otherDate). Set the variable type to DateTime.
6. In the “Value” field, use the DateTime.ParseExact method to convert the string representation of the other date to a DateTime object. For example:

**DateTime.ParseExact(“2024-05-10”, “yyyy-MM-dd”, System.Globalization.CultureInfo.InvariantCulture)**

Replace “2024-05-10” with the string representation of your desired date.

1. Drag another “Assign” activity onto your workflow.
2. In the “To” field of the Assign activity, create a new variable to store the difference between the two dates (let’s call it dateDifference). Set the variable type to TimeSpan.
3. In the “Value” field, subtract today from otherDate:

**otherDate - today**

1. Now, you can use the dateDifference variable to extract the desired information, such as total days, total hours, etc., using its properties (e.g., dateDifference.TotalDays, **dateDifference.TotalHours** **, etc.).**

---

<div class="post-metadata">

**Author:** ![ndtran85](https://avatars.discourse-cdn.com/v4/letter/n/a3d4f5/32.png) [@ndtran85](https://forum.uipath.com/u/ndtran85)\
**Post date:** [May 7, 2024, 3:07pm UTC](https://forum.uipath.com/t/error-when-calculate-the-difference-between-two-dates/739424/5 "2024-05-07T15:07:20Z")

</div>

hi @treesa.maria

it works. Thanks so much!

---

<div class="post-metadata">

**Author:** ![postwick](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/postwick/32/227083_2.png) [@postwick](https://forum.uipath.com/u/postwick)\
**Post date:** [May 7, 2024, 3:23pm UTC](https://forum.uipath.com/t/error-when-calculate-the-difference-between-two-dates/739424/6 "2024-05-07T15:23:51Z")

</div>

You don’t have to do all that. It’s creating extra variables you don’t need. It can all be done in one expression the way you originally had it.

Looking back at your original post, you are converting DateTime.Now to string and then converting it back to datetime. This is redundant.

Use this as your expression to assign to NumberofDays.

DateDiff(DateInterval.Day,DateTime.ParseExact(date1,“MM/dd/yyyy”,System.Globalization.CultureInfo.InvariantCulture),Now)

Also if you are only using NumberofDays once (ie in an If condition, entering it into a field, etc) then you don’t need this variable. You can just directly use that expression in the If condition, entering it into the field, etc.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/system/32/182466_2.png) [@system](https://forum.uipath.com/u/system)\
**Post date:** [May 10, 2024, 3:24pm UTC](https://forum.uipath.com/t/error-when-calculate-the-difference-between-two-dates/739424/7 "2024-05-10T15:24:46Z")

</div>

Closed on May 10, 2024, 3:24pm UTC

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.
