Adding days to date while ignoring weekends using Javascript
1. In this post we can learn declaring the date picker in html page and getting the selected value.
2. Setting the date picker control disabling the weekends and disabling the previous dates.
3. Adding the Number of days to Selected date with Excluding the Weekends.
HTML Code Snippet:
<div>
<label>
Start Date:
</label>
<input type="text" id="_startDatePicker" readonly>
</div>
<div style="margin-top: 10px;">
<label>Date after adding Days:</label>
<input type="text" id="_duration" disabled>
</div>
CSS and JS reference Files:
<link href="datepicker.css" rel="stylesheet" type="text/css">
<link href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.css" rel="stylesheet" type="text/css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js" type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js" type="text/javascript"></script>
<script src="datepicker-1.10.js" type="text/javascript"></script>
Java Script Code to get and Add the Number of days to Selected with Excluding Weekends:
<script>
$(document).ready(function() {
var selectedDate,last,day,month,year,noOfDaysToAdd,currentDate,count = 0;
$('#_startDatePicker').datepicker({
minDate: 0,
maxDate: '+1y',
beforeShowDay: $.datepicker.noWeekends,
onSelect: function(datestr){
selectedDate = $(this).datepicker('getDate');
noOfDaysToAdd=10; // Days you want to subtract
selectedDate = new Date(selectedDate.getTime());//Converting selected date
//Getting the Day, Month and Year from the slectedDate variable
day =selectedDate.getDate();
month=selectedDate.getMonth()+1;
year=selectedDate.getFullYear();
currentDate = month+"/"+day+"/"+year;
currentDate = new Date(currentDate.replace(/-/g, "/"));
//The below while function is used to exclude the weekends and adding the values to selected date.
count = 0;
while(count < noOfDaysToAdd){
//Using The below line we adding the days to selected date and if helps to check the Sunday and Saturday. Zero is Sunday and 6 is Saturday.
endDate = new Date(currentDate.setDate(currentDate.getDate() + 1));
if(endDate.getDay() != 0 && endDate.getDay() != 6){
count++;
}
}
selectedDate = new Date(endDate.getTime());
day =selectedDate.getDate();
month=selectedDate.getMonth()+1;
year=selectedDate.getFullYear();
if(day <= 9)
day="0"+day;
$('#_duration').val(day+"/"+month+"/"+year)
}
});
});
</script>
Full Snippet of the code:
<html>
<head>
<link href="datepicker.css" rel="stylesheet" type="text/css">
<link href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.css" rel="stylesheet" type="text/css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js" type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js" type="text/javascript"></script>
<script src="datepicker-1.10.js" type="text/javascript"></script>
<!-- <script src="DatePicker.js" type="text/javascript"></script> -->
<script>
$(document).ready(function() {
var selectedDate,last,day,month,year,noOfDaysToAdd,currentDate,count = 0;
$('#_startDatePicker').datepicker({
minDate: 0,
maxDate: '+1y',
beforeShowDay: $.datepicker.noWeekends,
onSelect: function(datestr){
selectedDate = $(this).datepicker('getDate');
noOfDaysToAdd=10; // Days you want to subtract
selectedDate = new Date(selectedDate.getTime());
day =selectedDate.getDate();
month=selectedDate.getMonth()+1;
year=selectedDate.getFullYear();
currentDate = month+"/"+day+"/"+year;
currentDate = new Date(currentDate.replace(/-/g, "/"));
count = 0;
while(count < noOfDaysToAdd){
endDate = new Date(currentDate.setDate(currentDate.getDate() + 1));
if(endDate.getDay() != 0 && endDate.getDay() != 6){
count++;
}
}
selectedDate = new Date(endDate.getTime());
day =selectedDate.getDate();
month=selectedDate.getMonth()+1;
year=selectedDate.getFullYear();
if(day <= 9)
day="0"+day;
$('#_duration').val(day+"/"+month+"/"+year)
}
});
});
</script>
</head>
<body>
<div>
<label>
Start Date:
</label>
<input type="text" id="_startDatePicker" readonly>
</div>
<div style="margin-top: 10px;">
<label>Previous Duration:</label>
<input type="text" id="_duration" disabled>
</div>
</body>
</html>
2. Setting the date picker control disabling the weekends and disabling the previous dates.
3. Adding the Number of days to Selected date with Excluding the Weekends.
HTML Code Snippet:
<div>
<label>
Start Date:
</label>
<input type="text" id="_startDatePicker" readonly>
</div>
<div style="margin-top: 10px;">
<label>Date after adding Days:</label>
<input type="text" id="_duration" disabled>
</div>
CSS and JS reference Files:
<link href="datepicker.css" rel="stylesheet" type="text/css">
<link href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.css" rel="stylesheet" type="text/css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js" type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js" type="text/javascript"></script>
<script src="datepicker-1.10.js" type="text/javascript"></script>
Java Script Code to get and Add the Number of days to Selected with Excluding Weekends:
<script>
$(document).ready(function() {
var selectedDate,last,day,month,year,noOfDaysToAdd,currentDate,count = 0;
$('#_startDatePicker').datepicker({
minDate: 0,
maxDate: '+1y',
beforeShowDay: $.datepicker.noWeekends,
onSelect: function(datestr){
selectedDate = $(this).datepicker('getDate');
noOfDaysToAdd=10; // Days you want to subtract
selectedDate = new Date(selectedDate.getTime());//Converting selected date
//Getting the Day, Month and Year from the slectedDate variable
day =selectedDate.getDate();
month=selectedDate.getMonth()+1;
year=selectedDate.getFullYear();
currentDate = month+"/"+day+"/"+year;
currentDate = new Date(currentDate.replace(/-/g, "/"));
//The below while function is used to exclude the weekends and adding the values to selected date.
count = 0;
while(count < noOfDaysToAdd){
//Using The below line we adding the days to selected date and if helps to check the Sunday and Saturday. Zero is Sunday and 6 is Saturday.
endDate = new Date(currentDate.setDate(currentDate.getDate() + 1));
if(endDate.getDay() != 0 && endDate.getDay() != 6){
count++;
}
}
selectedDate = new Date(endDate.getTime());
day =selectedDate.getDate();
month=selectedDate.getMonth()+1;
year=selectedDate.getFullYear();
if(day <= 9)
day="0"+day;
$('#_duration').val(day+"/"+month+"/"+year)
}
});
});
</script>
Full Snippet of the code:
<html>
<head>
<link href="datepicker.css" rel="stylesheet" type="text/css">
<link href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.css" rel="stylesheet" type="text/css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js" type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js" type="text/javascript"></script>
<script src="datepicker-1.10.js" type="text/javascript"></script>
<!-- <script src="DatePicker.js" type="text/javascript"></script> -->
<script>
$(document).ready(function() {
var selectedDate,last,day,month,year,noOfDaysToAdd,currentDate,count = 0;
$('#_startDatePicker').datepicker({
minDate: 0,
maxDate: '+1y',
beforeShowDay: $.datepicker.noWeekends,
onSelect: function(datestr){
selectedDate = $(this).datepicker('getDate');
noOfDaysToAdd=10; // Days you want to subtract
selectedDate = new Date(selectedDate.getTime());
day =selectedDate.getDate();
month=selectedDate.getMonth()+1;
year=selectedDate.getFullYear();
currentDate = month+"/"+day+"/"+year;
currentDate = new Date(currentDate.replace(/-/g, "/"));
count = 0;
while(count < noOfDaysToAdd){
endDate = new Date(currentDate.setDate(currentDate.getDate() + 1));
if(endDate.getDay() != 0 && endDate.getDay() != 6){
count++;
}
}
selectedDate = new Date(endDate.getTime());
day =selectedDate.getDate();
month=selectedDate.getMonth()+1;
year=selectedDate.getFullYear();
if(day <= 9)
day="0"+day;
$('#_duration').val(day+"/"+month+"/"+year)
}
});
});
</script>
</head>
<body>
<div>
<label>
Start Date:
</label>
<input type="text" id="_startDatePicker" readonly>
</div>
<div style="margin-top: 10px;">
<label>Previous Duration:</label>
<input type="text" id="_duration" disabled>
</div>
</body>
</html>
Comments
Post a Comment