How to increase the days of an input date without counting on Sundays?

1

I have an input date where I put a date and another input date where N days appear after the day I fill in the first input date, but I want the N days do not count on Sundays.

What I did until now that is easy is to increase the N days but counting on Sunday

CODE

function myFunction() {
               
       document.getElementById("myDate1").value=document.getElementById("myDate").value;
       document.getElementById("myDate1").stepUp(5);
    }
    Date: <input type="date" id="myDate" >
    
    <p>Click the button to increment the value of the date field by 5 days (each time you click).</p>
    <button onclick="myFunction()">Try it</button>
    <input type="date" id="myDate1" >
    
    <p><strong>Note:</strong> The stepUp() method is not supported by Internet Explorer.</p>
    <p><strong>Note:</strong> input elements with type="date" do not show as any date field/calendar in Firefox or in IE 11 and earlier versions.</p>
    

    
    
asked by ingswsm 19.06.2017 в 04:57
source

3 answers

0

You can use a library called moment.js that makes it easier to work with dates, use the day () that this library gives you to know what day of the week it is and use a conditional to filter as necessary.

Here is an example of its use:

var campoFecha = document.getElementById("fecha");
var campoDiaSemana = document.getElementById("dia-semana");

campoFecha.value = moment().format("YYYY-MM-DD");

campoFecha.addEventListener("input", function() {
  var fecha = moment(campoFecha.value);
  campoDiaSemana.value = fecha.day();
});
<!doctype html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Trabajando fechas con moments</title>
        <script src="http://momentjs.com/downloads/moment.js"></script>
    </head>
    <body>
        <label for="fecha">Edita este campo</label>
        <input type="date" id="fecha">
        <br>
        <label for="fecha">Dia de la semana</label>
        <input id="dia-semana" disabled>
    </body>
</html>
    
answered by 19.06.2017 в 06:20
0

Here is an example in JS.

var startDate = "9-DEC-2011";
startDate = new Date(startDate.replace(/-/g, "/"));
var endDate = "", noOfDaysToAdd = 13, count = 0;
while(count < noOfDaysToAdd){
    endDate = new Date(startDate.setDate(startDate.getDate() + 1));
    if(endDate.getDay() != 0 && endDate.getDay() != 6){
       //Date.getDay() gives weekday starting from 0(Sunday) to 6(Saturday)
       count++;
    }
}
alert(endDate);//You can format this date as per your requirement

the Demo of the JS

    
answered by 19.06.2017 в 06:22
-2

Seeming to do what you need.

<?php

function sumasdiasemana($fecha,$dias)
{
$datestart= strtotime($fecha);
$datesuma = 15 * 86400;
$diasemana = date('N',$datestart);

$totaldias = $diasemana+$dias;

$findesemana =  intval( $totaldias/5) *2 ; 

$diasabado = $totaldias % 5 ; 
if ($diasabado==6) $findesemana++;

$total = (($dias+$findesemana) * 86400)+$datestart ;  

return $twstart=date('Y-m-d', $total);
}

echo sumasdiasemana("2008/03/26",260)

?> 
    
answered by 19.06.2017 в 06:16