1

I've created a booking calculator by date using JavaScript.

Basically I want the base price to be £25 for 1 day (24 hours) or less, and £10 for each additional day (each additional 24 hours).

Below is the main part of the code.

jQuery(document).ready(function($) {

var prequote=25.00;
var taxa = 10.00;
var hoje = new Date();
hoje=hoje.getTime();
i = 0;

 $('#quote').click(function(event) {

    var d1= $('#d1').val();
    var d2= $('#d2').val();
    var t1= $('#t1').val();
    var t2= $('#t2').val();

    console.log(d1);
    console.log(d2);
    console.log(t1);
    console.log(t2);

    // end - start returns difference in milliseconds 
    var date2 = new Date(d2);
    var date1 = new Date(d1);
    var millisecondsPerDay = 1000 * 60 * 60 * 24;
    var millisBetween = date2.getTime() - date1.getTime();
    // get days
    var d = millisBetween / millisecondsPerDay;
    //alert ('value of days is:' +d);
    //alert ( new Date("1970-1-1 " + t2) - new Date("1970-1-1 " + t1) ) / 1000 / 60 / 60;
    var h= ( new Date("1970-1-1 " + t2) - new Date("1970-1-1 " + t1) ) / 1000 / 60 / 60; 
    //alert ('value of hours is:' +h);      
     t1 =t1.split(':');
     t2 =t2.split(':');


    var dat1 = d1.split("-"); 
    var dd1 = dat1[2];
    var mm1 = dat1[1];
    var yy1 = dat1[0];
    var hh1 = t1[0];
    var ms1 = t1[1];

    var dat2 = d2.split("-"); 
    var dd2 = dat2[2];
    var mm2 = dat2[1];
    var yy2 = dat2[0];
    var hh2 =t2[0];
    var ms2 = t2[1];


     var x1 = yy1 + ',' + mm1 + ',' + dd1 + ' ' + hh1 + ':' + ms1;
     var x2 = yy2 + ',' + mm2 + ',' + dd2 + ' ' + hh2 + ':' + ms2;

     var ent = dd1 + '/'+ mm1 +'/'+yy1+' '+ hh1 + ':' + ms1;
     var ext = dd2 + '/'+ mm2 +'/'+yy2+' '+ hh2 + ':' + ms2;


     var xi = yy1 + ',' + mm1 + ',' + dd1 ;
     var xj = yy2 + ',' + mm2 + ',' + dd2 ;


      var start =new Date(x1);
      //var start_i =new Date(xi);
      var end = new Date(x2);
     // var end_i = new Date(xj);
      start = start.getTime();
      end= end.getTime(); 
      if(start === end){
            alert('Min rental days is 1');
        }
        else if(start < end){
            //  hh1 = parseInt(hh1);ms1 = parseInt(ms1);hh2 = parseInt(hh2);ms2 = parseInt(ms2);;
                /*while(start_i < end_i){
                     i++;
                     var newDate = start_i.setDate(start_i.getDate() + 1);
                       start_i = new Date(newDate);                           
                }*/

                i=d;
                if(i >= 1 ){
                    if(h > 0 ){
                        i=i+1;
                    }
                    prequote = prequote + (taxa * (i-2));
                    prequote =  parseFloat(prequote.toFixed(2));
                }    

                 $('#en-tex').text(ent);
                 $('#ex-t').text(ext);
                 $('#prequote').html(prequote);
                 $('#modal-img').modal('show'); 
                 prequote=25.00;

                $('#tupd').val(ent);
                $('#tdod').val(ext);
        }
        else{
            alert('Please fill in all the date and time fields.');
        }


 });

The 1st issue is, if I select for example Monday 21st at 9:00am to Tuesday 22nd at 9:00am it doesn't count the fee as for 24 hours. Only if the end date is AFTER 9:00am.

Likewise for longer dates, it only charges for a day AFTER 24 hours and not from 24 hours on the dot.

2nd issue is, if somebody selects less than 24 hours (i.e Monday 21st at 9:00am to Tuesday 22nd at 7:00am) it minuses the £10 from £25. I want it to still quote the base price of £25.

j08691
  • 204,283
  • 31
  • 260
  • 272
Sam
  • 37
  • 1
  • 9
  • does `var d = millisBetween / millisecondsPerDay;` it give you correct result – Kumar Nov 26 '16 at 02:53
  • Yes, it gives the correct result. I actually managed to sort the 24 hour issue out by adding an equal sign: `if(h >= 0 )` instead of just `if(h > 0 )` **BUT** i still have the base price issue. i.e. if someone selects less than 24 hours difference, the price goes down to £15. Any way of maintaining a **minimum** price of £25? – Sam Nov 26 '16 at 02:56

3 Answers3

1

I advise you to use momentJS. Is pretty easy to use.

var a = moment([2007, 0, 29]);
var b = moment([2007, 0, 28]);
a.diff(b, 'days') // 1

Or just like that:

var a = moment('2016-06-06T21:03:55');//now
var b = moment('2016-05-06T20:03:55');

console.log(a.diff(b, 'minutes')) // 44700
console.log(a.diff(b, 'hours')) // 745
console.log(a.diff(b, 'days')) // 31
console.log(a.diff(b, 'weeks')) // 4

Edit.:

Adding some ideas. You can just simply use the example of @jeff:

dt1 = new Date('2016-01-21 20:00:00');
dt2 = new Date('2016-01-24 09:00:00');
dif = dt2-dt1;
dif = dif / ( 1000 * 60 * 60 * 24 );
days = Math.ceil(dif);
var total = 0;

if (days > 0) {
  total = 25 + ( (days-1) * 10 ) // total = <first day with prequote 25 and then others days are 10 bucks>
}
console.log(total); // total of amout to be paid

Maybe thia can solve your issue.

Community
  • 1
  • 1
Celso Agra
  • 1,389
  • 2
  • 15
  • 37
  • Thanks. I actually managed to sort the 24 hour issue out by adding an equal sign: `if(h >= 0 )` instead of just `if(h > 0 )` **BUT** i still have the base price issue. i.e. if someone selects less than 24 hours difference, the price goes down to £15. Any way of maintaining a **minimum** price of £25? – Sam Nov 26 '16 at 02:58
  • Maybe you can use the example of @jeff (below). – Celso Agra Nov 26 '16 at 03:39
0

Since you are working with whole days you can simplify the process by:

dt1 = new Date('2016-01-21 09:00:00');
dt2 = new Date('2016-01-22 09:00:00');
dif = dt2-dt1;
dif = dif / ( 1000 * 60 * 60 * 24 );
days = Math.ceil(dif);

The days value contains a whole number for the amount of days between the two dates. Do your pricing from this value.

  • I actually managed to sort the 24 hour issue out by adding an equal sign: `if(h >= 0 )` instead of just `if(h > 0 )` **BUT** i still have the base price issue. i.e. if someone selects less than 24 hours difference, the price goes down to £15. Any way of maintaining a **minimum** price of £25? – Sam Nov 26 '16 at 02:50
0

When some one rent room for less than 24 hour. Value of i is become negative. From your expression

prequote = prequote + (taxa * (i-2));

If i is smaller than 2. taxa variable become negative and you have get price less than 25. Please use this below line

i=(i<2)?i=2:i;

From above expression. Your value cannot be smaller than £25. Hope it helps you

Kumar
  • 1,187
  • 18
  • 31