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

Question

Question

Calculating number of days based on calendar input

asked on December 15, 2014

Hello, 

We have a form in which users enter the Start and End Date of a task using the calendar in the date field. 

We would like the number of days between the Start and End date to be calculated and placed in another field. 

Any ideas on how we can do this? 

Thanks

1 0

Replies

replied on December 15, 2014

Should be fairly simple with Javascript Dates...

$('.DateClass input').change( function () {
   var t1 = new Date($('.StartDate input').val());
   var t2 = new Date($('.EndDate input').val());
   $('.DateDiff input').val(Math.floor((t2-t1)/(1000*60*60*24)));
});

Here, I have DateClass on both my date fields, which should catch any changes to either field. Additionally, the start date has a StartDate class and the end date has the EndDate class. Finally, the Difference field has the DateDiff class. You should probably add some code to handle the case for when an invalid date is entered into one of the fields. You may also need to adjust the difference by 1 day if the difference you are looking for is inclusive of the end points.

This works by taking the dates from the fields as a string value and converting it to a Javascript Date class. You can add/subtract these dates (which just store time by counting in milliseconds the time since Jan1 1970). Then, you can just divide out various time factors to convert the units to days/months, years, etc. You may need to be careful if your dates are from different time zones, but other than that it should work well. At any rate, that should get you started.

3 0
replied on April 20, 2015 Show version history

I use a script to calculate a date difference in a Forms process as well. I use a date picker to let a manager choose a start date for a new employee and I use the date difference between today and the start date to then assign priority to User Tasks. This way the new employees who need to start their positions in a few days get priority over ones who may start in more than 10 days. I hope this helps. I think the screenshots should explain what I mean.

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

Sign in to reply to this post.