Solved

Javascript is not giving the correct output - problem with dates array

Posted on 2004-08-18
3
195 Views
Last Modified: 2010-04-06
Hi,

Probably need a second set of eyes to go over my code.

I am designing a vacation request form for my users. I have some javascript that accounts for holidays as well as weekends while calculating the number of days between two days.
I got most of this code functioning - however for some reason, my code is not reading the last array VacDays (list of holidays during the year) and taking them into account while doing the calculations for myTotal or RealTotal at the end.

Thus if I put start date = 02/13/2004 and end date = 02/17/2004 - it should be giving me two days between these two days instead of 3 as 02/16/2004 is listed as a holiday in the array VacDays. However its currently giving me 3 days - not sure where I am going wrong.

Please help!!

A.


function CalcVacDays(FirstDay, LastDay){
{
var FirstDay;
var LastDay;

FirstDay = new Date(document.myForm._1_1_7_1.value);
LastDay = new Date(document.myForm._1_1_8_1.value);

var d1v = FirstDay.valueOf();
var d2v = LastDay.valueOf();
d1v= Math.ceil((((d1v/1000)/60)/60)/24);
d2v= Math.ceil((((d2v/1000)/60)/60)/24);
if (d2v>=d1v)
{
     var TotalDays = d2v-d2v+1;
     
     myTotal.value = TotalDays;
     var RealTotal = TotalDays;
     

// Starting loop to figure time

var startCount
var sdate = 0
var sDate = FirstDate.getDay()


for(startCount=0, startCount<days,startCount++)
     {
          if ((sdate=5)||(sdate=6)) RealTotal--
               sdate++;
          if (sdate=7) sdate=0
     }

     var VacDays = new Array()
     VacDays[0]=new Date("01/01/2005")
     VacDays[0]=Math.ceil((((VacDays[0].valueOf()/1000)/60)/60)/24)
     VacDays[1]=new Date("01/02/2005")
     VacDays[1]=Math.ceil((((VacDays[0].valueOf()/1000)/60)/60)/24)
     VacDays[2]=new Date("02/16/2004")
     VacDays[2]=Math.ceil((((VacDays[1].valueOf()/1000)/60)/60)/24)
     VacDays[3]=new Date("05/31/2004")
     VacDays[3]=Math.ceil((((VacDays[2].valueOf()/1000)/60)/60)/24)
     VacDays[4]=new Date("07/05/2004")
     VacDays[4]=Math.ceil((((VacDays[3].valueOf()/1000)/60)/60)/24)
     VacDays[5]=new Date("09/06/2004")
     VacDays[5]=Math.ceil((((VacDays[4].valueOf()/1000)/60)/60)/24)
     VacDays[6]=new Date("11/25/2004")
     VacDays[6]=Math.ceil((((VacDays[5].valueOf()/1000)/60)/60)/24)
     VacDays[7]=new Date("11/26/2004")
     VacDays[7]=Math.ceil((((VacDays[6].valueOf()/1000)/60)/60)/24)
     VacDays[8]=new Date("12/24/2004")
     VacDays[8]=Math.ceil((((VacDays[7].valueOf()/1000)/60)/60)/24)
     VacDays[9]=new Date("12/27/2004")
     VacDays[9]=Math.ceil((((VacDays[8].valueOf()/1000)/60)/60)/24)
     VacDays[10]=new Date("12/28/2004")
     VacDays[10]=Math.ceil((((VacDays[9].valueOf()/1000)/60)/60)/24)
     VacDays[11]=new Date("12/29/2004")
     VacDays[11]=Math.ceil((((VacDays[10].valueOf()/1000)/60)/60)/24)
     VacDays[12]=new Date("12/31/2004")
     VacDays[12]=Math.ceil((((VacDays[10].valueOf()/1000)/60)/60)/24)

     var hvalue
     for (startCount=0,startCount<=12,startCount++)
     {
          hvalue=VacDays[startCount]
          if ((hvalue>=d2v) && (hvalue<=d1v))
               {
                 RealTotal--
               }
     }
          myTotal.value = RealTotal
document.write (RealTotal)


}

}
0
Comment
Question by:arunatata
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 19

Accepted Solution

by:
dakyd earned 200 total points
ID: 11844699
This section looks problematic:
for(startCount=0, startCount<days,startCount++)
     {
          if ((sdate=5)||(sdate=6)) RealTotal--
               sdate++;
          if (sdate=7) sdate=0
     }

You need to change the commas to semi-colons in the for loop.  The second thing that's weird is that you don't define days anywhere.  Odds are, your script is treating days as 0.  I think you meant this:
for(startCount=0; startCount<TotalDays; startCount++)
     {
          if ((sdate=5)||(sdate=6))
            RealTotal--
          sdate++;
          if (sdate=7) sdate=0;
     }


The second for loop also looks like it has a couple errors.  I think you want this:
     var hvalue;
     for (startCount=0;startCount<=12; startCount++)
     {
          hvalue=VacDays[startCount]
          if ((hvalue>=d1v) && (hvalue<=d2v))
               {
                 RealTotal--
               }
     }
d2v is greater/after d1v, so you want a value that is between d1v and d2v.  The way you had it before was decrementing RealTotal only if hValue was less than d1v and greater than d2v, which shouldn't ever happen.

Regardless, hope that helps.
0
 

Author Comment

by:arunatata
ID: 11845014
Thanks, that helped!!

A.
0
 
LVL 19

Expert Comment

by:dakyd
ID: 11845149
Sure thing, glad you got what you wanted.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
XSL Grouping 7 43
Google adds suggestions 3 77
Problem to run file 9 130
Some high points and examples of what's great about Facebook React? 5 221
There are two main kinds of selectors in CSS: One is base selector like h1, h2, body, table or any existing HTML tags.  For instance, the following rule sets all paragraphs (<p> elements) to red: (CODE) CSS also allows us to define our own custom …
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question