How to get number of days subtracted between two strings of dates?

Hi. I am trying to subtract the days between two strings that has the format of dd/MM/yyyy in both variables. Both variables types are String. I have tried assigning a new variable under the type of Int32 with DateDiff(DateInterval.Day, Date1, Date2) and it returned the error

An example
Date1 = 10/02/2021
Date2 = 22/12/2020

What is the best way to go forward in order to get the number of days subtracted between the two dates strings?

Refer this

Hi, thank you for the above links. I tried these (the dates are in a variable so is not hardcoded as shown in the above links) and no joy as it keeps giving the same error message as shown in the screen shot of original post. Any idea where I may be going wrong?

Once try with this syntax


Hi…thank you for providing the syntax. For some reason it’s not accepting as is still returning the same error

What is the variable type of DaysSubrtracted ?

Refer this

Try to make the variables standard like string or datetime and import some namespace by referring the above solution.

I had it down for Int64 and also Int32 and no joy

Whats the both date vairable type ?

They are both in String variable type because the values inside both variables are in String for example like

NowDate =“01/01/2021”
CreatedRequestDate =“10/02/2021”

change DaysSubrtracted variable type to String

1 Like

Thank you, I changed the variable to String aswell and now it’s returning a different error this time, would I be missing another command?

one of you variable (NowDate or CreatedDate) is not in proper format. Show exact value stored in variables

In the output window they seem to be showing in the correct format


For the NowDate variable, the value stored is as Date.Now.addDays(-14).toString(“dd/MM/yyyy”) as I need the date from 14 days prior to today

For the RequestDateCreated is extracted from a table where the value will then be like “18/11/2020 16:19:03”
and to get the date only the value is outputted to System.Text.RegularExpressions.Regex.Matches(CreatedRequestDate,"\d{2}/\d{2}/\d{4}")(0).toString where it will output the value “18/11/2020” then

assign variable like below:

NowDate = Date.Now.addDays(-14).ToString("ddMMMyyyy")
RequestDateCreated  = Date.ParseExact("18/11/2020 16:19:03","dd/MM/yyyy HH:mm:ss",Nothing).ToString("ddMMMyyyy")

you are getting this error because the date format of date in variable is dd/MM/yyyy and your system date format is different (might be MM/dd/yyyy). When we use CDate(), it uses system date format as reference to convert the string to date. When function tried to convert “27/01/2021” string to date, it tried to convert “27” as month which is invalid month hence error.

That did the job, thank you so much for your help :)!

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