Introduction
In this blog, I have described, how to create a simple count down of days (business days alone) in a page with minimal use of JavaScript and HTML.
JavaScript, given below, has three parts-
- The actual timer, which is triggered every 7 milliseconds, so that you can change as per your wish.
- myTimerFn() is a function, which sets the start and end dates to get the count down of the business days in between.
- The actual function, which provides the count of business days for the given start and end dates.
Code
- <html>
-
- <head>
- <script type="text/JavaScript">
-
- var myVar = setInterval(myTimerFn, 7000);
-
- function myTimerFn() {
- var d = new Date();
- var enDate = new Date("09/28/2016");
-
- document.getElementById("demo").innerHTML = workingDaysBetweenDates(d.toISOString(),enDate.toISOString());
- }
-
- function workingDaysBetweenDates(stDate, enDate)
- {
- var startDate = new Date(stDate);
- var endDate = new Date(enDate);
-
- if (endDate < startDate)
- return 0;
-
- var millisecondsPerDay=8 6400 * 1000;
-
- startDate.setHours(0);
-
- startDate.setMinutes(0);
- startDate.setSeconds(0);
- endDate.setHours(23);
-
- endDate.setMinutes(59);
- endDate.setSeconds(59);
- var diff=e ndDate - startDate;
-
- var days=M ath.ceil(diff / millisecondsPerDay);
-
-
- var weeks=M ath.floor(days / 7);
- days=d ays - (weeks * 2);
-
- var startDay=s tartDate.getDay();
- var endDay=e ndDate.getDay();
-
- if (startDay - endDay> 1)
- days = days - 2;
-
- if (startDay == 0 && endDay != 6)
- days = days - 1
-
- if (endDay == 6 && startDay != 0)
- days = days - 1 return days;
- }
-
- </script>
- </head>
- <div id="demodiv" style="border:1px solid black;background-color:yellow;height:50px;"><strong><span id="demo"></strong> </span>
- day(s) left </div>
-
- </html>
Output
Please let me know, if you have any issues implementing it.