• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 199
  • Last Modified:

A function that will check the distance between 2 dates.

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.

Thanks in advance.
0
CodedK
Asked:
CodedK
  • 5
  • 4
  • 2
1 Solution
 
MMDeveloperCommented:
ok that's easy enough, lemme write up an algorithm
0
 
CodedKAuthor Commented:
Thanks :)
0
 
MatthiasVanceCommented:
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
	alert(delta);
	
</script>

Open in new window

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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

Kind regards,

Matthias Vance
0
 
MMDeveloperCommented:
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">
<head>
<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;
	
	alert(daysDiff);
}
-->
</script>
</head>
<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>

Open in new window

0
 
CodedKAuthor Commented:
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() {
        alert("something....");
        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;
       
        alert("something....2");

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

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

        alert(daysDiff);
        alert(depart);
        alert(arrival);
        alert("something....3");
}
~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-

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
 
MMDeveloperCommented:
not at all. can you paste your form's HTML that you're using so I can test/debug locally?
0
 
CodedKAuthor Commented:
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>

Open in new window

0
 
MMDeveloperCommented:
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
 
MMDeveloperCommented:
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">
<head>
<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;
 
        alert(daysDiff);
}
-->
</script>
</head>
<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>

Open in new window

0
 
CodedKAuthor Commented:
Thank you very much. :)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 5
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now