Calculate difference in days between two dates excluding weekends

Asked

Viewed 311 times

-2

I would like to know how to calculate the difference in days between two dates, excluding Saturday and Sunday.

I have a field in my form where picks the date acts, and another where the user selects a date.

var hoje = new Date('04/07/2019')
var dataFinal = new Date('10/07/2019')

in the format DD/MM/YYYY

  • 2

    How you want to calculate the difference between dates and show them in the format DD/MM/AAAA? It doesn’t make much sense to me. In fact:

  • Excluding Saturdays and Sundays and bank holidays (working days)?

  • @Luizfelipe I think I was not clear, but it is not to be returned in DD/MM/YYYY format, the initial and final date is that are in this format, it should return an integer, sorry for the misunderstanding

  • @Guilhermenascimento only Saturday and dismissal

1 answer

0

F1 = difference and returns when it is over. param = initial date; param2 = final date; param3 = day of the week of the first parameter.

var hoje = new Date();
var limite = 14;
var data = hoje.getDate();
function diferenca(param,param2,param3) {
    this.dia_feira = 0;
    this.day = param3.getDay();
    for (f0=param;f0<=param2;f0++) {
        if (this.day !== 0 && this.day !== 6) {
            this.dia_feira += 1;
        }
        if (this.day < 6) {
            this.day += 1;
        } else {
            this.day = 0;
        }
    }
    return this.dia_feira;
}
alert(diferenca(data,limite,hoje));

returns 5;

If you want something advanced: https://github.com/datejs/Datejs

Has extra function.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.