You are viewing limited content. For full access, please sign in.

Question

Question

Subtracting Dates with Date Token Calculator

asked on January 28, 2014

 

Hey Everybody:

 

I'm currently looking to subtract two dates from each other and generate the difference in days. so for example:

 

1/12/2014 - 1/10/2014 = a difference of 2 days.

 

I know that this is feasible using the "token editor", but I cant figure out exactly how to format it.

 

does anyone have experience in this?

 

Tony

 

 

1 0

Answers

APPROVED ANSWER
replied on January 27, 2016

As of version 10, the Token Calculator activity includes a DateDiff function that can calculate the difference between 2 dates and return it in the specified unit (years, months or days).

2 0
SELECTED ANSWER
replied on January 28, 2014

Yes, you have to explicitly tell the script to resolve the 2 date tokens from the Retrieve Field Values activity. Try something like this:

 

            Dim days as Integer
            Dim dateA as DateTime = GetTokenValue("RetrieveFieldValues_Date1")
            Dim dateB as DateTime = GetTokenValue("RetrieveFieldValues_Date2")
            days = (dateA-dateB).Days
            SetTokenValue("days",days)

 

8 0

Replies

replied on January 28, 2014

None of the built-in activities can subtract 2 dates. It is not possible in the token editor either. Like Mike says, a script can do it.

2 0
replied on February 7, 2014

Hello everyone,

 

For those of you who need the time difference in terms of minutes, here's Miruna's code I have adapted:

 

 

            Dim duration as Integer
            Dim dateA as DateTime = GetTokenValue("token name:left argument")
            Dim dateB as DateTime = GetTokenValue("token name:right argument")
            duration = (dateA - dateB).Days * 3600 + (dateA - dateB).Hours * 60 + (dateA - dateB).Minutes
            SetTokenValue("output token name",duration)

2 0
replied on February 7, 2014

I think you calculation is slightly off for days (24 hours * 60 minutes = 1440, not 3600)

0 0
replied on January 28, 2014

Alternatively, you can use the Script activity and do the calculation directly with code:

 

int days = (DateTimeA - DateTimeB).Days

 

1 0
replied on January 28, 2014

Using the "Assign Token Values" activity, you can separate out the different values the dates and then do individual calculations. You will need to do some logic there for if the years are different but you should be well on your way to getting this information once you have these values.

0 0
replied on January 28, 2014

Hey Ken,

 

thanks for the quick response.

 

I'm still struggling a bit with this concept. Should I assign both dates to a multi value token and do the calculation?

 

thanks in advance.

 

Tony

0 0
replied on January 28, 2014

No, I do not think that would work. I was recommending that you try separating out the day, month and year for each of the dates you have and put them into tokens for ease of use. Then calculate the individual differences between each and use this information to find out how many days have been between the two.

0 0
replied on January 28, 2014

I'll give it a spin... let you know how it goes, thanks!!

 

Tony

0 0
replied on January 28, 2014

Thanks for the feedback.

 

I'm rather new to vb.NET, can I please see a snippet of the code as to what it should look like? I will copy in an example of what I currently have - I do believe I'm missing some information. I believe that it has something to do with the assigning of the generated value to a token.

 

FYI -

 

Public Class Script1

Inherits ScriptClass90

'''<summary>

'''This method is run when the activity is performed.

'''</summary>

Protected Overrides Sub Execute()

'int days = ("RETRIEVEFIELDVALUES_DATE1" - "RETRIEVEFIELDVALUES_DATE2").Days.

End Sub

End Class

End Namespac

 

 

0 0
replied on January 28, 2014

100% perfect. worked like a charm.

 

Thank you.

 

 

Tony

0 0
replied on February 7, 2014

Hi Mirunda:

 

Thank you for this, much appreciated.

 

Tony

0 0
replied on February 7, 2014 Show version history

Late post to this thread.  For those that do not want to use the script activity I do have a custom workflow activity that will accept two datetime values, determine their difference, and provide tokens for number of milliseconds, seconds, minutes, hours, days, business days, weeks, calendar weeks, months, and/or years between them.  It is a free custom workflow activity, available at www.qfiche.com  on the Products page

0 0
replied on January 27, 2016

hello Cliff

provided link not working 

 

Thank You.

0 0
replied on January 27, 2016
0 0
replied on January 27, 2016

Best to visit www.qfiche.com  and then navigate to the Products page...

0 0
replied on November 27, 2023 Show version history

If anyone is still looking for another version of this with C# you can use this and it will give you the HH:mm:ss:ms format. 

 

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.