Calculate a project start date from the duration and project due date
AnsweredClient specifies due date. based on certain criteria, a duration is assigned. How can the project start date be calculated from the due date and project duration?
Thanks

Hi Tieka,
that's no problem. But it depends on your app setup how the code looks like. Some questions:
Which type is the field where the duration is in? Is it a Podio duration field (like: 10 Days 4 Hours) or is it a number field where someone types in a number (# of weeks? # of days? # of hours?)?
Do you want to calculate with business days (MonFri), without counting Sat and Sun) only so that e.g. DueDate 02/03/2015 minus 5 days = StartDate 01/28/2015? Or do you want to calculate with calendar days so that start date would be 01/30/2015?
Should the StartDate be shown like a DateField (so that you can use it in the Podio Calendar) or as a text string)
Rainer

Hello Rainer,
I set up the duration field as a calc that assigns the duration based on project details. Eg. a project with 12tems items is assigned 10 weekdays, 34 items is assigned 15 workdays and so on. I am not sure how it works yet.if (Number of Items == '12') {Duration Category = '10 weekdays';} else if (Number of Items == '34') {Duration Category = '15 weekdays';} else if (Number of Items == '56') {Duration Category = '20 weekdays';} else if (Number of Items == '7+') {Duration Category = '30 weekdays';} else if (Number of Items == 'Rush') {Duration Category = '5 weekdays';}
I want to calculate with workdays and the start date should be a date field. Thanks again.

Hi Tieka,
you have to make sure that your duration field is a number type calculation field. Then you can use the following code for calculating the StartDate. Create a new calculation field in the template editor with:
var dueDate = @DueDateFieldName; var dur = @DurationFieldName; function subtractWeekdays(date, days) { date = dueDate; while (days > 1) { date = date.subtract(1, 'days'); if (date.isoWeekday() !== 6 && date.isoWeekday() !== 7) { days = 1; } } return date; } var startDate = subtractWeekdays(moment(),dur); moment(starDate).toDate();
Than click onto the field icon (left from the name) and choose the options in the field settings.
For the calc in your duration field: If that what you have written in your post is the whole code in the field the result is a text string and you can't calculate further like you want to do. You have to use another code for getting the number of duration weekdays. And how to get the number depends on your setup :)
Rainer
rg@delosconsulting.com 
may ask a thing Rainer;
First of all, the code is not working for me, i couldnt find the problem.
and a second phase, what if i want to reverse scenario?
Start Day + Duration must be calculate End day (date) using workdays (starts monday morning and ends Friday afternoon.)
how can i?
thanks.

Hi AdminBK,
change the line
date = duedate
to
date = moment(dueDate)
For the reverse scenario just changedate.subtract(1, 'days')
to
date.add(1, 'days')I would also change
subtractWeekdays
to
addWeekdays  just to make it clear that this is an addfunction (it's only naming, doesn't have an impact on the result)Rainer

Hello there Rainer,
I am looking to Chop & Screw your script to help us to calculate how many work days our employees are taking off on their vacation requests while excluding weekend days from the calculation. The issue is that I am a total hack at coding these calculations  DOH!
Do you mind giving this a look over to let me know if I am just being a savage to your script?!?
Any help would be greatly appreciated!

Hi Shane,
it can't work cause that's the wrong code for your use case. The code needs a date = var dueDate and a number of days = var dur; but you have 2 dates. And the code returns a date, but you want to return a number of days.
You can try this one:var start = @Days/Time out of Office;
var end = @Return to Office;
var dur = moment(end).diff(moment(end),"days");
var workdays = 0;
for(var i = 0; i < dur; i++){
var day = moment(start).add(i,"days").isoWeekday();
if(day < 6){
workdays += 1;
}
};
workdaysTry that code in a new calculation field.
Rainer

Hey Rainer,
Thank you for being willing to review and adjust this. I have replaced the script to match, but I now receive a numerical value of zero and I think this has to do with the " var workdays = 0; " line. Should this instead be my original calculation for my yielded workdays? See below:
var workdays = 0;
var workdays = (@Return to Office  @Days/Time Out of Office) / 86400000;
When I swap the "var workdays" to not be zero it comes up with the 13 days calculation, but it is not removing the weekends from the sum.
Thank you for your help here! I think after this calculation quest, I will need to get some night courses under my belt for the next round.
Please sign in to leave a comment.
Comments
11 comments