cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Browse apps to extend the software in the new JMP Marketplace
Choose Language Hide Translation Bar
fr2007
Level II

Calculating duration between two dates from different years

I am using JSL scripting to run one of my reports that can calucate the duration between two dates. My report has a Start Date in column 'A' and End Date in column 'E' , I want to calcuate the following:

1. Duration between those two days excluding weekends

2, Duration between those two days , start date is 10/31/2012 and end date is 02/20/2013, and when I used the forumla "Formula(Day(:E - :A) )" , Iam getting a value of 10, instead of something bigger. Start Date is in 2012 and end date is in 2013, so do I need to make any change to my script ? The other durations that fall in the same year are calculated properly using the same formula..but the dates that fall in different years are not calculated accurately..

can some one help with the two questions?

thanks,

1 ACCEPTED SOLUTION

Accepted Solutions
ms
Super User (Alumni) ms
Super User (Alumni)

Re: Calculating duration between two dates from different years

Ok I think I understand the problem. JMP apparently does not follow the ISO 8601 standard (i.e first day of week is monday) but the US tradition of celebrating sunday as the first day of the week. This perfectly explains your results above as my formula assumes the former. However, it's trivial to adapt the formula to take that into account (or any other local definition of day of week or what days that are considered belonging to the weekend).

Try e.g. this:

Try( Sum( 1 < Day Of Week( Index( :A, :E, 86400 ) ) <= 6 ) - 1 )

Below is my test output and it appears to work. The "counter" halts over weekends (sat-sun where I live). Remove the -1 if you by "duration between two dates" mean to include the full duration of both start and end dates. (Here A = today, E = future days)

AEWeekdayWorking days
2013-04-052013-04-05Friday0
2013-04-052013-04-06Saturday0
2013-04-052013-04-07Sunday0
2013-04-052013-04-08Monday1
2013-04-052013-04-09Tuesday2
2013-04-052013-04-10Wednesday3
2013-04-052013-04-11Thursday4
2013-04-052013-04-12Friday5
2013-04-052013-04-13Saturday5
2013-04-052013-04-14Sunday5
2013-04-052013-04-15Monday6
2013-04-052013-04-16Tuesday7
2013-04-052013-04-17Wednesday8
2013-04-052013-04-18Thursday9
2013-04-052013-04-19Friday10
2013-04-052013-04-20Saturday10
2013-04-052014-04-21Monday271
2013-04-052015-04-22Wednesday533

View solution in original post

13 REPLIES 13
pmroz
Super User

Re: Calculating duration between two dates from different years

Can you provide more information?  What is the data type and format of the A and E columns?  Also, when you subtract dates you get the number of seconds between them.  Here's a simple example that calculates the date difference in days:

adate = informat("10/31/2012", "m/d/y");

edate = informat("02/20/2013", "m/d/y");

cdate = (edate - adate) / 3600 / 24;


This results in

112

fr2007
Level II

Re: Calculating duration between two dates from different years

can you make it more general? I will explain my data clearly.. I have a column "Start Date" with format as Date (mm/dd/yyyy) and another column "End Date" with format as mm/dd/yyyy.I want to subtract start date from End date, my date are

Start Date     End Date

10/31/2012   03/31/2013

09/15/2012    02/13/2013

08/15/2012     01/29/2013

Now I want to subtract End Date - Start Date to get the value in days

since start date is in 2012 and end dates are in 2013, iam not getting the correct value if I simply say :End Date - :Start Date

Re: Calculating duration between two dates from different years

This will give you time elapsed in days.

First, add a new numeric variable/column.

In the Formula Editor window, select first date, click subtract, select second date, then divided by, select Date Time function, in days, put a 1 in the parenthesis, click apply.

(:diagnosis_date - :dob_date) / In Days( 1 )

fr2007
Level II

Re: Calculating duration between two dates from different years

This is calcuating same as using Date Difference..but this is including weekends as well..is there a way to calculate ONLY weekdays?

ms
Super User (Alumni) ms
Super User (Alumni)

Re: Calculating duration between two dates from different years

Did you ever try the formula in my reply? It was supposed to exclude weekdays and it works for me if the columns are in date format (not strings).

fr2007
Level II

Re: Calculating duration between two dates from different years

I tried your formula..it is working for few cells..see my data below..using your formula..the rows 1 and 2 contradict..

