calculate when it starts and when the week ends

0

How do I generate a function js to do the calculation of when it starts and when the week ends from a day that the user selected. (Taking into account that the only one from Monday to Friday)

Example of how it is intended to be, should be indicated that the calculation will be per week, then must show the calendar for the user to select a day of the week, when the user has indicated the day, on the side will be an input in which you should print what the start and end of the week is depending on the date you selected, for example if you select Tuesday, February 6, 2018, the input data should read Start 05-02 -2018 ends on 09-02-2018 ..... Taking as a start day every Monday and Friday as a final day

    
asked by Karime 28.07.2018 в 00:56
source

3 answers

0

You can create a function with the getDay () method. This method returns the day of the week you chose, with base 0 starting on Monday. That is Monday = 0, Tuesday = 1, etc ...

For example, if your input is called "date"

<input type="date" id="fecha">

Well, in your function, the variable diaSemana will have the value of the day you selected.

var fecha = new Date (document.getElementById("fecha").value);            
var diaSemana = fecha.getDay();

more information at: getDay

Based on this you can do the calculations, but since you do not show any code, there is not much that you can comment on.

I hope it serves you.

    
answered by 28.07.2018 в 02:22
0

I see that you want the week to start from Monday and end on Sunday, so you should add some conditionals since getDay () will return on Sunday as the first day, it can be solved with an If so that when it arrives Sunday I took it as the Seventh day of the week. They suggested the following:

HTML:

<input type="date" id="fechaAnalizar" onChange="weekIF()"><!--Cada vez que se toma una fecha se llama a la funcion-->
<div id="inicio_fin"></div><!--Aqui se imprime el inicio y fin de semana-->

JavaScript:

<script type="text/javascript" src="https://momentjs.com/downloads/moment.min.js"></script>
<script type="text/javascript">
 function weekIF() {

    let div = document.getElementById("inicio_fin");
    let fecha = document.getElementById("fechaAnalizar").value;//Toma el valor de la fecha seleccionado
    var current = new Date(fecha);

    const week = [];
    const weekFormat = [];

    //Controla cual sera el primer dia
    if(current.getDay() == 0){
        current.setDate(((current.getDate() - 7)+1));
    }else{
        current.setDate(((current.getDate() - current.getDay()) + 1));
    }

    week.push(new Date(current));//Agrega el primer dia al array
    current.setDate(current.getDate()+6);//Define el septimo dia
    week.push(new Date(current));//Agrega el ultimo dia al array

    //Aplicar formato deseado a la fecha
    week.forEach((w) => {
        weekFormat.push(moment(w).format('DD/MM/YYYY'));
    });

    div.innerHTML = "Inicia el " + weekFormat[0] + " y termina el " + weekFormat[1];
}

</script>

I hope it works for you. I'm attentive.

    
answered by 28.07.2018 в 06:36
0

The function that takes a value of type Date and returns two values of type Date with the start and end dates of the week (Monday and Friday) is the following:

function obtenerInicioYFinSemana(fecha) {
    return {
        inicio: new Date(fecha.getFullYear(), fecha.getMonth(), fecha.getDate() - fecha.getDay() + 1),
        fin: new Date(fecha.getFullYear(), fecha.getMonth(), fecha.getDate() + 5 - fecha.getDay()),
    }
}

Example:

obtenerInicioYFinSemana(new Date());

Result:

{
    inicio: Mon Jul 30 2018 00:00:00 GMT+0200 (hora de verano de Europa central), 
    fin: Fri Aug 03 2018 00:00:00 GMT+0200 (hora de verano de Europa central)
}

I hope it serves you.

Greetings.

    
answered by 30.07.2018 в 11:15