snipplr.com Open in urlscan Pro
2606:4700:20::6819:272c  Public Scan

Submitted URL: http://snipplr.com/view/4086/calculate-business-days-between-two-date/
Effective URL: https://snipplr.com/view/4086/calculate-business-days-between-two-date
Submission: On December 01 via manual from US — Scanned from DE

Form analysis 2 forms found in the DOM

Name: searchGET /all

<form name="search" action="/all" method="get" accept-charset="utf-8" class="search">
  <div class="row">
    <div class="col-4">
      <label for="search"></label><input type="text" name="search" class="s-input s-search-box" value="" placeholder="Search by title, description" id="search">
      <a class="advsearch" href="/advanced-search/"><strong>Advanced Search</strong></a>
    </div>
  </div>
</form>

<form id="folder-add">
  <input type="text" id="folder-name">
  <input type="submit" value="Add">
  <input type="button" id="folder_close" value="Done">
</form>

Text Content

 * code snippets
   * all code snippets/
   * popular code snippets/
   * your code snippets
 * 
 * blog
 * privacy
 * about snipplr

 * New Snippet/
 * Register/
 * Login

Advanced Search


POSTED BY

--------------------------------------------------------------------------------

Bluebeard on 11/07/07


TAGGED

--------------------------------------------------------------------------------

 * date
 * Business


VERSIONS (?)

--------------------------------------------------------------------------------

Last Edited at 11/07/07 10:44pm


STATISTICS

--------------------------------------------------------------------------------

Viewed 1763 times
Favorited by 0 user(s)


RELATED SNIPPETS

--------------------------------------------------------------------------------

997
Get number of days between two dates
757
Get difference between two dates in days
561
Get working days between two dates, with custom off day
420
Days between two dates (datediff)
646
calculate angle between two vectors
414
Calculate the difference between two dates
448
Difference in days between two dates
712
Show a list of days between two dates


CALCULATE BUSINESS DAYS BETWEEN TWO DATES

--------------------------------------------------------------------------------

/ Published in: JavaScript

Save to your folder(s)






--------------------------------------------------------------------------------

This routine is loosely based on elightbo's "Calculate Business Days" snippet in
ASP, with some process & logic modifications (most notably accounting for
situations when both dates fall on a weekend) and a translation to JavaScript.

Expand | Embed | Plain Text


Copy this code and paste it in your HTML
 1.  function calcBusinessDays(dDate1, dDate2) {         // input given as Date objects
 2.   
 3.    var iWeeks, iDateDiff, iAdjust = 0;
 4.   
 5.    if (dDate2 < dDate1) return -1;                 // error code if dates transposed
 6.   
 7.    var iWeekday1 = dDate1.getDay();                // day of week
 8.    var iWeekday2 = dDate2.getDay();
 9.   
 10.   iWeekday1 = (iWeekday1 == 0) ? 7 : iWeekday1;   // change Sunday from 0 to 7
 11.   iWeekday2 = (iWeekday2 == 0) ? 7 : iWeekday2;
 12.  
 13.   if ((iWeekday1 > 5) && (iWeekday2 > 5)) iAdjust = 1;  // adjustment if both days on weekend
 14.  
 15.   iWeekday1 = (iWeekday1 > 5) ? 5 : iWeekday1;    // only count weekdays
 16.   iWeekday2 = (iWeekday2 > 5) ? 5 : iWeekday2;
 17.  
 18.   // calculate differnece in weeks (1000mS * 60sec * 60min * 24hrs * 7 days = 604800000)
 19.   iWeeks = Math.floor((dDate2.getTime() - dDate1.getTime()) / 604800000)
 20.  
 21.   if (iWeekday1 <= iWeekday2) {
 22.     iDateDiff = (iWeeks * 5) + (iWeekday2 - iWeekday1)
 23.   } else {
 24.     iDateDiff = ((iWeeks + 1) * 5) - (iWeekday1 - iWeekday2)
 25.   }
 26.  
 27.   iDateDiff -= iAdjust                            // take into account both days on weekend
 28.  
 29.   return (iDateDiff + 1);                         // add 1 because dates are inclusive
 30.  
 31. }

Report this snippet








COMMENTS

Subscribe to comments

--------------------------------------------------------------------------------

Comment:



You need to login to post a comment.


 * Code Snippets/
 * Snipplr Blog/
 * About Snipplr


CHOOSE A LANGUAGE FOR EASY BROWSING:

 * ActionScript
 * ActionScript 3
 * Apache
 * AppleScript
 * ASP
 * Assembler
 * AutoIt
 * Awk
 * Bash
 * C
 * C#
 * C++
 * Clojure
 * ColdFusion
 * CSS
 * Delphi
 * Diff
 * Django
 * DOS Batch
 * Emacs Lisp
 * eZ Publish
 * Forth
 * Fortran
 * Gnuplot
 * Groovy
 * HAML
 * Haskell
 * HTML
 * iPhone
 * Java
 * JavaScript
 * jQuery
 * LaTeX
 * lighttpd
 * Lisp
 * Lua
 * Makefile
 * MatLab
 * Maxscript
 * Mel
 * MXML
 * MySQL
 * NewtonScript
 * Objective C
 * Open Firmware
 * Other
 * Pascal
 * Perl
 * PHP
 * PicBasic
 * PL/SQL
 * Processing
 * Prolog
 * Pseudocode
 * Python
 * R
 * Rails
 * Regular Expression
 * Revolution
 * Ruby
 * SAS
 * SASS
 * Scala
 * Scheme
 * SmallTalk
 * Smarty
 * SML
 * SPSS
 * SQL
 * SVN
 * Symfony
 * TCL
 * Textpattern
 * TYPO3
 * VB.NET
 * VHDL
 * Visual Basic
 * W-Language
 * Windows PowerShell
 * Windows Registry
 * XHTML
 * XML
 * XSLT