www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 21

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

  1. #1
    Join Date
    Feb 2007
    Posts
    3

    Question Help Customizing Script to Subtract Business Days from Current Date

    Hi all, I'm working on something for my job that requires a script that will display a date x number of business days in the past. (Actually two scripts - one for 3 business days ago, and one for 8 business days ago - but I just need to get one of them done, and I'll be set!!) I found a script online that will count days in the FUTURE and skip weekends, however, when I tried to modify it to count backwards, it no longer skips weekend days. Can someone take a look at my Franken-Script and let me know if I forgot to change a setting or something that will allow it to count backwards and NOT include weekend days? It displays in the format I want already, but it can look as pretty as it wants - it won't help if the date is wrong!

    For example, the script below, when run today (Feb 2), returns January 25 - 8 days ago - instead of January 23 - 8 BUSINESS days ago.


    <script language=JavaScript>
    function getNextBusinessDay() {
    return getDeliveryDateObj(-8);
    }

    function getDeliveryDateObj(businessDaysLeftForDelivery) {
    var now = new Date();
    var dayOfTheWeek = now.getDay();
    var calendarDays = businessDaysLeftForDelivery;
    var deliveryDay = dayOfTheWeek + businessDaysLeftForDelivery;
    if (deliveryDay >= 6) {
    businessDaysLeftForDelivery -= 6 - dayOfTheWeek; //deduct this-week days
    calendarDays += 2; //count this coming weekend
    deliveryWeeks = Math.floor(businessDaysLeftForDelivery / 5); //how many whole weeks?
    calendarDays += deliveryWeeks * 2; //two days per weekend per week
    }
    now.setTime(now.getTime() + calendarDays * 24 * 60 * 60 * 1000);
    return now;
    }


    var now = getNextBusinessDay();

    var months = new Array('January','February','March','April','May','June','July','August','September','October','Novem ber','December');

    var date = ((now.getDate()<10)? "0" : "")+ now.getDate();

    daymonth = months[now.getMonth()]
    wkday = date

    document.write("<p align=center><b><font face=Verdana size=1 color=blue>");
    document.write(daymonth);
    document.write("</font><br><font face=Verdana size=6 color=blue>");
    document.write(wkday);
    document.write("</font></b></p>");
    </script>

  2. #2
    Join Date
    Nov 2006
    Posts
    528
    One question. What do you want to happen if "today" is not a business day?

    for example, if "today" was Friday and you wanted to count back 3 days you get Tuesday. But if "today" is Sat , do you want it to return Tuesday or Wednesda. In otherwords, do you want it treated as Friday or Monday? I would guess Monday, but want to be sure.
    Last edited by mjdamato; 02-02-2007 at 01:38 PM.

  3. #3
    Join Date
    Feb 2007
    Posts
    3

    Cool

    Well, I'm not thinking that's going to be an issue... Our agents only work Monday through Friday, and are only on the site those days. But I guess for the sake of the question, "today" being a weekend day would be best treated as a Monday.

  4. #4
    Join Date
    Feb 2006
    Posts
    2,927
    This function will return the Date that is n business days from today.
    If you pass a negative number, it will return a Date n business days in the past.
    You could adapt it for your script- restrict the input, format the return to a string, account for holidays, add a day if the time is after business hours, whatever.

    Code:
    function businessDays(n){
    	var 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));
    		tem=D.getDay();
    		if(tem!=0 && tem!=6) ++count;
    	}
    	return D;
    }
    sample uses:

    var D=businessDays(-3).toLocaleDateString(); //string
    var D=businessDays(8); //object
    Last edited by mrhoo; 02-02-2007 at 02:24 PM.

  5. #5
    Join Date
    Nov 2006
    Posts
    528
    Well, my script is a little longer than mrhoo's but I think it is more efficient as it does not need to loop through every day. Just pass it the number of business days to adjust from today: positive or negative.

    Code:
    function getDeliveryDateObj(businessDays) { 
      businessDays = Math.abs(businessDays);
      var now = new Date();
      var dayOfTheWeek = now.getDay();
    
      if (dayOfTheWeek==0 || dayOfTheWeek==6) { //adjust if start date is a weekend
        dayOfTheWeek=(businessDays<0)?1:5;
      }
    
      weeks = (businessDays/5);
    
      if (businessDays>0) { //Business days in the future
        wholeWeek = Math.floor(weeks);
        partsWeek = businessDays-(wholeWeek*5);
        if ((dayOfTheWeek+partsWeek)>5) { partsWeek += 2; }
      }
    
      if (businessDays<0) {  //Business days in the past
        wholeWeek = Math.ceil(weeks);
        partsWeek = businessDays-(wholeWeek*5);
        if ((dayOfTheWeek+partsWeek)<1) { partsWeek -= 2; }
      }
    
      var daysToMove = partsWeek + (wholeWeek * 7);
      now.setTime(now.getTime() + daysToMove * 24 * 60 * 60 * 1000);
    
      return now;
    }
    However, mrhoo's script could be modified to account for holidays. You could create an array of the holidays that should not be counted as business days and have those skipped as well.
    Last edited by mjdamato; 02-02-2007 at 03:21 PM.

  6. #6
    Join Date
    Feb 2006
    Posts
    2,927
    You may want to find the date n business days before or after some arbitrary date-
    10 business days after the first of the month, say.
    If so, pass the function a second argument for the starting Date.

    And I included a skeleton for holiday checking...

    Code:
    function businessDays(n,D){
    	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.isHoliday())continue;
    		tem=D.getDay();
    		if(tem!=0 && tem!=6) ++count;
    	}
    	return D;
    }
    Code:
    Date.prototype.isHoliday=function(){
    	var A=[this.getMonth()+1,this.getDate()]
    	var hol={
    		'Christmas':[12,25],
    		'GroundHogDay':[2,2]
    	}
    	var tem;
    	for(var p in hol){
    		tem= hol[p];
    		if(A[0]==tem[0] && A[1]==tem[1]) return p;
    	}
    	return false;
    }
    Last edited by mrhoo; 02-02-2007 at 03:42 PM.

  7. #7
    Join Date
    Feb 2007
    Posts
    3
    Wow, thanks both of you for all your help!!!!! I'm using mjdamato's for now since it's pretty much grab-n-go (THANKYOUTHANKYOUTHANKYOU), but I'm going to play around with what mrhoo gave me as well to see if I can't get some holidays programmed in there as well!!!! That would also be a wonderful feature so I wouldn't have to remember to account for them later!

    Again, guys, I can't thank you enough for all your help!!!! I would give you both a cookie if I could!
    Last edited by rwest81; 02-05-2007 at 01:36 PM.

  8. #8
    Join Date
    Aug 2013
    Posts
    8
    I am new to JavaScript and I have been looking at mrhoo's script. I have a test page where I am seeing how to manipulate the script to account for dates in the post and how to select a date to calculate from.
    I have replace (n) in the line var num=Math.abs with a negative number and I only get today’s date.
    I have also replaced D.getDate with a variable and the script returns "undefined".
    Can anyone please shed a little light on my issues?

  9. #9
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,277
    To work with holidays. It's possible to build javascript files for each years with PHP with something like this (the date('z',$timestamp) function give the day of year from 0 to 365)

    (the datas are made for France where holidays are : 1er janvier, 1er et 8 mai, lundi de Pâques, lundi de Pentecôte, 14 juillet, 15 août, 1er novembre, 11 novembre et 25 décembre Noël.

    Code:
    $vcs=str_repeat("000000",61);// a string with 366 zeros 
            $vcs[0]=1; // 1er janvier 
            $vcs[date('z',easter_date($year)+129600)]=1;// Lundi Pâques
            $vcs[date('z',easter_date($year)+38*86400+129600)]=1; // Ascension
            $vcs[date('z',easter_date($year)+49*86400+129600)]=1;// Pentecôte
            $vcs[date('z',mktime(12,0,0,5,1,$year))]=1; // 1er mai
            $vcs[date('z',mktime(12,0,0,5,8,$year))]=1; // 8 mai
            $vcs[date('z',mktime(12,0,0,7,14,$year))]=1;// 14 juillet
            $vcs[date('z',mktime(12,0,0,8,15,$year))]=1;// 15 août
            $vcs[date('z',mktime(12,0,0,11,1,$year))]=1;// 1er novembre
            $vcs[date('z',mktime(12,0,0,11,11,$year))]=1;// 11 novembre
            $vcs[date('z',mktime(12,0,0,12,25,$year))]=1;// 25 décembre
    
    $cnt = 'var hdy="'.$vcs.'";
    file_put_content('hollidays2013.js',cnt);
    Then with javascript this prototype allows to test if a date is a hollidays ( hdy.substr(dayOfYear,1) is true)

    Code:
    Date.prototype.getDayOfYear=function(){var a=this.getFullYear(),m=this.getMonth()+1,r=this.getDate(); 
        while(--m) {r+=new Date(a,m,0).getDate();}
        return r-1;
    };
    Since we use calendar on this form
    Last edited by 007Julien; 08-07-2013 at 07:38 PM.

  10. #10
    Join Date
    Aug 2013
    Posts
    8
    I understand how to build out the holiday array.
    My question is how would incorporate “datetimepicker css.js” into this script to allow users to select their own date. I need to and calculate the date 10 business days, including holidays, in the future and in the past from the date the user selects.

  11. #11
    Join Date
    Aug 2013
    Posts
    8
    Allow me to provide some additional information. I have a need to calculate 10 and 30 business days in the future and into the past form both "today’s date" and a date selected by the user.

    My code follows:
    <title>Business Days from Today's Date</title>
    <script src="\\datetimepicker_css.js"></script>
    <style type="text/css">
    .style2 {
    text-align: center;
    }
    .style3 {
    color: #0d2bcc;
    }
    </style>

    </head>

    <body>
    <fieldset style="width: 314px">
    <legend class="style3"><span class="style2"><strong><em>Business Days from Today's Date</em></strong></span></legend>
    <body onload="businessDays2()"></body>

    <SCRIPT LANGUAGE="JavaScript">
    function businessDays10(n,D){
    D = D || new Date();
    var num=Math.abs(10);
    var tem,count=0;
    var dir= (n<0)? -1: 1;
    while(count< num){
    D= new Date(D.setDate(D.getDate()+dir));
    if(D.isHoliday())continue;
    tem=D.getDay();
    if(tem!=0 && tem!=6) ++count;
    }
    return D;
    }
    function businessDaysM10(n,D){
    D = D || new Date();
    var num=Math.abs(10);
    var tem,count=0;
    var dir= (n<0)? -1: 1;
    while(count< num){
    D= new Date(D.setDate(D.getDate()+dir));
    if(D.isHoliday())continue;
    tem=D.getDay();
    if(tem!=0 && tem!=6) ++count;
    }
    return D;
    }

    Date.prototype.isHoliday=function(){
    var A=[this.getMonth()+1,this.getDate()]
    var hol={ //Manually update Holiday Dates
    'New Years Day':[1,1,2013],
    'Martin Luther King, Jr. Day':[1,21,2013],
    'Presidents’ Day':[2,18,2013],
    'Memorial Day':[5,27,2013],
    'Independence Day':[7,4,2013],
    'Labor Day':[9,2,2013],
    'Columbus Day':[10,14,2013],
    'Veterans Day':[11,11,2013],
    'Thanksgiving Day':[11,28,2013],
    'Christmas':[12,25,2013]
    }
    var tem;
    for(var p in hol){
    tem= hol[p];
    if(A[0]==tem[0] && A[1]==tem[1]) return p;
    }
    return false;
    }
    </script>

    <span class="style3"><strong><em>Future CALENDAR Dates</em></strong></span><br>
    10 Business Days =
    <SCRIPT LANGUAGE="JavaScript">
    var str = (businessDays10().toLocaleDateString())
    document.write (str.fontsize(3))
    </script>
    <br>
    <br>
    <span class="style3"><strong><em>Past CALENDAR Dates</em></strong></span><br>
    10 Business Days =
    <SCRIPT LANGUAGE="JavaScript">
    var str = (businessDaysM10(-10).toLocaleDateString())
    document.write (str.fontsize(3))
    </script>

    </fieldset>

    <SCRIPT LANGUAGE="JavaScript">

    var InputDate;
    var SelectDate;
    var numDays;
    var getDate;
    var newDate

    var Edate30
    var bDate10

    EnumDays0 = 0
    EnumDays30 = 2592000 //(30 days) 2629743 (1 month=30.44 Days)

    function addDate30(dateObject, numDays30) {
    dateObject.setDate(dateObject.getDate() + numDays30);
    return dateObject.toLocaleDateString();
    }


    var EdateM30

    EnumDaysM30 = -2592000

    function addDatem30(dateObject, numDaysm30) {
    dateObject.setDate(dateObject.getDate() + numDaysm30);
    return dateObject.toLocaleDateString();
    }

    oldDate = new Date();
    oldDate1 = new Date();

    numDays30 = 30;

    newDate30 = addDate30(oldDate1, numDays30);

    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) + EnumDaysM30);
    EdateM30 = new Date (SelectDate *1000);
    var eDateM30 = (EdateM30.toLocaleDateString());

    oldDateM = new Date();
    oldDateM1 = new Date();


    numDaysM30 = -(30);


    newDateM30 = addDatem30(oldDateM1, numDaysM30);


    alert(("The date you selected is " + eDate0) + ("\n\nFUTURE CALENDAR DATES from your selected date:\n30 Days is "+ eDate30) + ("\n\nPAST CALENDAR DATES from your selected date:\n30 Days is ")+ eDateM30);
    }
    </script>

    <br>
    <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">
    </html>

    All of the calendar date calculations for correctly but the 10 business days in the past returns “today’s date” and I can’t get the selected date to pass to the business day calculator.
    I hope this clarifies everything that I am trying to accomplish.

  12. #12
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,277
    Don't use 86400. With dayligth saving time all days are not 86400 secondes ! Its a very rare error cause, but a common misstake !

    Prefers always your right form :
    Code:
    dateObject.setDate(dateObject.getDate() + numDay);
    NB : Please insert your script in *[* CODE *]* *[* / CODE *]* (whitout * and spaces) tags or click on the button Go Advanced to use the button <>
    Last edited by 007Julien; 08-09-2013 at 09:33 AM.

  13. #13
    Join Date
    Aug 2013
    Posts
    8
    So here is my complete script. When the script is run it displays 4 sections:
    Section 1 - today's date
    2 - business days from today's date
    3 - calendar dates calculated from today's daye
    4 - select any date to calculate from.

    My problems are:
    I need to get the user's selected date to pass to the Business day calculator and Alert the correct dates.
    If I do not use 86400 seconds per day I would I calculate 30, 45, 60 and 90 daysin the future and past.

    I am sorry if you have explained how I would accomplish this alreay, but I do not understand.


    Code:
    <html lang="en">
    <head>
    <META name="WebPartPageExpansion" content="full">
    <title>Date Calculator</title>
    <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>
    <table border="0" cellpadding="3"><td valign="top" style="width: 450px">
    <fieldset>
    <legend class="style3"><span class="style2"><strong><em>CALENDAR DATE CALCULATOR</em></strong></span></legend>
    <div class="style2">
    
    <fieldset>
    <legend class="style3"><span class="style2"><strong><em>Today's Date</em></strong></span></legend>
    <script type="text/javascript" >
    var t = new Date();
    var ldt = t.toLocaleDateString(); 
    document.write(ldt.bold())
    </script>
    </fieldset>
    
    
    <fieldset>
    <legend class="style3"><span class="style2"><strong><em>Business Days from Today's Date</em></strong></span></legend>
    <body onload="businessDays2()"></body>
    
    <SCRIPT LANGUAGE="JavaScript">
    function businessDays2(n,D){
    D = D || new Date();
    var num=Math.abs(2);
    var tem,count=0;
    var dir= (n<0)? -1: 1;
    while(count< num){
    D= new Date(D.setDate(D.getDate()+dir));
    if(D.isHoliday())continue;
    tem=D.getDay();
    if(tem!=0 && tem!=6) ++count;
    }
    return D;
    }
    function businessDays10(n,D){
    D = D || new Date();
    var num=Math.abs(10);
    var tem,count=0;
    var dir= (n<0)? -1: 1;
    while(count< num){
    D= new Date(D.setDate(D.getDate()+dir));
    if(D.isHoliday())continue;
    tem=D.getDay();
    if(tem!=0 && tem!=6) ++count;
    }
    return D;
    }
    function businessDaysM2(n,D){
    D = D || new Date();
    var num=Math.abs(-2);
    var tem,count=0;
    var dir= (n<0)? -1: 1;
    while(count< num){
    D= new Date(D.setDate(D.getDate()+dir));
    if(D.isHoliday())continue;
    tem=D.getDay();
    if(tem!=0 && tem!=6) ++count;
    }
    return D;
    }
    function businessDaysM10(n,D){
    D = D || new Date();
    var num=Math.abs(-10);
    var tem,count=0;
    var dir= (n<0)? -1: 1;
    while(count< num){
    D= new Date(D.setDate(D.getDate()+dir));
    if(D.isHoliday())continue;
    tem=D.getDay();
    if(tem!=0 && tem!=6) ++count;
    }
    return D;
    }
    Date.prototype.isHoliday=function(){
    var A=[this.getMonth()+1,this.getDate()]
    var hol={ //Manually update Holiday Dates
    'New Years Day':[1,1,2013],
    'Martin Luther King, Jr. Day':[1,21,2013],
    'Presidents’ Day':[2,18,2013],
    'Memorial Day':[5,27,2013],
    'Independence Day':[7,4,2013],
    'Labor Day':[9,2,2013],
    'Columbus Day':[10,14,2013],
    'Veterans Day':[11,11,2013],
    'Thanksgiving Day':[11,28,2013],
    'Christmas':[12,25,2013]
    }
    var tem;
    for(var p in hol){
    tem= hol[p];
    if(A[0]==tem[0] && A[1]==tem[1]) return p;
    }
    return false;
    }
    </script> 
    <span class="style3"><strong><em>Future Dates</em></strong></span><br>
    2 Business Days =
    <SCRIPT LANGUAGE="JavaScript"> 
    var str = (businessDays2(2).toLocaleDateString())
    document.write (str.fontsize(3))
    </script> 
    <br>
    10 Business Days =
    <SCRIPT LANGUAGE="JavaScript">
    var str = (businessDays10(10).toLocaleDateString())
    document.write (str.fontsize(3)) 
    </script>
    <br>
    <br><span class="style3"><strong><em>Past Date</em></strong></span><br>
    10 Business Days =
    <SCRIPT LANGUAGE="JavaScript">
    var str = (businessDaysM10(-10).toLocaleDateString())
    document.write (str.fontsize(3))
    </script>
    </fieldset>
    
    <fieldset>
    <strong>
    <legend class="style3"><span class="style2"><strong><em>Calculated Dates from Today's Date</em></strong></span></legend>
    </strong>
    
    <SCRIPT LANGUAGE="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(oldDate5, 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 CALENDAR DATES from your selected date:\n30 Days is ")+ eDateM30 + ("\n45 Days is ")+ eDate45 + ("\n60 Days is ")+ eDate60 + ("\n90 Days is ")+ eDateM90);
    }
    
    oldDateM = new Date();
    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>
    
    <span class="style3"><strong><em>Future Dates</em></strong></span><br>
    <SCRIPT LANGUAGE="JavaScript">
    document.write("30 days = " + newDate30 + "");//using computer date
    document.write("<br>45 days = " + newDate45 + "");
    document.write("<br>60 days = " + newDate60 + "");
    document.write("<br>90 days = " + newDate90 + "");
    </script>
    
    <br><br><span class="style3"><strong><em>Past Dates</em></strong></span>
    <SCRIPT LANGUAGE="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>90 days = " + newDateM90 + "");
    </SCRIPT>
    </fieldset> 
    
    <fieldset style="height: 70px" class="style4">
    <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>

  14. #14
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,443

    Lightbulb

    Here is a different take on your problem.
    Note, you did not supply code for the datepicker script, so I left that out.
    Also, your code looks a bit dated as the use of 'language="Javascript"' is deprecated
    and you should not use document.write unless it is for initial code testing only as it may also go the way of the dodo soon.
    Code:
    <!DOCTYPE HTML>
    <html>
    <head>
    <title> Fixed & Variable Holidays </title>
    <meta charset="utf-8">
    
    </head>
    <body>
    <div id="debugDIV"></div>
    
    <script type="text/javascript">
    
    // From post #6 of
    // http://www.webdeveloper.com/forum/showthread.php?136969-Help-Customizing-Script-to-Subtract-Business-Days-from-Current-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 */   // 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 = '<h1> Business Day &amp; Holiday Counter </h1>';
    
      var Dlong = today;
      var Dshort = today.toLocaleDateString();
    
      var D1long=businessDays(-3);                        //string for 3 days before now (today)
      var D1short=businessDays(-3).toLocaleDateString();  //string for 3 days before now (today)
      var D2long=businessDays(8);                         //object for 8 business days after now (today)
      var D2short=businessDays(8).toLocaleDateString();   //object for 8 business days after now (today)
    
      debug.innerHTML += 'Today: '+Dshort+' is also: '+Dlong;   // display for now (today)
      debug.innerHTML += '<p>Business days:';
      debug.innerHTML += '-3 business days: '+D1short+' is also: '+D1long;
      debug.innerHTML += '<br>+8 business days: '+D2short+' is also: '+D2long;
      debug.innerHTML += '</p>';
      debug.innerHTML += '<p>Today test: '+today.variableDate()+' (month,week,day)'; // current day as a string variable
      debug.innerHTML += 'Fixed Holiday? : '+today.isFixedHoliday();                 // test for today being a fixed holiday
      debug.innerHTML += '<br>Variable Holiday? : '+today.isVariableHoliday();       // test for today being a variable holiday
      debug.innerHTML += '</p>';
    
      var str = '<p><b> 30, 45, 60 &amp; 90 BUSINESS days +/- from today</b>';
      str += '<br>-90: '+businessDays(-90);
      str += '<br>-60: '+businessDays(-60);
      str += '<br>-45: '+businessDays(-45);
      str += '<br>-30: '+businessDays(-30);
      str += '<br>-14: '+businessDays(-14);
      str += '<br>-7: '+businessDays(-7);
      str += '<br><b>'+today+'</b>';
      str += '<br>+7: '+businessDays(7);
      str += '<br>+14: '+businessDays(14);
      str += '<br>+30: '+businessDays(30);
      str += '<br>+45: '+businessDays(45);
      str += '<br>+60: '+businessDays(60);
      str += '<br>+90: '+businessDays(90);
     
      debug.innerHTML += str+'</p>';
    }
    
    </script>
    
    </body>
    </html>

  15. #15
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,277
    Unless I'm mistaken, I saw a variable EnumDaysM30 = -2592000 witch seems to use 30 days of 86400 seconds ? I have not extended the investigation to further use of this kind of variables, but just wanted to warn you !

    Here is another calendar of school Holydays in France (this changes with academic areas)...

    Connected teachers have the ability (with Ajax calls in a php version) to update new years (see 2015 ...) by clicking on months or days while holding down the Ctrl key.

    With holidays it could be an other way to display your four sections...

    Good luck !
    Last edited by 007Julien; 08-09-2013 at 07:16 PM.

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