Solved

# A function that will check the distance between 2 dates.

Posted on 2008-10-08
Medium Priority
194 Views
Hi.

I have 6 select boxes.
The user select the arrival date using the first 3 select boxes. (Date, Month, Year)
and then he selects the departure time ... another 3 boxes. (Date , Month, Year).

the name of the boxes are :
select name 1 : afiksi_date
select name 2 : afiksi_month
select name 3 : afiksi_year
select name 4 : anaxorisi_date
select name 5 : anaxorisi_month
select name 6 : anaxorisi_year

I want upon the selection of the departure year (select 6) my function to check the days between
arrival and departure :
example :  17/01/2008  -> 19/01/2008   (2 days)

If its easier i would like to check only if the distance is negative or positive.

0
Question by:CodedK
[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
• 5
• 4
• 2

LVL 15

Expert Comment

ID: 22671243
ok that's easy enough, lemme write up an algorithm
0

LVL 16

Author Comment

ID: 22671257
Thanks :)
0

LVL 8

Expert Comment

ID: 22671318
I provided some sample code for you. Hope it helps you out!

Kind regards,

Matthias Vance
``````<script language="JavaScript">
// Initialize dates
var date1 = new Date(2008, 11, 12);
var date2 = new Date(2008, 11, 25);

// Calculate difference
var day = 1000*60*60*24;
var delta = (date2.getTime() - date1.getTime()) / day;

// Debug

</script>
``````
0

LVL 8

Expert Comment

ID: 22671333
I'm always minutes later than the first reply. I don't do this on purpose.

Kind regards,

Matthias Vance
0

LVL 15

Expert Comment

ID: 22671568
this will alert the difference in days (departure - arrival);
``````<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript">
<!--
function checkDates() {
arm = document.getElementById("ardm").value;
ard = document.getElementById("ardd").value;
ary = document.getElementById("ardy").value;

dpm = document.getElementById("dpdm").value;
dpd = document.getElementById("dpdd").value;
dpy = document.getElementById("dpdy").value;

arrival = Date.UTC(ary, parseInt(arm) - 1, ard);
depart = Date.UTC(dpy, parseInt(dpm) - 1, dpd);

daysDiff = ((depart - arrival) / 1000) / 86400;

}
-->
</script>
<body>
<label>Arrival: </label>
<label>
<select name="ardd" id="ardd">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
<option value="31">31</option>
</select>
</label>
/
<select name="ardm" id="ardm">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
</select>
/
<label>
<select name="ardy" id="ardy">
<option value="2007">2007</option>
<option value="2008">2008</option>
<option value="2009">2009</option>
<option value="2010">2010</option>
</select>
</label>
<p>Departure:
<label>
<select name="dpdd" id="dpdd">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
<option value="31">31</option>
</select>
</label>
/
<select name="dpdm" id="dpdm">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
</select>
/
<label>
<select name="dpdy" id="dpdy" onchange="checkDates();">
<option value="2007">2007</option>
<option value="2008">2008</option>
<option value="2009">2009</option>
<option value="2010">2010</option>
</select>
</label>
</p>
</body>
</html>
``````
0

LVL 16

Author Comment

ID: 22672093
Thank you both for your help.

MMDeveloper i'm trying to adapt your code but it doesnt work for me ...
Here is what i do :
~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
function checkDates() {
arm = form.afiksi_month.value;
ard = form.afiksi_date.value;
ary = form.afiksi_year.value;

dpm = form.anaxorisi_month.value;
dpd = form.anaxorisi_date.value;
dpy = form.anaxorisi_year.value;

arrival = Date.UTC(ary, parseInt(arm) - 1, ard);
depart = Date.UTC(dpy, parseInt(dpm) - 1, dpd);

daysDiff = ((depart - arrival) / 1000) / 86400;

}
~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-

I changed [document.getElementById] with [form.X.value;]
which i use some lines below and works ... but still i cant make it to alert beyond the first alert !

Are there any vars ... something like create date i should do before i use it ?
0

