Solved

A function that will check the distance between 2 dates.

Posted on 2008-10-08
11
192 Views
Last Modified: 2012-05-05
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
Comment
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
  • Learn & ask questions
  • 5
  • 4
  • 2
11 Comments
 
LVL 15

Expert Comment

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

Author Comment

by:CodedK
ID: 22671257
Thanks :)
0
 
LVL 8

Expert Comment

by:MatthiasVance
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
	alert(delta);
	
</script>

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Expert Comment

by:MatthiasVance
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

by:MMDeveloper
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">
<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
 
LVL 16

Author Comment

by:CodedK
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() {
        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
 
LVL 15

Expert Comment

by:MMDeveloper
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

by:CodedK
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>

Open in new window

0
 
LVL 15

Expert Comment

by:MMDeveloper
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

by:
MMDeveloper earned 500 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">
<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
 
LVL 16

Author Closing Comment

by:CodedK
ID: 31504335
Thank you very much. :)
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question