www.webdeveloper.com
Page 2 of 2 FirstFirst 12
Results 16 to 21 of 21

Thread: Help Customizing Script to Subtract Business Days from Current Date

  1. #16
    Join Date
    Aug 2013
    Posts
    8
    JMRKER I like your solution for the Business day and holiday. It saves me from having to go in and update the dates of the variable holidays each year.

    In my original script I was calculating date using 86400 seconds per day.
    Code:
    		<script type="text/javascript">
                              var InputDate;
    		var SelectDate;
    		var numDays;
    		var getDate;
    		var newDate
    		
    		var Edate30 
    		var Edate45
    		var Edate60
    		var Edate90
    		
    		EnumDays0 = 0
    		EnumDays30 = 2592000 //(30 days) 2629743 (1 month=30.44 Days) 
    		EnumDays45 = 3888000 //(45 days)
    		EnumDays60 = 5184000 //(60 Days) 5259486 (60.87 Days)
    		EnumDays90 = 7776000 //(90 Days) 7819200 (90.5 Days) 
    
    			function addDate30(dateObject, numDays30) {
    			dateObject.setDate(dateObject.getDate() + numDays30);
    			return dateObject.toLocaleDateString();
    			}
    
    			function addDate45(dateObject, numDays45) {
    			dateObject.setDate(dateObject.getDate() + numDays45);
    			return dateObject.toLocaleDateString();
    			}
    			
    			function addDate60(dateObject, numDays60) {
    			dateObject.setDate(dateObject.getDate() + numDays60);
    			return dateObject.toLocaleDateString();
    			}
    
                                          function addDate90(dateObject, numDays90) {
    			dateObject.setDate(dateObject.getDate() + numDays90);
    			return dateObject.toLocaleDateString();
    			}
    
    		var EdateM30
    		var EdateM45
    		var EdateM60
    		var EdateM90
    
    		EnumDaysM30 = -2592000
    		EnumDaysM45 = -3888000
    		EnumDaysM60 = -5184000
    		EnumDaysM90 = -7776000
     
    
    			function addDatem10(dateObject, numDaysm10) {
    			dateObject.setDate(dateObject.getDate() + numDaysm10);
    			return dateObject.toLocaleDateString();
    		}
    
    			function addDatem30(dateObject, numDaysm30) {
    			dateObject.setDate(dateObject.getDate() + numDaysm30);
    			return dateObject.toLocaleDateString();
    		}
    
    			function addDatem45(dateObject, numDaysm45) {
    			dateObject.setDate(dateObject.getDate() + numDaysm45);
    			return dateObject.toLocaleDateString();
    		}
    
    			function addDatem60(dateObject, numDaysm60) {
    			dateObject.setDate(dateObject.getDate() + numDaysm60);
    			return dateObject.toLocaleDateString();
    		}
    
    			function addDatem90(dateObject, numDaysm90) {
    			dateObject.setDate(dateObject.getDate() + numDaysm90);
    			return dateObject.toLocaleDateString();
    		}
    
    
                              oldDate = new Date();
    		oldDate1 = new Date();
    		oldDate2 = new Date();
    		oldDate3 = new Date();
    		oldDate4 = new Date();
    		
    		numDays30 = 30;
    		numDays45 = 45;
    		numDays60 = 60;
    		numDays90 = 90;
    
    		newDate30 = addDate30(oldDate1, numDays30);
    		newDate45 = addDate45(oldDate2, numDays45);
    		newDate60 = addDate60(oldDate3, numDays60);
    		newDate90 = addDate90(oldDate4, numDays90);
    		
    	 		function SelectedDate (InputDate, EnumDays) {
    
    			var SelectDate = ((Date.parse(myform.InputDate.value) / 1000) + EnumDays0);
    			Edate0 = new Date (SelectDate *1000);
    			var eDate0 = (Edate0.toLocaleDateString());
    		
    			var SelectDate = ((Date.parse(myform.InputDate.value) / 1000) + EnumDays30);
    			Edate30 = new Date (SelectDate *1000);
    			var eDate30 = (Edate30.toLocaleDateString());
    		
    			var SelectDate = ((Date.parse(myform.InputDate.value) / 1000) + EnumDays45);
    			Edate45 = new Date (SelectDate *1000);
    			var eDate45 = (Edate45.toLocaleDateString());
    		
    			var SelectDate = ((Date.parse(myform.InputDate.value) / 1000) + EnumDays60);
    			Edate60 = new Date (SelectDate *1000);
    			var eDate60 = (Edate60.toLocaleDateString());
    
    			var SelectDate = ((Date.parse(myform.InputDate.value) / 1000) + EnumDays90);
    			Edate90 = new Date (SelectDate *1000);
    			var eDate90 = (Edate90.toLocaleDateString());
    
    			var SelectDate = ((Date.parse(myform.InputDate.value) / 1000) + EnumDaysM30);
    			EdateM30 = new Date (SelectDate *1000);
    			var eDateM30 = (EdateM30.toLocaleDateString());
    
    			var SelectDate = ((Date.parse(myform.InputDate.value) / 1000) + EnumDaysM45);
    			EdateM45 = new Date (SelectDate *1000);
    			var eDateM45 = (EdateM45.toLocaleDateString());
    
    			var SelectDate = ((Date.parse(myform.InputDate.value) / 1000) + EnumDaysM60);
    			EdateM60 = new Date (SelectDate *1000);
    			var eDateM60 = (EdateM60.toLocaleDateString());
    
    			var SelectDate = ((Date.parse(myform.InputDate.value) / 1000) + EnumDaysM90);
    			EdateM90 = new Date (SelectDate *1000);
    			var eDateM90 = (EdateM90.toLocaleDateString());
    	
    
    		alert(("The date you selected is " + eDate0) + ("\n\nFUTURE CALENDAR DATES from your selected date:\n30 Days is "+ eDate30) + ("\n45 Days is ")+ eDate45 + ("\n60 Days is ")+ eDate60 + ("\n90 Days is ")+ eDate90 +("\n\nPAST FUTURE CALENDAR DATES from your selected date:\n30 Days is ")+ eDateM30 + ("\n45 Days is ")+ eDateM45 + ("\n60 Days is ")+ eDateM60 + ("\n90 Days is ")+ eDateM90);
    	}
    
    		oldDateM1 = new Date();
    		oldDateM2 = new Date();
    		oldDateM3 = new Date();
    		oldDateM4 = new Date();
    
    		numDaysM30 = -(30);
    		numDaysM45 = -(45);
    		numDaysM60 = -(60);
    		numDaysM90 = -(90);
    
    		newDateM30 = addDatem30(oldDateM1, numDaysM30);
    		newDateM45 = addDatem45(oldDateM2, numDaysM45);
    		newDateM60 = addDatem60(oldDateM3, numDaysM60);
    		newDateM90 = addDatem90(oldDateM5, numDaysM90);
    	</script>
    and displaying the dates using
    Code:
    	<span class="style3"><strong><em>Future CALENDAR Dates</em></strong></span><br>
    		<script type="text/javascript">
    			document.write("30 days = " + newDate30 + "");//using computer date
    			document.write("<br>45 days = " + newDate45 + "");
    			document.write("<br>60 days = " + newDate60 + "");
    			document.write("<br>68 days = " + newDate68 + "");
    			document.write("<br>90 days = " + newDate90 + "");
    			document.write("<br>118 days = " + newDate118 + "");
    		</script>
    
    
    	<br><br><span class="style3"><strong><em>Past CALENDAR Dates</em></strong></span>
    		<script type="text/javascript">
    			document.write("<br>30 days = " + newDateM30 + "");//using computer date
    			document.write("<br>45 days = " + newDateM45 + "");
    			document.write("<br>60 days = " + newDateM60 + "");
    			document.write("<br>68 days = " + newDateM68 + "");
    			document.write("<br>90 days = " + newDateM90 + "");
    			document.write("<br>118 days = " + newDateM118 + "");
    		</SCRIPT>
    and an "alert"; which did not always give me a correct date.

    I am using "datetimepicker_ccs/js" from TengYong Ng and
    Code:
    			<FORM NAME="myform">
    				<legend class="style3"><span class="style2"><strong><em>Calculate from Your Own Date</em></strong></span></legend>
    					<input type="Text" NAME="InputDate" class="style1" onchange="SelectedDate(this.form)" value="Select a Date"> 
    		        		<img src=\\cal.gif" onclick="javascript:NewCssCal('InputDate','mmddyyyy','arrow')" style="cursor:hand"><br>
    				<input type="reset" value="Reset Calendar">
    
    			</form>
    to allow the uses to select any date. Now the problem I am having is calculating the Business Dates from the selected date.

  2. #17
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,389
    Once again I will say: You cannot use document.write statements to change the webpage AFTER it has been rendered.
    It will only reload the page with the original contents. Not what you want or expect, I assume.

    Also, why are you using 86400 seconds per day when the solution I provided does not use that method at all.

    Finally, what is the format of the date returned by your datetimepicker_css? I don't use that particular script.
    Rather than mess-up your current webpage design, show a BRIEF example of you datepicker and the code I posted.
    Then we can debug your problem and put the corrections into your original page.

  3. #18
    Join Date
    Aug 2013
    Posts
    8
    Quote Originally Posted by JMRKER View Post
    Once again I will say: You cannot use document.write statements to change the webpage AFTER it has been rendered.
    It will only reload the page with the original contents. Not what you want or expect, I assume.

    Also, why are you using 86400 seconds per day when the solution I provided does not use that method at all.

    Finally, what is the format of the date returned by your datetimepicker_css? I don't use that particular script.
    Rather than mess-up your current webpage design, show a BRIEF example of you datepicker and the code I posted.
    Then we can debug your problem and put the corrections into your original page.
    I was using the "Document.write" to display the dates calculated from the computer's date, so it did not have to refresh when a date was selected.

    I was using 86400 seconds/day in my original code. My new code is using the code you provided.

    The datepicker_ccs.js is 1341 lines so if I included it on this post the post would not be brief.
    The datepicker_ccs.js uses Epoch time to determine the selected date.
    In order to allow a user to select a date and to display the selected date I am using the following code:
    Code:
    	<script src="\\datetimepicker_css.js"></script>
    </head>
    <body>
    <script type="text/javascript">
    
    		var Edate0
    		var InputDate;
    		var SelectDate;
    		var numDays;
    		var getDate;
    		var newDate
    		
    		EnumDays0 = 0
    
    	oldDate = new Date();
    	numDays0 = 0
    	
    	function SelectedDate (InputDate, EnumDays) {
    
    		var SelectDate = ((Date.parse(myform.InputDate.value) / 1000) + EnumDays0);
    			Edate0 = new Date (SelectDate *1000);
    			var eDate0 = (Edate0.toLocaleDateString());
    
    			function addDate0(dateObject, numDays0) {
    			dateObject.setDate(dateObject.getDate() + numDays0);
    			return dateObject.toLocaleDateString();
    			}
    
    
    		alert(("The date you selected is " + eDate0));
    	}
    
    </script>			
    			<FORM NAME="myform">
    				<legend class="style3"><span class="style2"><strong><em>Calculate from Your Own Date</em></strong></span></legend>
    					<input type="Text" NAME="InputDate" class="style1" onchange="SelectedDate(this.form)" value="Select a Date"> 
    		        		<img src="\\cal.gif" onclick="javascript:NewCssCal('InputDate','mmddyyyy','arrow')" style="cursor:hand"><br>
    				<input type="reset" value="Reset Calendar">
    			</form>
    </body>
    </html>
    I am using your code as follows:
    Code:
    <%@ Page Language="C#" %>
    <!DOCTYPE HTML>
    <html>
    <head>
    <META name="WebPartPageExpansion" content="full">
    <title> Fixed & Variable Holidays </title>
    <meta charset="utf-8">
    </head>
    <body>
    <div id="debugDIV"></div>
    
    <script type="text/javascript">
    
    //Business Days from Today's Date
    
    function businessDays(n,D){  // counts business days for a different problem
      D = D || new Date();
      var num = Math.abs(n);
      var tem, count = 0;
      var dir = (n<0)? -1: 1;
      while (count < num) {
        D = new Date(D.setDate(D.getDate()+dir));
        if ( D.isFixedHoliday() || D.isVariableHoliday() ) { continue; } // bypass count if holiday found
        tem = D.getDay();  if(tem != 0 && tem != 6) ++count;
      }
      return D;
    }
    
    Date.prototype.isFixedHoliday=function(){
      var A=[this.getMonth()+1,this.getDate()]
      var hol = {             // fixed dates of year
                 'New Year':[1,1],
    	     	 'Ground Hog Day':[2,2],
                 'Valentine':[2,14],
                 'Independence Day':[7,4],
                 'Christmas':[12,25]
    	    }
      var tem;
      for(var p in hol) {
        tem= hol[p];  if(A[0]==tem[0] && A[1]==tem[1]) return p;  // not false value if match found
      }
      return false;
    }
    
    Date.prototype.variableDate = function(){
    // get month and date - no leading zeroes)
      var A=[this.getMonth()+1,this.getDate()];
    
    // weekday from beginning of the month (month/number_of_week/day_of_week)
      var n_wday = this.getDay(),
      n_wnum = Math.floor((A[1] - 1) / 7) + 1;
      var v_date = A[0] + ',' + n_wnum + ',' + n_wday;
    
      return v_date;
    }
    
    Date.prototype.isVariableHoliday = function(){
      var now = this.variableDate();    // alert(today);
    
      var hol = [  ]; // variable dates of year
    
    /* variable dates of the year */   // &#x10;Check format = 'month (1-12),week in month (1-5), day of week (0=Sun, 6=Sat.)
    //      hol['Current Date'] = '8,2,5';        // test entry for today for testing purposes only (change as needed)
          hol['MLK BDay'] = '1,3,1';            // Birthday of Martin Luther King, third Monday in January
          hol['GW BDay'] = '2,3,1';             // Washington's Birthday, third Monday in February
          hol['Armed Forces Day'] = '5,3,6';    // Armed Forces Day, third Saturday in May
          hol['Labor Day'] = '9,1,1';           // Labor Day, first Monday in September
          hol['Columbus Day'] = '10,2,1';       // Columbus Day, second Monday in October
          hol['Thanksgiving'] = '11,4,4';       // Thanksgiving Day, fourth Thursday in November
          hol['Black Friday'] = '11,4,5';       // Thanksgiving Day +1, fourth Friday in November
    
      var tem;
      for(var p in hol){
        tem= hol[p];  if (now == hol[p]) { return p; }  // return key if match found (not false value)
      }
      return false;
    }
    
    window.onload = function() {
    // sample usage:
      var today = new Date();
      var debug = document.getElementById('debugDIV');
      debug.innerHTML = '<h2><i>Check Fraud Important Date Calculator</i></h2>';
    
      var Dshort = today.toLocaleDateString();
    
      var D1short=businessDays(2).toLocaleDateString();  //string for 3 days after now (today)
      var D2short=businessDays(10).toLocaleDateString();   //object for 8 business days after now (today)
      var D3short=businessDays(-10).toLocaleDateString();   //object for 10 business days before now (today)
    
    //Calendar Days from Today's date
    
    	function calDays(n,C) {
    	C = C || new Date();
    	var num = Math.abs(n);
      	var tem, count = 0;
      	var dir = (n<0)? -1: 1;
      	while (count < num) {
        C = new Date(C.setDate(C.getDate()+dir));
        tem = C.getDay();  if(tem != -1 && tem != 7) ++count;
      }
      return C;
    }
     
      debug.innerHTML += '<i><h3>Today\'s Date:</h3></i> '+Dshort; // is also: '+Dlong;   // display for now (today)
      debug.innerHTML += '<i><h4>Business days from Today\'s Date<br> - Future Date</h4></i>';
      debug.innerHTML += '2 business days: '+D1short; //+' is also: '+D1long;
      debug.innerHTML += '<br>10 business days: '+D2short; //+' is also: '+D2long;
      debug.innerHTML += '<p><h4><i><b>- Past Dates </b></i></h4>';
      debug.innerHTML += '10 business days: '+D3short;
    }
    
    </script>
    </body>
    </html>
    I hope I included everything you are looking for.

  4. #19
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,389
    Quote Originally Posted by nbkcu33 View Post
    I...
    The datepicker_ccs.js is 1341 lines so if I included it on this post the post would not be brief.
    The datepicker_ccs.js uses Epoch time to determine the selected date.
    In order to allow a user to select a date and to display the selected date I am using the following code:
    Code:
    	<script src="\\datetimepicker_css.js"></script>
    ...
    I hope I included everything you are looking for.
    Since you cannot post because of the size, can you provide a link to the datetimepicker_css.js script?
    If not, can you provide a live link to your program for testing?

  5. #20
    Join Date
    Aug 2013
    Posts
    8
    Quote Originally Posted by JMRKER View Post
    Since you cannot post because of the size, can you provide a link to the datetimepicker_css.js script?
    If not, can you provide a live link to your program for testing?
    I found the js script at http://www.rainforestnet.com/datetim...timepicker.htm. On this site they have the code along with examples and demos.

  6. #21
    Join Date
    Aug 2013
    Posts
    8
    I have adapted the code you provided to only calculate date (not business days) for testing purposes.
    Code:
     
      <html>
    <head>
    <META name="WebPartPageExpansion" content="full">
    <title> Fixed & Variable Holidays </title>
    <meta charset="utf-8">	<script src="\\datetimepicker_css.js"></script>
    		<style type="text/css">
    		.style1 {
    		margin-top: 0px;
    			}
    				.style2 {
    				text-align: center;
    			}
    				.style3 {
    				color: #0d2bcc;
    			}
    				.style4 {
    				margin-bottom: 0px;
    			}
    				.style5 {
    				text-align: left;
    			}
    				.style6 {
    				color: #FFFFFF;
    			}
    		</style>
    	</head>
    
    <body>
    <script type="text/javascript">
    
    		var Edate
    		var InputDate;
    		
    	numDays0 = 0
    
    	function SelectedDate (InputDate) {
        
    		var InputDate
    
    		var SelectDate = Date.parse(myform.InputDate.value);
    			Edate = new Date (SelectDate);
    			var eDate = (Edate.toLocaleDateString());
    
    //Calendar Days from Selected date
    
    	function ScalDays(n,S) {
    	S = S || new Date(SelectedDate);
    	var num = Math.abs(n);
      	var tem, count = 0;
      	var dir = (n<0)? -1: 1;
      	while (count < num) {
        S = new Date(S.setDate(S.getDate()+dir));
        tem = S.getDay();  if(tem != -1 && tem != 7) ++count;
      }
      return S;
    }
      
      var D9short=ScalDays(30).toLocaleDateString();   //object for 30 calendar days after now (Selected)
    
    		alert(("The date you selected is " + eDate) + "\n\nFUTURE CALENDAR DATES from your selected date:\n30 Days is "+ D9short);
    }
    </script>
    		<fieldset style="height: 70px; width: 316px;" class="style4">
    			<FORM NAME="myform" style="width: 308px">
    				<legend class="style3"><span class="style2"><strong><em>Calculate from Your Own Date</em></strong></span></legend>
    					<input type="Text" NAME="InputDate" class="style1" onchange="SelectedDate(this.form)" value="Select a Date"> 
    		        		<img src=/cal.gif" onclick="javascript:NewCssCal('InputDate','mmddyyyy','arrow')" style="cursor:hand"><br>
    				<input type="reset" value="Reset Calendar">
    </form>
    </fieldset>
    </body>
    </html>
    I now receive an alert that reads "The date you selected is" eDate (the selected date) "Future Calendar Dates from your selected date: 30 Days is NaN". I am not sure why the D9short date is appearing as NaN since the date should NOT be a number.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles