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
  1. <html>
  2. <head>
  3. <script type="text/JavaScript">
  4. //To call a javascript function using timer (part 1)
  5. var myVar = setInterval(myTimerFn, 7000);
  6. // To call the pass the start and end date to function calculating the business days in between (part 2)
  7. function myTimerFn() {
  8. var d = new Date();
  9. var enDate = new Date("09/28/2016");
  10. //alert (d.toISOString() + " | " +enDate.toISOString());
  11. document.getElementById("demo").innerHTML = workingDaysBetweenDates(d.toISOString(),enDate.toISOString());
  12. }
  13. //Function for finding business days between two dates (part 3)
  14. function workingDaysBetweenDates(stDate, enDate)
  15. {
  16. var startDate = new Date(stDate);
  17. var endDate = new Date(enDate);
  18. // Validate input
  19. if (endDate < startDate)
  20. return 0;
  21. // Calculate days between dates
  22. var millisecondsPerDay=8 6400 * 1000;
  23. // Day in milliseconds
  24. startDate.setHours(0);
  25. // Start just after midnight
  26. startDate.setMinutes(0);
  27. startDate.setSeconds(0);
  28. endDate.setHours(23);
  29. // End just before midnight
  30. endDate.setMinutes(59);
  31. endDate.setSeconds(59);
  32. var diff=e ndDate - startDate;
  33. // Milliseconds between datetime objects
  34. var days=M ath.ceil(diff / millisecondsPerDay);
  35. // alert ( "Days Diff:" + days);
  36. // Subtract two weekend days for every week in between
  37. var weeks=M ath.floor(days / 7);
  38. days=d ays - (weeks * 2);
  39. // Handle special cases
  40. var startDay=s tartDate.getDay();
  41. var endDay=e ndDate.getDay();
  42. // Remove weekend not previously removed.
  43. if (startDay - endDay> 1)
  44. days = days - 2;
  45. // Remove start day if span starts on Sunday but ends before Saturday
  46. if (startDay == 0 && endDay != 6)
  47. days = days - 1
  48. // Remove end day if span ends on Saturday but starts after Sunday
  49. if (endDay == 6 && startDay != 0)
  50. days = days - 1 return days;
  51. }
  52. </script>
  53. </head>
  54. <div id="demodiv" style="border:1px solid black;background-color:yellow;height:50px;"><strong><span id="demo"></strong> </span>
  55. day(s) left </div>
  56. </html>
Output
Please let me know, if you have any issues implementing it.