Web Developer's Virtual Library: Encyclopedia of Web Design Tutorials, Articles and Discussions
 Discussion Forums
 HTML, XML, JavaScript...
 Software Reviews
 Editors,Others...
 Top100
 JavaScript Tutorials, ...
 Tutorials
 ASP, CSS, Databases...
 Discussion List
 FAQ, Roundup, Configure ...
 Authoring
 HTML, JavaScript, CSS...
 Design
 Layout, Navigation,...
 Graphics
 Tools, Colors, Images...
 Software
 Browsers, Editors, XML...
 Internet
 Domains, E-Commerce, ...
 WDVL Resources
  Intermdiate, Tutorials,...
 WDVL
 Discussion Lists, Top 100,...
 Technology Jobs


WDVL Newsletter

Active Server Pages
JSP/Java Servlets
Microsoft SQL Server
Daily Backup
Dedicated Servers
Streaming Audio/Video
24-hour Support    

jobs.webdeveloper.com

Hiermenus


e-commerce
Partner With Us
Laptops
Free Business Cards
Calling Cards
Promotional Pens
Dental Insurance
Computer Deals
Logo Design
Baby Photo Contest
Boat Donations
Corporate Gifts
Web Hosting Directory
Best Price
Desktop Computers
Phone Cards

Developer Channel
FlashKit.com
JavaScript.com
JavaScriptSource
Developer Jobs
ScriptSearch
StreamingMediaWorld
Web Developer's Journal
Web Developer's Virtual Library
WebDeveloper.com
Webreference
Web Hosts
XMLfiles.com

internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers


Just because Web sites are easy to build these days, that doesn't mean it's easy to build a quality Web site that meets your business objectives.

Before developing your next Web site, or redesigning an existing site, download this Internet.com eBook to guide you through the process and plan your project, whether you're developing a site in-house or outsourcing the project.
Register now for your free Internet.com membership to download your complimentary eBook. Membership will also give you access to:

eBook library         Whitepapers         Webcasts
Newsletters         WinDrivers
Top 10 Articles
  1. Web Developer's Virtual Library: Encyclopedia of Web Design Tutorials, Articles and Discussions
  2. JavaScript Tutorial for Programmers
  3. Design
  4. JavaScript Tutorial for Programmers - Objects
  5. JavaScript Tutorial for Programmers - JavaScript Grammar
  6. JavaScript Tutorial for Programmers - Versions of JavaScript
  7. Cascading Style Sheets
  8. JavaScript Tutorial for Programmers - Embedding JavaScript
  9. JavaScript Tutorial for Programmers - Functions
  10. Authoring JavaScript
Domain Name Lookup
Search to find the availability of a domain name. Just enter the complete domain name with extension (.com, .net, .edu)

Try It Out: Using the Select Element for Date Difference Calculations - Page 21

October 5, 2001

Let's take a look at an example that uses the onchange event and makes good use of the select element in its drop-down list form. Its purpose is to calculate the difference, in days, between two dates as set by the user via drop-down list boxes.

<HTML>

<HEAD>

<SCRIPT LANGUAGE=JavaScript>
function writeOptions(startNumber, endNumber)
{
  var optionCounter;
  for (optionCounter = startNumber; optionCounter <=
   endNumber; optionCounter++)
  {
    document.write('<OPTION value=' + optionCounter + '>' +
     optionCounter);
  }
}
function writeMonthOptions()
{
  var theMonth;
  var monthCounter;
  var theDate = new Date();
  for (monthCounter = 0; monthCounter < 12; monthCounter++)
  {
    theDate.setMonth(monthCounter);
    theMonth = theDate.toString();
    theMonth = theMonth.substr(4,3);
    document.write('<OPTION value=' + theMonth + '>'
     + theMonth);
  }
}
function recalcDateDiff()
{
  var myForm = document.form1;
  var firstDay = myForm.firstDay.options[myForm.firstDay.
  selectedIndex].value;
  var secondDay =
    myForm.secondDay.options[myForm.secondDay.selectedIndex].value;
  var firstMonth =
    myForm.firstMonth.options[myForm.firstMonth.selectedIndex].value;
  var secondMonth =
    myForm.secondMonth.options[myForm.secondMonth.selectedIndex].value;
  var firstYear =
    myForm.firstYear.options[myForm.firstYear.selectedIndex].value;
  var secondYear =
    myForm.secondYear.options[myForm.secondYear.selectedIndex].value;
  var firstDate = new Date(firstDay + " " + firstMonth + " " + firstYear);
  var secondDate = new Date(secondDay + " " + secondMonth + " "
   + secondYear);
  var daysDiff = (secondDate.valueOf() - firstDate.valueOf());
  daysDiff = Math.floor(Math.abs((((daysDiff  / 1000) / 60) / 60) / 24));
  myForm.txtDays.value = daysDiff;
  return true;
}
function window_onload()
{
  var theForm = document.form1;
  var nowDate = new Date();
  theForm.firstDay.options[nowDate.getDate() - 1].selected = true;
  theForm.secondDay.options[nowDate.getDate() - 1].selected = true;
  theForm.firstMonth.options[nowDate.getMonth()].selected = true;
  theForm.secondMonth.options[nowDate.getMonth()].selected = true;
  theForm.firstYear.options[nowDate.getFullYear()-
   1970].selected = true;
  theForm.secondYear.options[nowDate.getFullYear() -
   1970].selected = true;
}
</SCRIPT>

</HEAD>

<BODY LANGUAGE=JavaScript onload="return window_onload()">

<FORM NAME=form1>
<P>
First Date<BR>
<SELECT NAME=firstDay SIZE=1 onchange="return recalcDateDiff()">
<SCRIPT LANGUAGE=JavaScript>
  writeOptions(1,31);
</SCRIPT>
</SELECT>
<SELECT NAME=firstMonth SIZE=1 onchange="return recalcDateDiff()">
<SCRIPT LANGUAGE=JavaScript>
  writeMonthOptions();
</SCRIPT>
</SELECT>
<SELECT NAME=firstYear SIZE=1 onchange="return recalcDateDiff()">
<SCRIPT LANGUAGE=JavaScript>
  writeOptions(1970,2010);
</SCRIPT>
</SELECT>
</P>
<P>
Second Date<BR>
<SELECT NAME=secondDay SIZE=1 onchange="return recalcDateDiff()">
<SCRIPT LANGUAGE=JavaScript>
  writeOptions(1,31);
</SCRIPT>
</SELECT>
<SELECT NAME=secondMonth SIZE=1 onchange="return recalcDateDiff()">
<SCRIPT LANGUAGE=JavaScript>
  writeMonthOptions();
</SCRIPT>
</SELECT>
<SELECT NAME=secondYear SIZE=1 onchange="return recalcDateDiff()">
<SCRIPT LANGUAGE=JavaScript>
  writeOptions(1970,2010);
</SCRIPT>
</SELECT>
</P>
Total difference in days
<INPUT TYPE="text" NAME=txtDays VALUE=0>
<BR>
</FORM>

</BODY>
</HTML>

[The colored lines above are one line. They have been split for formatting purposes.]

Call the example ch6_examp9.htm and load it into your web browser. You should see the form below, but with both date boxes set to the current date.

If you change any of the select boxes, then the difference between the days will be recalculated and shown in the text box.

Adding New Options with Internet Explorer - Page 20
Beginning JavaScript
How It Works - Page 22


Up to => Home / Authoring / JavaScript / Begin




Jupiter Online Media: internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and Jupiter Online Media

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Web Hosting | Newsletters | Tech Jobs | Shopping | E-mail Offers