Given a date (dd-mm-yyyy), get Monday of that week. In number [duplicate]

1

I am trying to create a function with datapikcer information but I can not find a way to do it. Given a date, for example: (01-09-2017), know what is Monday of this week? In this case I would have to return (08-28-2017). (That would be a special case because Monday of this week corresponds to the previous month). But a normal case would be for example if I happen to the function (06-09-2017), I would have to return (04-09-2017) which is the same month and is the Monday corresponding to that week and that date.

    
asked by morrison 01.09.2017 в 18:44
source

1 answer

2

Well, taking into account that the day Sunday is the first day of the week, you could do a little math so that you can get the result you want. I leave you some code so you can review it.

fecha1 = new Date("2017-09-01");
dias1=(fecha1.getUTCDay()-1)*(-1);
fecha1.setDate(fecha1.getDate() + dias1);
console.log("Lunes de la semana:");
console.log(fecha1);

And if you take Monday as the first day of the week, then:

fecha1 = new Date("2017-09-03");
diapararestar=fecha1.getUTCDay();
if(diapararestar==0){
    dias1=(-6);        
}else{
    dias1=(diapararestar-1)*(-1);        
}

fecha1.setDate(fecha1.getDate() + dias1);
console.log("Lunes de la semana:");
console.log(fecha1);

Well I hope the code will help you.

    
answered by 01.09.2017 / 20:19
source