Get difference between two dates?

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

1 Like

Please find the attached code

dateDiff.xaml (6.6 KB)

3 Likes

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

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

For future use

1 Like

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

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 :slight_smile:

3 Likes

Thank you yes it works :slight_smile:
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!!

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 :slight_smile: 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..!!
   }

For your Reference a sample, what you asked for - Prajakta Future Date Comparison Sample.xaml (8.9 KB)

Regards…!!
Aksh

1 Like

appreciate your time and efforts :slight_smile: thank you very much

(EndDate.Date - StartDate.Date).Days

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:

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!

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)

2 Likes

Amazing, thank you!

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

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

Hey @1vivek12

Please find the attached sample for you and let me know :slight_smile:
Get Last 15 Days datetime.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

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.

check the updated example upload in the same post :wink:

the first post was almost correct
see my computer format is est 12/18/2017

now output which i want is from yesterday date with time be 12:00:00 AM (you can consider this as a string)

therefore output i want is this:

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

dude that was just a minor format change :slight_smile:
anyways see this : Get Last 15 Days datetime.xaml (8.0 KB)

Regards…!!
Aksh