PDA

View Full Version : Dates problem


MarioPro
12-16-2002, 11:45 AM
Hello,

I've been arround with a problem, since I'm a newbi in JS, trying to get the following to work:

Supposing that I have two dates, or a period from, 2002-12-20 to 2002-12-30.
Now, I have a price of $10,00 USD for the period from 2002-12-20 to 2002-12-25 and a price of $20,00 USD for the period from 2002-12-26 to 2002-12-30.

How may I calculate, automatically the date and price difference just by entering one the start date and end date and getting the other information from my MySQL tabel prices? (The problem is just about computing, not getting data from MySQL). This is, how may I get the following:

// Dates entered in form: Start 2002-12-20 | End 2002-12-30
// Data from MySQL
// $10,00 USD from 2002-12-01 to 2002-12-25
// $20,00 USD from 2002-12-26 to 2002-12-31

2002-12-20 to 2002-12-25 = 5 days x $10,00 USD = Subtotal $50,00
2002-12-26 to 2002-12-30 = 4 days x $20,00 USD = Subtotal $80,00

Total = $130,00 USD

Thanks in advance for any help in this problem

Regards
MarioPro

RadarBob
12-16-2002, 03:34 PM
Off the top of my head, to do the calculations automatically, I'd use the onblur event tied to the end date. When the user moves off the end date field, onblur will trigger a javascript function that does the calculations.

You don't want to do the calc till both dates are entered, so doing this on the start date wouldn't make sense.

. . .
function calcPricing () {
var totalPrice= 0;
// validate start & end date fields
// calculate date difference
// lookup price for the date range
// calculate the price

document.theFormName.thePrice.value = totalPrice;
}

. . .

<input type="text" name="endDate" value="" onblur="calcPricing ();">
. . .
<input type="text" name="thePrice" value="">
. . .

MarioPro
12-16-2002, 04:06 PM
Thanks for your help.

In fact, my biggest problem consists on coding the third step, this is,

// calculate date difference (thinks it is ok
function dateDideference(){
with (document.forms['myform']){
totaldays.value = DiffDays(Date1.value.trim(), Date2.value.trim())
Date1.focus()
}
}

// lookup price for the date range

??????? How may I do this in Javascript


Thanks for you help

RadarBob
12-16-2002, 04:29 PM
WAY too much work on your part, MP!

The Javascript built in date object makes things easy.

Look here to find out more about the date object:
http://developer.netscape.com/docs/manuals/js/core/jsguide15/contents.html

Look here for all the available Date properties and methods. You'll find that what you need is alread done for you. Just use it.
http://developer.netscape.com/docs/manuals/js/core/jsref15/contents.html

jalarie
12-16-2002, 04:37 PM
MarioPro, I think you missed the fact that your visitor is with you overnight on the 25th also. Shouldn't the correct example result be $140,00 USD?

MarioPro
12-16-2002, 05:37 PM
Yes, but on 30 he will be out and the script is intended to count nights, not days and that's why it would be $130,00 USD, not a discount ;) .

MarioPro
12-16-2002, 05:38 PM
RadarBob, thanks for the links, I think that I might handle it myself with the tips there. Thanks

Originally posted by RadarBob
WAY too much work on your part, MP!

The Javascript built in date object makes things easy.

Look here to find out more about the date object:
http://developer.netscape.com/docs/manuals/js/core/jsguide15/contents.html

Look here for all the available Date properties and methods. You'll find that what you need is alread done for you. Just use it.
http://developer.netscape.com/docs/manuals/js/core/jsref15/contents.html

jalarie
12-16-2002, 05:46 PM
Date Amount Balance
20th $10,00 $10,00
21st $10,00 $20,00
22nd $10,00 $30,00
23rd $10,00 $40,00
24th $10,00 $50,00
25th $10,00 $60,00
26th $20,00 $80,00
27th $20,00 $100,00
28th $20,00 $120,00
29th $20,00 $140,00

What am I doing wrong?

MarioPro
12-16-2002, 05:59 PM
Originally posted by jalarie
Date Amount Balance
20th $10,00 $10,00 //------< Here. 20th $0,00
21st $10,00 $20,00
22nd $10,00 $30,00
23rd $10,00 $40,00
24th $10,00 $50,00
25th $10,00 $60,00
26th $20,00 $80,00
27th $20,00 $100,00
28th $20,00 $120,00
29th $20,00 $140,00

What am I doing wrong?

jalarie
12-16-2002, 06:09 PM
Don't you charge for the night of the 20th? Once that night has passed, your customer owes you $10,00.

MarioPro
12-16-2002, 06:15 PM
I'm sorry! It seems that I was really making a discount! :)
You're absolutely right on the $140,00.

jalarie
12-16-2002, 06:27 PM
Assuming that the users enters dates as the day number only, you could use the following code. If s/he enters a full date, you have to work a bit harder:

&nbsp;F=document.forms[0].Date_from.value;
&nbsp;T=document.forms[0].Date_to.value;
&nbsp;if&nbsp;(T&nbsp;<=&nbsp;F)&nbsp;{
&nbsp;&nbsp;alert('Date_to&nbsp;is&nbsp;less&nbsp;than&nbsp;or&nbsp;equal&nbsp;to&nbsp;Date_from');
&nbsp;&nbsp;document.forms[0].Total.value=0;
&nbsp;}
&nbsp;if&nbsp;(T&nbsp;<=&nbsp;26)&nbsp;{
&nbsp;&nbsp;document.forms[0].Total.value=(T-F)*10;
&nbsp;&nbsp;return;
&nbsp;}
&nbsp;if&nbsp;(F&nbsp;>=&nbsp;26)&nbsp;{
&nbsp;&nbsp;document.forms[0].Total.value=(T-F)*20;
&nbsp;&nbsp;return;
&nbsp;}
&nbsp;document.forms[0].Total.value=(26-F)*10+(T-26)*20;
&nbsp;return;

MarioPro
12-16-2002, 06:34 PM
It seems that the code you posted is what I needed as a starting point and a great help. I'm not confortable with Javascript and I'm trying to get some improvements, so, I will work on that code, make some calculations and I'll post here the final result.

Thanks again for your valuable help. :) :thumbsup:

jalarie
12-16-2002, 06:39 PM
I don't know your ultimate goal here, so this might be totally off-the-wall, but.... Take a look at the 'needed by' field at the bottom of the 'Samples of my work > Order form' page at my site for a pop-up date-picker that works on nearly every browser. It might be an interesting starting point for something usefull. Many similar things available on the 'net don't work in some browsers. The site address is:

&nbsp;http://spruce.flint.umich.edu/~jalarie/

MarioPro
12-16-2002, 07:00 PM
Thanks. Your page on Days between dates http://spruce.flint.umich.edu/~jalarie/jaa_kch.htm has a good script for the date calculations with regard to workdays.

With the regard to the order form, I made my own, let me say, bit more complex, where it calculates each rate according to the number of days between given dates, but...
suddenly I noticed that I might be working with different rates per night and that I would have to recode it in order to check that the dates from check-in to check-out don't fall in a period where I have to charge two different rates. But if they follow in those periods I have to calculate them.

Since I'm workin with year quaters, 2002-01-01 > 2002-03-31, I have to check in which querter(s) the booking will fall and then calculate the days and rates for the final invoice. The code you posted seems to handle this problem.