Calculate Calender without Weekend

  • tonylie
  • Topic Author
  • Offline
  • Fresh Boarder
  • Fresh Boarder
  • Posts: 3
  • Thank you received: 0

tonylie created the topic: Calculate Calender without Weekend

HY everyone!
I have the package "calculation with Dates" from the crosstec page.

the code:

function ff_calcdaysbetweendates_init()
{
setInterval('calcdays()',500);
} // ff_QuickForm300860205_init
function calcdays(){
var one_day=1000*60*60*24;
var days='';
sdate=ff_getElementByName('start').value;
edate=ff_getElementByName('end').value;
if (sdate==''){sdate='0000-00-00';}
if (edate==''){edate='0000-00-00';}
sdate = new Date(Number(sdate.split("-")[2]), Number(sdate.split("-")[0])-1, sdate.split("-")[1],0,0,0,0).getTime();
edate = new Date(Number(edate.split("-")[2]), Number(edate.split("-")[0])-1, edate.split("-")[1],0,0,0,0).getTime();
if(edate<sdate){ff_getElementByName('amount_days').value="end date before start date";}
else{
days=edate-sdate;
day=days/one_day;
ff_getElementByName('amount_days').value=day;
}
}

It works great but for my Task it is important, that the Weekend days (Saturday an Sunday) are not calculated, e.g. Wednesday-Wednesday=5 days, not 7 days

Couldt you help me by this Task?


Thanks, Tony.
#233151
  • Posts: 1965
  • Karma: 67
  • Thank you received: 238

mihaela replied the topic: Calculate Calender without Weekend

Hello,

Please try to replace the existing code with the following code to calculate the number of working days:
function ff_calcdaysbetweendates_init()
{

setInterval('calcdays()',500);

} // ff_QuickForm300860205_init

function getNumWorkDays(startDate, endDate) {
    var numWorkDays = 0;
    var currentDate = new Date(startDate);
    while (currentDate <= endDate) {
        // Skips Sunday and Saturday
        if (currentDate.getDay() !== 0 && currentDate.getDay() !== 6) {
            numWorkDays++;
        }
        currentDate = currentDate.addDays(1);
    }
    return numWorkDays;
}

Date.prototype.addDays = function (days) {
    var date = new Date(this.valueOf());
    date.setDate(date.getDate() + days);
    return date;
};

function calcdays(){
var one_day=1000*60*60*24;
var days='';
sdate=ff_getElementByName('start').value;
edate=ff_getElementByName('end').value;

if (sdate==''){sdate='0000-00-00';}
if (edate==''){edate='0000-00-00';}

startdate = new Date(Number(sdate.split("-")[2]), Number(sdate.split("-")[0])-1, sdate.split("-")[1],0,0,0,0);
enddate = new Date(Number(edate.split("-")[2]), Number(edate.split("-")[0])-1, edate.split("-")[1],0,0,0,0);

sdate = new Date(Number(sdate.split("-")[2]), Number(sdate.split("-")[0])-1, sdate.split("-")[1],0,0,0,0).getTime();
edate = new Date(Number(edate.split("-")[2]), Number(edate.split("-")[0])-1, edate.split("-")[1],0,0,0,0).getTime();

if(edate<sdate){ff_getElementByName('amount_days').value="end date before start date";}

else{
  var wd = getNumWorkDays(startdate,enddate);

  ff_getElementByName('amount_days').value=wd;

}

}

Let me know whether this worked as you wanted.

Regards,

Mihaela
#233156
  • tonylie
  • Topic Author
  • Offline
  • Fresh Boarder
  • Fresh Boarder
  • Posts: 3
  • Thank you received: 0

tonylie replied the topic: Calculate Calender without Weekend

Perfect result. Thanks a lot Mihaela!!!
#233166
  • Posts: 1965
  • Karma: 67
  • Thank you received: 238

mihaela replied the topic: Calculate Calender without Weekend

Great! I'm glad that I've helped. :)

Regards,

Mihaela
#233176
Moderators: ForumSupporttomeperica
Time to create page: 0.062 seconds

Community Reward

Help us to create new extensions and plugins! With only $5 you help us a lot and get unlimited download access to all of our products, professional support and even more. Get your reward now!

Read More Here

News and Updates

Get informed about new downloads, updates and more in our News and Updates newsletter.

All Extensions Subscription

Get 1 year access to all of our current and future products and 1 year of professional support -- 99€

No support per domain or website installation limits! Includes all of our current and future Joomla!® extensions, Joomla!® templates for the duration of your membership. This means, by purchasing an All Extensions Subscription you'll have it all covered!

Get it from here

3rd Party Discount - 25% Off

We help you to keep your costs under control. If you are a new member and purchased a form building tool from a different form vendor, then you'll get a 25% discount on our subscription plans.

How to receive the discount:

Send us a quick email to sales@crosstec.org with a proof of purchase (for example a paypal receipt), await payment instructions and enjoy your membership!

Community Reward

Help us to create new extensions and plugins! With only $5 you help us a lot and get unlimited download access to all of our products, professional support and even more. Get your reward now!

Read More Here