I found a code for finding days excluding weekends between two date without using datapicker but I need to show calendar(visually).I wrote code for showing calendar with datapicker but I could not be successful to update the code to write the code the way to cover the weekend.I need this calendar for an ASP.NET Core MVC project.
I will write both:
<script>
$(function () {
$("#date1").datepicker();
var date1 = $("#date1")
});
$(function () {
$("#date2").datepicker();
var date2 = $("#date2")
});
count = 0;
function Diffr() {
date1 = new Date(date1.value);
date2 = new Date(date2.value);
if (date1 < date2)
{
var milli_seconds = date1.getTime() - date2.getTime();
}
else
var milli_seconds = date2.getTime() - date1.getTime();
var days = milli_seconds / (1000 * 3600 * 24);
document.getElementById("idd").innerHTML = Math.round(Math.abs(days));
}
</script>
<p>
Date1 :
<input type="text" id="date1">
</p>
<p>
Date2 :
<input type="text" id="date2">
</p>
<button id="sub" class="btn btn-success" onclick="Diffr()">Submit</button>
<h3 id="idd">Difference</h3>
The other one is:
<script>
function CalculateDiffr() {
var date = document.getElementById('txt_Start').value;
var date1 = date.split('.')[1] + "/" + date.split('.')[0] + "/" + date.split('.')[2];
date = document.getElementById('txt_Last').value;
var date2 = date.split('.')[1] + "/" + date.split('.')[0] + "/" + date.split('.')[2];
var days = ['N', 'Y', 'Y', 'Y', 'Y', 'Y', 'N'];
var d1 = new Date(date1);
var d2 = new Date(date2);
var busssinessday= 0;
while (true) {
if (d1 > d2) {
break;
}
var dayName = days[d1.getDay()];
if (dayName != "N") {
businessday++;
}
d1.setDate(d1.getDate() + 1);
}
document.getElementById('Days').value = businessday.toString();
}
</script>
@Html.TextBox("txt_Start", "", new { @class = "form-control" })
@Html.TextBox("txt_Last", "", new { @class = "form-control" })
@Html.TextBox("Days", "", new { @class = "form-control" })
<button onclick="CalculateDiffr()">OK</button>
2
Answers
How about using
type="date"
to show calendar(visually) ?Try:
result:
refer this answer
According to this answer, you just create a function to get the count of the business days: