Calculate working days in mail

Post Reply
Jarmer
Newbie
Posts: 12
Joined: Mon Feb 23, 2015 3:47 pm

Calculate working days in mail

Post by Jarmer »

Hello

I need to calculate working days in a mail, when I receive a XML file then I tricker a mail that needs this information.

is it possible use the "define condition with variables"with "Calculation" in some way ?

René
freddyp
Advanced member
Posts: 413
Joined: Thu Feb 09, 2012 3:53 pm

Re: Calculate working days in mail

Post by freddyp »

The calculation of a new date is not a straightforward calculation. A date has to be converted to a number and after the calculation back to a date. And you will need several if's and even more if you want to take holidays into account. This is not possible without a script expression.

Here is a start:
var now = new Date();
var nowInMilliseconds = Date.parse(now.toString());
var dayInMilliseconds = 86400000;
var nrOfDays;
if (now.getDay() < 5) { //5=Friday
nrOfDays = 1;
} else {
nrOfDays = 3;
}
var then = new Date(nowInMilliseconds+(nrOfDays*dayInMilliseconds));
then;

The fine-tuning is up to you.
User avatar
gabrielp
Advanced member
Posts: 577
Joined: Fri Aug 08, 2014 4:31 pm
Location: Boston
Contact:

Re: Calculate working days in mail

Post by gabrielp »

Not super easy to adapt, since it uses DateInterval and a PHP library called Business, but I did something similar in a PHP project once. This calculates the number of business minutes that have elapsed between two dates based on some defined hours. This is helpful in our case because if someone submits an estimate on 4pm on a Friday and the estimator submits the quote on 10am on the following Monday, it would calculate that only 2 business hours have elapsed.

Code: Select all

            // Opening hours for each week day. If not specified, it is considered closed
            $days = [
                // Standard days with fixed opening hours
                new Day(Days::MONDAY, [['9 AM', '5 PM']]),
                new Day(Days::TUESDAY, [['9 AM', '5 PM']]),
                new Day(Days::WEDNESDAY, [['9 AM', '5 PM']]),
                new Day(Days::THURSDAY, [['9 AM', '5 PM']]),
                new Day(Days::FRIDAY, [['9 AM', '5 PM']]),
            ];
            // Business
            $business = new Business($days);
            // One minute interval
            $interval = new \DateInterval('PT1M');
            // Get all business minutes between estimate creation date and now
            $minutes = $business->timeline($estimateCreatedDateTime, new \DateTime('now'), $interval);
            $businessMinutes = count($minutes);
You can get check out Business here: https://florianv.github.io/business/

You could adapt this to work in Scripter but it would be difficult. But it would be pretty straightforward to make a script that calls the PHP CLI to run this. Just something to think about! :P
Chat: open-automation @ gitter
Code: open-automation & dominickp @ GitHub
Tools: Switch, Pitstop, EPMS, Veracore, PageDNA, SmartStream, Metrix
Post Reply