LVL 15

Expert Comment

ID: 22672113
not at all. can you paste your form's HTML that you're using so I can test/debug locally?
0

LVL 16

Author Comment

ID: 22672193
Ignore PHP...

As you can see i use another function i made (which works nice) ...
name="afiksi_year"   onchange="check_Feb();"

And i use the form.X.value ... So i know i dont do something wrong with the names...
Of course i also tried document getElementById but it also didnt work :/
``````          <table>
<tr>
<td width="45%">(Check in):</td>
<td align="left">

<select name="afiksi_date">
<option value="-1">-</option>
<?php
for (\$i1=1; \$i1<=31; \$i1++)
{
echo "<option value='".\$i1."'";
if (\$_POST['afiksi_date']==\$i1) echo "selected";
echo ">".\$i1."</option>";
}
?></select>

<select name="afiksi_month">
<option value="-1">-</option>
<?
for (\$i2=1; \$i2<=12; \$i2++)
{
echo "<option value='".\$i2."'";
if (\$_POST['afiksi_month']==\$i2) echo "selected";
echo ">".\$i2."</option>";
}
?></select>

<select name="afiksi_year" onchange="check_Feb();">
<option value="-1">-</option>
<?php
echo ("<option value='".date(Y)."'");
if (\$_POST['afiksi_year']==date(Y)) { echo(" selected>".date(Y)."</option>");}
else { echo(">".date(Y)."</option>");}
echo ("<option value='".(date(Y)+1)."'");
if (\$_POST['afiksi_year']==(date(Y)+1)) {echo (" selected>".(date(Y)+1)."</option>");}
else {echo (">".(date(Y)+1)."</option>");}
?>
</select>
</td>
</tr>
<tr>
<td width="45%" align="right">(Check out):</td>
<td align="left">
<select name="anaxorisi_date">
<option value="-1">-</option>
<?
for (\$i1=1; \$i1<=31; \$i1++)
{
echo "<option value='".\$i1."'";
if (\$_POST['anaxorisi_date']==\$i1) echo "selected";
echo ">".\$i1."</option>";
}
?></select>

<select name="anaxorisi_month">
<option value="-1">-</option>
<?
for (\$i2=1; \$i2<=12; \$i2++)
{
echo "<option value='".\$i2."'";
if (\$_POST['anaxorisi_month']==\$i2) echo "selected";
echo ">".\$i2."</option>";
}
?></select>

<select name="anaxorisi_year" onchange="checkDates();">
<option value="-1">-</option>
<?php

echo ("<option value='".date(Y)."'");
if (\$_POST['anaxorisi_year']==date(Y)) { echo(" selected>".date(Y)."</option>");}
else { echo(">".date(Y)."</option>");}

echo ("<option value='".(date(Y)+1)."'");
if (\$_POST['anaxorisi_year']==(date(Y)+1)) {echo (" selected>".(date(Y)+1)."</option>");}
else {echo (">".(date(Y)+1)."</option>");}

?>
</select>
</td>
</tr>
<table>
``````
0

LVL 15

Expert Comment

ID: 22672539
ok I think I see the problem...

it's in the form.afiksi_month.value type references. Typically when you use that type of reference, you need to name the form like this

<form name="whatever">

<input name="something">

</form>

and you'd reference it like: document.whatever.something.value

so it's simply an issue with how you are referencing your fields. The specific error I get from your modified javascript is "form is not defined".
0

LVL 15

Accepted Solution

