# Get difference between two dates?

**URL:** <https://forum.uipath.com/t/get-difference-between-two-dates/3965>\
**Category:** Help\
**Tags:** studio\
**Created:** [April 27, 2017, 11:14am UTC](https://forum.uipath.com/t/get-difference-between-two-dates/3965 "2017-04-27T11:14:34Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![MGDEL](https://avatars.discourse-cdn.com/v4/letter/m/35a633/32.png) [@MGDEL](https://forum.uipath.com/u/MGDEL)\
**Post date:** [April 27, 2017, 11:14am UTC](https://forum.uipath.com/t/get-difference-between-two-dates/3965/1 "2017-04-27T11:14:34Z")

</div>

I have two dates (using Now), wherein I want to get difference between them? How can I achieve that?

What would be the variable to save Now ???

---

<div class="post-metadata">

**Author:** ![vvaidya](https://avatars.discourse-cdn.com/v4/letter/v/e56c9b/32.png) [@vvaidya](https://forum.uipath.com/u/vvaidya)\
**Post date:** [April 27, 2017, 11:17am UTC](https://forum.uipath.com/t/get-difference-between-two-dates/3965/2 "2017-04-27T11:17:30Z")

</div>

double dayDiff = (datetimeStart - datetimeEnd).TotalDays

---

<div class="post-metadata">

**Author:** ![MGDEL](https://avatars.discourse-cdn.com/v4/letter/m/35a633/32.png) [@MGDEL](https://forum.uipath.com/u/MGDEL)\
**Post date:** [April 27, 2017, 12:11pm UTC](https://forum.uipath.com/t/get-difference-between-two-dates/3965/3 "2017-04-27T12:11:02Z")

</div>

Thanks, I keep getting these error - double dayDiff = (Excel\_Date - Now).TotalDays. where Excel\_Date is variable which I am picking from datatable (while converting it as Convert.ToDateTime(rows(“Time of upload”).ToString)).

The error which I receive is  
 ![](https://global.discourse-cdn.com/uipath/original/2X/0/06fe6f9b4906631c38177e4c63a569aa142a7ff8.png)

---

<div class="post-metadata">

**Author:** ![vvaidya](https://avatars.discourse-cdn.com/v4/letter/v/e56c9b/32.png) [@vvaidya](https://forum.uipath.com/u/vvaidya)\
**Post date:** [April 27, 2017, 12:31pm UTC](https://forum.uipath.com/t/get-difference-between-two-dates/3965/4 "2017-04-27T12:31:42Z")

</div>

Please find the attached code

[dateDiff.xaml](https://global.discourse-cdn.com/uipath/original/2X/4/435b072ed972ca21aa74110a8783b8b31cc3e3e7.xaml) (6.6 KB)

---

<div class="post-metadata">

**Author:** ![MGDEL](https://avatars.discourse-cdn.com/v4/letter/m/35a633/32.png) [@MGDEL](https://forum.uipath.com/u/MGDEL)\
**Post date:** [April 27, 2017, 12:47pm UTC](https://forum.uipath.com/t/get-difference-between-two-dates/3965/5 "2017-04-27T12:47:53Z")

</div>

Worked like a charm. Thank you @vvaidya. You have been best.

Note:- Didn’t know Double was variable type 😛

---

<div class="post-metadata">

**Author:** ![vvaidya](https://avatars.discourse-cdn.com/v4/letter/v/e56c9b/32.png) [@vvaidya](https://forum.uipath.com/u/vvaidya)\
**Post date:** [April 27, 2017, 12:50pm UTC](https://forum.uipath.com/t/get-difference-between-two-dates/3965/6 "2017-04-27T12:50:24Z")

</div>

For future use

> **[Data Type Summary - Visual Basic](https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/data-types/?redirectedfrom=MSDN)**
>
> Learn more about: Data Type Summary (Visual Basic)

---

<div class="post-metadata">

**Author:** ![prajakta](https://avatars.discourse-cdn.com/v4/letter/p/ba8739/32.png) [@prajakta](https://forum.uipath.com/u/prajakta)\
**Post date:** [October 12, 2017, 8:43am UTC](https://forum.uipath.com/t/get-difference-between-two-dates/3965/7 "2017-10-12T08:43:29Z")

</div>

Hello,

My problem is I want to compare if the date is a future date or not.  
Hence,I have tried this solution .  
i understood that Excel\_date is a variable.So i am trying with different dates and it is giving me inconsistent results.  
Please confirm me, if I am missing anything?

Excel\_date= 12-10-2017  
then it should give me difference as 0  
But the result is -59

---

<div class="post-metadata">

**Author:** ![aksh1yadav](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/aksh1yadav/32/135359_2.png) [@aksh1yadav](https://forum.uipath.com/u/aksh1yadav)\
**Post date:** [October 12, 2017, 9:13am UTC](https://forum.uipath.com/t/get-difference-between-two-dates/3965/8 "2017-10-12T09:13:25Z")

</div>

> [@prajakta](#):
>
> Excel\_date= 12-10-2017
> 
> then it should give me difference as 0
> 
> But the result is -59

Hey @prajakta

well this is due to the date format you are passing.

on your system it might be like MM/dd/yyyy(10/12/2017) better to check date format on your system **write line**  **Now.Date.Tostring** and check.  
and from your excel you are getting like this 12-10-2017 i.e so format mismatching so you are getting wrong or negative result.

if you will do like this it will return you 0.

**Convert.ToString((Now.Date - Now.Date).TotalDays)** // write line it.

**Convert.ToString((Convert.ToDateTime(“10/12/2017”)- Convert.ToDateTime(“10/12/2017”)).Days)** //write line it.

this will also return you the 0 days.

**Note-** you were noticing why i am using **.Days** in the second line.  
well if you will use **TotalDays** Extension method it will return result as a **double** and if you will use **.Days** Extension method it will return you result as an Integer.Both results will be same just the return type is different.  
IMO better to use int instead of double 🙂

---

<div class="post-metadata">

**Author:** ![prajakta](https://avatars.discourse-cdn.com/v4/letter/p/ba8739/32.png) [@prajakta](https://forum.uipath.com/u/prajakta)\
**Post date:** [October 16, 2017, 9:05am UTC](https://forum.uipath.com/t/get-difference-between-two-dates/3965/9 "2017-10-16T09:05:04Z")

</div>

Thank you yes it works 🙂  
I have another question,  
Can you please share the program where the date which i have to compare if it is in future is in( day- Month name-year) format  
e.g. 01 jan 2018

and my system date is in mm/dd/yyyy format.

Thank you in advance!!

---

<div class="post-metadata">

**Author:** ![aksh1yadav](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/aksh1yadav/32/135359_2.png) [@aksh1yadav](https://forum.uipath.com/u/aksh1yadav)\
**Post date:** [October 16, 2017, 10:22am UTC](https://forum.uipath.com/t/get-difference-between-two-dates/3965/10 "2017-10-16T10:22:42Z")

</div>

> [@prajakta](#):
>
> Can you please share the program where the date which i have to compare if it is in future is in( day- Month name-year) format
> 
> e.g. 01 jan 2018

From above i have got as per my understanding that you wanna compare the future date having format like **01 jan 2018** with current date to know either it is a future date or past date.

Yes you can do this.

if your application is returning a date in a string format then you can use **DateTime.ParseExact** to convert first your date i.e. **01 jan 2018** to your system date format(Date.now.date).

`Datetime converted_date = DateTime.ParseExact("01 jan 2018","dd MMM yyyy",Globalization.CultureInfo.InvariantCulture)`

// Above will return **01/01/0001 00:00:00** (MM/dd/yyyy)

and if your application system is returning already a datetime format 🙂 then you just no need to convert to get date in desired format just use

`Datetime converted_date = your_returend_datetime_var.ToString("MM/dd/yyyy")`

and to check if returned date is a future date or not you can use like below simply.

```
if (converted_date > DateTime.Now)
   {
   // It's a future Date..!!
   }

```

> [@prajakta](#):
>
> Can you please share the program

For your Reference a sample, what you asked for - [Prajakta Future Date Comparison Sample.xaml](https://global.discourse-cdn.com/uipath/original/2X/0/01369e1ad4bdbff71216835b304f1e545e88dece.xaml) (8.9 KB)

Regards…!!  
Aksh

---

<div class="post-metadata">

**Author:** ![prajakta](https://avatars.discourse-cdn.com/v4/letter/p/ba8739/32.png) [@prajakta](https://forum.uipath.com/u/prajakta)\
**Post date:** [October 16, 2017, 11:01am UTC](https://forum.uipath.com/t/get-difference-between-two-dates/3965/11 "2017-10-16T11:01:39Z")

</div>

appreciate your time and efforts 🙂 thank you very much

---

<div class="post-metadata">

**Author:** ![BikashPanigrahi](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@BikashPanigrahi](https://forum.uipath.com/u/BikashPanigrahi)\
**Post date:** [October 16, 2017, 11:02am UTC](https://forum.uipath.com/t/get-difference-between-two-dates/3965/12 "2017-10-16T11:02:02Z")

</div>

(EndDate.Date - StartDate.Date).Days

---

<div class="post-metadata">

**Author:** ![Short](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/short/32/177411_2.png) [@Short](https://forum.uipath.com/u/Short)\
**Post date:** [October 20, 2017, 4:53pm UTC](https://forum.uipath.com/t/get-difference-between-two-dates/3965/13 "2017-10-20T16:53:41Z")

</div>

Hi @vvaidya

On the back of this, I downloaded your code and tried to amend it so it wasn’t against today’s date but a different date.

I amended the endDate to “DateTime.Parse(strEndDate)” which was copied from your startDate, but I keep getting this error:

 ![DateError](https://global.discourse-cdn.com/uipath/original/2X/8/84ee50347b36abb9b7a082185f11ea6ed3e8e55f.jpg)

It stops at the endDate assign activity.

I recreated it in a different project and get the error on the startDate, so bascially whichever one I’ve created, it fails at.

Am I doing something incorrectly?

Thanks!

---

<div class="post-metadata">

**Author:** ![ClaytonM](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/claytonm/32/555957_2.png) [@ClaytonM](https://forum.uipath.com/u/ClaytonM)\
**Post date:** [October 20, 2017, 5:01pm UTC](https://forum.uipath.com/t/get-difference-between-two-dates/3965/14 "2017-10-20T17:01:12Z")

</div>

Hi Short, I think you need to specify the format so it can Parse the date correctly.  
Try this instead:  
**DateTime.ParseExact(“20/10/2017”,“dd/MM/yyyy”,System.Globalization.CultureInfo.InvariantCulture)**

---

<div class="post-metadata">

**Author:** ![Short](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/short/32/177411_2.png) [@Short](https://forum.uipath.com/u/Short)\
**Post date:** [October 23, 2017, 10:19am UTC](https://forum.uipath.com/t/get-difference-between-two-dates/3965/15 "2017-10-23T10:19:55Z")

</div>

Amazing, thank you!

---

<div class="post-metadata">

**Author:** ![1vivek12](https://avatars.discourse-cdn.com/v4/letter/1/ea5d25/32.png) [@1vivek12](https://forum.uipath.com/u/1vivek12)\
**Post date:** [December 18, 2017, 11:06am UTC](https://forum.uipath.com/t/get-difference-between-two-dates/3965/16 "2017-12-18T11:06:47Z")

</div>

Hi Guys need some help  
Logic is like this  
take value of current date suppose 12/18/2017 in mm/dd/yy  
then take yesterday date ie . 12/17/2017  
then create a string as 12/17/2017 12:00:00 12 AM;12/16/2017 12:00:00 AM  
the string should include upto previous 15 dates  
Please help

---

<div class="post-metadata">

**Author:** ![1vivek12](https://avatars.discourse-cdn.com/v4/letter/1/ea5d25/32.png) [@1vivek12](https://forum.uipath.com/u/1vivek12)\
**Post date:** [December 18, 2017, 11:07am UTC](https://forum.uipath.com/t/get-difference-between-two-dates/3965/17 "2017-12-18T11:07:53Z")

</div>

Hi Guys need some help  
Logic is like this  
take value of current date suppose 12/18/2017 in mm/dd/yy  
then take yesterday date ie . 12/17/2017  
then create a string as 12/17/2017 12:00:00 12 AM;12/16/2017 12:00:00 AM  
the string should include upto previous 15 dates  
Please help  
@Mr_JDavey

---

<div class="post-metadata">

**Author:** ![aksh1yadav](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/aksh1yadav/32/135359_2.png) [@aksh1yadav](https://forum.uipath.com/u/aksh1yadav)\
**Post date:** [December 18, 2017, 11:32am UTC](https://forum.uipath.com/t/get-difference-between-two-dates/3965/18 "2017-12-18T11:32:56Z")

</div>

> [@1vivek12](#):
>
> Logic is like this
> 
> take value of current date suppose 12/18/2017 in mm/dd/yy
> 
> then take yesterday date ie . 12/17/2017
> 
> then create a string as 12/17/2017 12:00:00 12 AM;12/16/2017 12:00:00 AM
> 
> the string should include upto previous 15 dates
> 
> Please help

Hey @1vivek12

Please find the attached sample for you and let me know 🙂  
[Get Last 15 Days datetime.xaml](https://global.discourse-cdn.com/uipath/original/2X/5/5f5adcc3bb335e9b7374203f2a4fd108372d4f6e.xaml) (8.0 KB)

output  
12/18/2017 12:00:00 AM; 12/17/2017 12:00:00 AM; 12/16/2017 12:00:00 AM; 12/15/2017 12:00:00 AM; 12/14/2017 12:00:00 AM; 12/13/2017 12:00:00 AM; 12/12/2017 12:00:00 AM; 12/11/2017 12:00:00 AM; 12/10/2017 12:00:00 AM; 12/09/2017 12:00:00 AM; 12/08/2017 12:00:00 AM; 12/07/2017 12:00:00 AM; 12/06/2017 12:00:00 AM; 12/05/2017 12:00:00 AM; 12/04/2017 12:00:00 AM

Regards…!!  
Aksh

---

<div class="post-metadata">

**Author:** ![1vivek12](https://avatars.discourse-cdn.com/v4/letter/1/ea5d25/32.png) [@1vivek12](https://forum.uipath.com/u/1vivek12)\
**Post date:** [December 18, 2017, 11:43am UTC](https://forum.uipath.com/t/get-difference-between-two-dates/3965/19 "2017-12-18T11:43:24Z")

</div>

Thanks a lot buddy.almost got it

But like today date is 12/18/2017 “Current Time” then i want output like this

time should be only 12:00:00 AM

12/17/2017 12:00:00 AM; 12/16/2017 12:00:00 AM; 12/15/2017 12:00:00 AM; 12/14/2017 12:00:00 AM; 12/13/2017 12:00:00 AM; 12/12/2017 12:00:00 AM; 12/11/2017 12:00:00 AM; 12/10/2017 12:00:00 AM; 12/09/2017 12:00:00 AM; 12/08/2017 12:00:00 AM; 12/07/2017 12:00:00 AM; 12/06/2017 12:00:00 AM; 12/05/2017 12:00:00 AM; 12/04/2017 12:00:00 AM; 12/03/2017 12:00:00 AM

And also what u have used can u help with that.

---

<div class="post-metadata">

**Author:** ![aksh1yadav](https://sea2.discourse-cdn.com/uipath/user_avatar/forum.uipath.com/aksh1yadav/32/135359_2.png) [@aksh1yadav](https://forum.uipath.com/u/aksh1yadav)\
**Post date:** [December 18, 2017, 11:44am UTC](https://forum.uipath.com/t/get-difference-between-two-dates/3965/20 "2017-12-18T11:44:02Z")

</div>

> [@aksh1yadav](#):
>
> Hey @1vivek12
> 
> Please find the attached sample for you and let me know 🙂
> 
> Get Last 15 Days datetime.xaml (8.0 KB)

check the updated example upload in the same post 😉

[Next page](https://forum.uipath.com/t/get-difference-between-two-dates/3965.md?page=2)
