Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8

Thread: Period of year

  1. #1
    New to the CF scene
    Join Date
    Jul 2004
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Period of year

    Hi all

    First I must say that I'm complete new in javascript. I've done something in php, but not in javascript.

    My problem is calculation for accomodation.

    I've got 5 periods of year

    01/01 - 28/05
    29/05 - 02/07
    03/07 - 03/09
    04/09 - 01/10
    02/10 - 31/12

    Each period of year has his price for accomodation

    for example:

    01/01 - 28/05 35
    29/05 - 02/07 45
    03/07 - 03/09 55
    04/09 - 01/10 45
    02/10 - 31/12 35

    Here comes problem

    If user wants to spend his vacation on period from 28.06 until 07.07 here come two prices.

    Calculation should be: 5 days at price 45 and 5 day on price 55

    Total of this should be 500.

    How to make function to determine period of year
    Last edited by ddragas; 07-07-2004 at 11:02 AM.

  • #2
    New to the CF scene
    Join Date
    Jul 2004
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Any suggestions?

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Posts
    338
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    function mkPeriod(startDate,startMonth,endDate,endMonth,price) {
    	return {
    		sd: startDate,
    		sm: startMonth,
    		ed: endDate,
    		em: endMonth,
    		p: price
    	};
    }
    var periods = [
    	mkPeriod(0,0,28,4,35),
    	mkPeriod(29,4,2,6,45),
    	mkPeriod(3,6,3,8,55),
    	mkPeriod(4,8,1,9,45),
    	mkPeriod(2,9,31,11,35)
    ]
    Date.prototype.getNextDay = function() {
    	var x = this;
    	x.setDate(x.getDate()+1);
    	return x;
    }
    Date.prototype.getPeriodPrice = function() {
    	var startP,endP,y = this.getFullYear();
    	for(var i = 0; i < periods.length; i++) {
    		startP = new Date(y,periods[i].sm,periods[i].sd);
    		endP = new Date(y,periods[i].em,periods[i].ed);
    		if(this>=startP&&this<=endP)
    			return periods[i].p;
    	}
    }
    
    function determinePrice(dateBegin,dateEnd) {
    	var price = 0;
    	while(dateBegin<=dateEnd) {
    		price += dateBegin.getPeriodPrice();
    		dateBegin = dateBegin.getNextDay();
    	}
    	return price;
    }
    Granted, there is a problem if you want the price of a vacation that spans between more than the current year, but otherwise, it should be a pretty good solution.

    Hope that helps!

    Happy coding!
    Last edited by nolachrymose; 07-07-2004 at 12:01 AM.

  • #4
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Looks like nolachrymose has beaten me to the solution! But I've been working on this for a little while, so I thought I'd post my solution as well.

    My solution does take 2 years into account..

    Mine's also a lot longer than his! But it makes more sense to me..
    Code:
    <script>
    var tdyDate = new Date();
    var tdyYear = tdyDate.getFullYear();
    
    var period = new Array();
    
    var j = 0;
    for(var i = 0; i < 2; i++) {
     period[j] = new Array();
     period[j]['start'] = new Date(tdyYear+i, 01, 01);
     period[j]['end'] = new Date(tdyYear+i, 05, 28);
     period[j++]['price'] = 35;
    
     period[j] = new Array();
     period[j]['start'] = new Date(tdyYear+i, 05, 29);
     period[j]['end'] = new Date(tdyYear+i, 07, 02);
     period[j++]['price'] = 45;
    
     period[j] = new Array();
     period[j]['start'] = new Date(tdyYear+i, 07, 03);
     period[j]['end'] = new Date(tdyYear+i, 09, 03);
     period[j++]['price'] = 55;
    
     period[j] = new Array();
     period[j]['start'] = new Date(tdyYear+i, 09, 04);
     period[j]['end'] = new Date(tdyYear+i, 10, 01);
     period[j++]['price'] = 45;
    
     period[j] = new Array();
     period[j]['start'] = new Date(tdyYear+i, 10, 02);
     period[j]['end'] = new Date(tdyYear+i, 12, 31);
     period[j++]['price'] = 35;
    }
    
    function ms_to_days(diff) {
     return (Math.floor(diff / (1000 * 60 * 60 * 24)) + 1);
    }
    
    function calculateCost(startDate, endDate) {
     var cost = 0;
     var days = 0;
     
     var d; 
     for(var i = 0; i < period.length; i++) {	
      if(period[i]['start'].getTime() <= startDate.getTime() && startDate.getTime() <= period[i]['end'].getTime()) {
       //this is the starting period
    	 
       if(period[i]['start'].getTime() <= endDate.getTime() && endDate.getTime() <= period[i]['end'].getTime()) {
        //this is ALSO the ending period
        //cost should bee the diff in days between the startDate and endDate
    		
        var diff_dates = endDate.getTime() - startDate.getTime();
    		
        d = ms_to_days(diff_dates);
        days += d;
        cost += d*period[i]['price'];
        break;
       }
       else {
        //this is NOT the ending period
        //cost is at least the diff in days between the startDate and the end of period date
    		
        var diff_till_end = period[i]['end'].getTime() - startDate.getTime();
    		
        d = ms_to_days(diff_till_end);
        days += d;
        cost += d*period[i]['price'];
       }//else
      }
      else if(startDate.getTime() < period[i]['start'].getTime()) {
       //start date is before this period..
    	 
       if(period[i]['end'].getTime() <= endDate.getTime()) {
        //end date is after this period
        //cost is the length of this period in days
      		
        var diff_period = period[i]['end'].getTime() - period[i]['start'].getTime();
     		
        d = ms_to_days(diff_period);
        days += d;
        cost += d*period[i]['price'];
       }
       else {
        //end date is also in this period
        //cost is the diff of the end date and the start of this period
    		
        var diff_from_start = endDate.getTime() - period[i]['start'].getTime();
        		
        d = ms_to_days(diff_from_start);
        days += d;
        cost += d*period[i]['price'];
        break;
       }//else	 
      }//else if
    	
     }//for
     
     return cost;
    }
    
    
    //test
    alert( calculateCost(new Date(2004, 05, 28), new Date(2005, 05, 29)) );
    </script>
    Hopefully that's of some help.

    Sadiq.

  • #5
    New to the CF scene
    Join Date
    Jul 2004
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thank you both.
    I'll try your codes soon I come home (I'm at work now), and I'll let you know results.

    Regards

  • #6
    New to the CF scene
    Join Date
    Jul 2004
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you Sadiq, script is working fine and it is a great help for me, but I think that something is calculating wrong.

    Price for period from 28.06.2004 untill 07.07.2004 should be 445, and script calculates 545.

    28.06.2004 45,00
    29.06.2004 45,00
    30.06.2004 45,00
    01.07.2004 45,00
    02.07.2004 45,00
    03.07.2004 55,00
    04.07.2004 55,00
    05.07.2004 55,00
    06.07.2004 55,00

    Total: 445,00

    If you would be so kind and check code please

    Regards

    Thank you
    Last edited by ddragas; 07-07-2004 at 12:02 PM.

  • #7
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Oops! I think I know why:

    Javascript dates start at 0, not 1. So I just substracted the months and days by 1 and it seems to work:
    Code:
    var period = new Array();
    
    var j = 0;
    for(var i = 0; i < 2; i++) {
     period[j] = new Array();
     period[j]['start'] = new Date(tdyYear+i, 00, 00);
     period[j]['end'] = new Date(tdyYear+i, 04, 27);
     period[j++]['price'] = 35;
    
     period[j] = new Array();
     period[j]['start'] = new Date(tdyYear+i, 04, 28);
     period[j]['end'] = new Date(tdyYear+i, 06, 01);
     period[j++]['price'] = 45;
    
     period[j] = new Array();
     period[j]['start'] = new Date(tdyYear+i, 06, 02);
     period[j]['end'] = new Date(tdyYear+i, 08, 02);
     period[j++]['price'] = 55;
    
     period[j] = new Array();
     period[j]['start'] = new Date(tdyYear+i, 08, 03);
     period[j]['end'] = new Date(tdyYear+i, 09, 00);
     period[j++]['price'] = 45;
    
     period[j] = new Array();
     period[j]['start'] = new Date(tdyYear+i, 09, 01);
     period[j]['end'] = new Date(tdyYear+i, 11, 30);
     period[j++]['price'] = 35;
    }
    Let me know how it goes.

    Sadiq.

  • #8
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    BTW: Although the O/P neglected to update this thread to indicate the accepted solution.
    For the archives here, I will post solution which was accepted elsewhere....

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>Untitled</title>
    <script type="text/javascript">
    <!--
    var period1=40;
    var period2=50;
    var period3=60;
    var period4=50;
    var period5=40;
    // -->
    </script>
    <script type="text/javascript">
    <!--
    Date.prototype.getRate = function () {
    if (this.getTime() < new Date(this.getFullYear(), 4, 29)) return period1;
    if (this.getTime() < new Date(this.getFullYear(), 6, 3)) return period2;
    if (this.getTime() < new Date(this.getFullYear(), 8, 4)) return period3;
    if (this.getTime() < new Date(this.getFullYear(), 9, 2)) return period4;
    return period5;
    }
    Date.ONE_SECOND = 1000;
    Date.ONE_MINUTE = Date.ONE_SECOND * 60;
    Date.ONE_HOUR = Date.ONE_MINUTE * 60;
    Date.ONE_DAY = Date.ONE_HOUR * 24;
    function getPrice (startDate, endDate) {
    var a = 0;
    var d = new Date (startDate);
    var e = new Date (endDate);
    while (d < e) {
    a += d.getRate();
    d = new Date(d.getTime() + Date.ONE_DAY)
    }
    return a;
    }
    alert (getPrice ('5 may 2004', '7 may 2004'))
    // -->
    </script>
    </head>
    <body>
    </body>
    </html>


  •  

    Posting Permissions

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