MMDeveloper earned 2000 total points
ID: 22672576
here is a working version of your form using your type of references. I have removed the PHP and replaced it with it's HTML equivalent (for testing purposes).
``````<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript">
<!--
function checkDates() {
arm = document.form.afiksi_month.value;
ard = document.form.afiksi_date.value;
ary = document.form.afiksi_year.value;

dpm = document.form.anaxorisi_month.value;
dpd = document.form.anaxorisi_date.value;
dpy = document.form.anaxorisi_year.value;

arrival = Date.UTC(ary, parseInt(arm) - 1, ard);
depart = Date.UTC(dpy, parseInt(dpm) - 1, dpd);

daysDiff = ((depart - arrival) / 1000) / 86400;

}
-->
</script>
<body>
<form name="form">
<table>
<tr>
<td width="45%">(Check in):</td>
<td align="left"><select name="afiksi_date">
<option value="-1">-</option>
<option value='1'>1</option>
<option value='2'>2</option>
<option value='3'>3</option>
<option value='4'>4</option>
<option value='5'>5</option>
<option value='6'>6</option>
<option value='7'>7</option>
<option value='8'>8</option>
<option value='9'>9</option>
<option value='10'>10</option>
<option value='11'>11</option>
<option value='12'>12</option>
<option value='13'>13</option>
<option value='14'>14</option>
<option value='15'>15</option>
<option value='16'>16</option>
<option value='17'>17</option>
<option value='18'>18</option>
<option value='19'>19</option>
<option value='20'>20</option>
<option value='21'>21</option>
<option value='22'>22</option>
<option value='23'>23</option>
<option value='24'>24</option>
<option value='25'>25</option>
<option value='26'>26</option>
<option value='27'>27</option>
<option value='28'>28</option>
<option value='29'>29</option>
<option value='30'>30</option>
<option value='31'>31</option>
</select>
<select name="afiksi_month">
<option value="-1">-</option>
<option value='1'>1</option>
<option value='2'>2</option>
<option value='3'>3</option>
<option value='4'>4</option>
<option value='5'>5</option>
<option value='6'>6</option>
<option value='7'>7</option>
<option value='8'>8</option>
<option value='9'>9</option>
<option value='10'>10</option>
<option value='11'>11</option>
<option value='12'>12</option>
</select>
<select name="afiksi_year">
<option value="-1">-</option>
<option value='2008'>2008</option>
<option value='2009'>2009</option>
</select>
</td>
</tr>
<tr>
<td width="45%" align="right">(Check out):</td>
<td align="left"><select name="anaxorisi_date">
<option value="-1">-</option>
<option value='1'>1</option>
<option value='2'>2</option>
<option value='3'>3</option>
<option value='4'>4</option>
<option value='5'>5</option>
<option value='6'>6</option>
<option value='7'>7</option>
<option value='8'>8</option>
<option value='9'>9</option>
<option value='10'>10</option>
<option value='11'>11</option>
<option value='12'>12</option>
<option value='13'>13</option>
<option value='14'>14</option>
<option value='15'>15</option>
<option value='16'>16</option>
<option value='17'>17</option>
<option value='18'>18</option>
<option value='19'>19</option>
<option value='20'>20</option>
<option value='21'>21</option>
<option value='22'>22</option>
<option value='23'>23</option>
<option value='24'>24</option>
<option value='25'>25</option>
<option value='26'>26</option>
<option value='27'>27</option>
<option value='28'>28</option>
<option value='29'>29</option>
<option value='30'>30</option>
<option value='31'>31</option>
</select>
<select name="anaxorisi_month">
<option value="-1">-</option>
<option value='1'>1</option>
<option value='2'>2</option>
<option value='3'>3</option>
<option value='4'>4</option>
<option value='5'>5</option>
<option value='6'>6</option>
<option value='7'>7</option>
<option value='8'>8</option>
<option value='9'>9</option>
<option value='10'>10</option>
<option value='11'>11</option>
<option value='12'>12</option>
</select>
<select name="anaxorisi_year" onchange="checkDates();">
<option value="-1">-</option>
<option value='2008'>2008</option>
<option value='2009'>2009</option>
</select>
</td>
</tr>
</table>
</form>
</body>
</html>
``````
0

LVL 16

Author Closing Comment

ID: 31504335
Thank you very much. :)
0

## Featured Post

Question has a verified solution.

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

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the â€¦
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult toâ€¦
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQueryâ€¦
###### Suggested Courses
Course of the Month14 days, 22 hours left to enroll