Planned start dateNew Task Completion DateMS
1/3/20131/3/20130
1/3/20131/4/20130
1/8/20131/8/20130
1/8/20131/8/20130
1/9/20131/9/20130
1/9/20131/9/20130
1/9/20131/11/20131
1/9/20131/9/20130
1/9/20131/10/20131
1/9/20131/9/20130
1/10/20131/11/20130
1/10/20131/11/20130
1/2/20131/2/20130
1/2/20131/8/20134
1/4/20131/4/2013-1
1/4/20131/4/2013-1
1/4/20131/4/2013-1
1/4/20131/7/20131
1/4/20131/4/2013-1
1/4/20131/4/2013-1
1/4/20131/4/2013-1
1/4/20131/8/20132
1/4/20131/4/2013-1
1/4/20131/4/2013-1
1/4/20131/4/2013-1
1/4/20131/7/20131
1/4/20131/4/2013-1
1/4/20131/4/2013-1
1/4/20131/4/2013-1
1/4/20131/8/20132
1/4/20131/4/2013-1
1/4/20131/7/20131
1/7/20131/8/20131
1/4/20131/4/2013-1
1/4/20131/4/2013-1
1/7/20131/7/20130
1/7/20131/9/20132
1/10/20131/10/20130
1/7/20131/7/20130
1/7/20131/7/20130
1/7/20131/7/20130
1/7/20131/8/20131
1/8/20131/9/20131
1/7/20131/7/20130
1/7/20131/7/20130
1/7/20131/8/20131
1/7/20131/7/20130
1/7/20131/7/20130
1/7/20131/8/20131
1/8/20131/8/20130
1/8/20131/8/20130
1/8/20131/9/20131
1/9/20131/16/20135
1/16/20131/22/20134
1/8/20131/8/20130
1/8/20131/8/20130
1/9/20131/9/20130
1/9/20131/10/20131
1/10/20131/10/20130
1/15/20131/15/20130
1/15/20131/16/20131
1/15/20131/16/20131
1/18/20132/19/201322
2/19/20132/19/20130
1/15/20131/15/20130
1/15/20131/16/20131
1/16/20131/18/20131
1/15/20131/15/20130
1/15/20131/16/20131
1/16/20131/18/20131
1/9/20131/9/20130
1/9/20131/9/20130
1/9/20131/10/20131
1/10/20131/10/20130
1/10/20131/10/20130
1/10/20131/10/20130
1/10/20131/10/20130
1/10/20131/10/20130
1/10/20131/11/20130
1/10/20131/10/20130
1/16/20131/29/20139
1/16/20131/29/20139
1/29/20132/1/20132
1/11/20131/11/2013-1
1/11/20131/11/2013-1
1/11/20131/11/2013-1
1/11/20131/14/20131
ms
Super User (Alumni) ms
Super User (Alumni)

Re: Calculating duration between two dates from different years

Ok I think I understand the problem. JMP apparently does not follow the ISO 8601 standard (i.e first day of week is monday) but the US tradition of celebrating sunday as the first day of the week. This perfectly explains your results above as my formula assumes the former. However, it's trivial to adapt the formula to take that into account (or any other local definition of day of week or what days that are considered belonging to the weekend).

Try e.g. this:

Try( Sum( 1 < Day Of Week( Index( :A, :E, 86400 ) ) <= 6 ) - 1 )

Below is my test output and it appears to work. The "counter" halts over weekends (sat-sun where I live). Remove the -1 if you by "duration between two dates" mean to include the full duration of both start and end dates. (Here A = today, E = future days)

AEWeekdayWorking days
2013-04-052013-04-05Friday0
2013-04-052013-04-06Saturday0
2013-04-052013-04-07Sunday0
2013-04-052013-04-08Monday1
2013-04-052013-04-09Tuesday2
2013-04-052013-04-10Wednesday3
2013-04-052013-04-11Thursday4
2013-04-052013-04-12Friday5
2013-04-052013-04-13Saturday5
2013-04-052013-04-14Sunday5
2013-04-052013-04-15Monday6
2013-04-052013-04-16Tuesday7
2013-04-052013-04-17Wednesday8
2013-04-052013-04-18Thursday9
2013-04-052013-04-19Friday10
2013-04-052013-04-20Saturday10
2013-04-052014-04-21Monday271
2013-04-052015-04-22Wednesday533
fr2007
Level II

Re: Calculating duration between two dates from different years

Yes it worked..thankyou so much!!

fr2007
Level II

Re: Calculating duration between two dates from different years

The data is Numeric COntinuous, Date format..and I want to calculate the number of weekdays excluding the weekends.. sample data:-

Planned start dateNew Task Completion Date
1/3/20131/3/2013
1/3/20131/4/2013
1/8/20131/8/2013
1/8/20131/8/2013
1/9/20131/9/2013
1/9/20131/9/2013
1/9/20131/11/2013
1/9/20131/9/2013
1/9/20131/10/2013
1/9/20131/9/2013
1/10/20131/11/2013
1/10/20131/11/2013