JavaScript
  Home arrow JavaScript arrow Page 2 - Completing a Simple Date Picker with JavaS...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

Completing a Simple Date Picker with JavaScript and CSS
By: Dan Wellman
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 2 stars2 stars2 stars2 stars2 stars / 8
    2007-03-14

    Table of Contents:
  • Completing a Simple Date Picker with JavaScript and CSS
  • Focusing on Visitor Selections
  • Adding the datestring
  • Finishing up

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Completing a Simple Date Picker with JavaScript and CSS - Focusing on Visitor Selections


    (Page 2 of 4 )

    Now we need to focus on getting the visitor's selections into the text field in the form. We should only do this once an entire date has been selected, so since we already know that the day part of the date will be selected last, we can use the existing getDay() function for this purpose as well. The first thing we should do however, is make the month variable appear as a number instead of letters. We can easily change the selected month into its numerical equivalent using an array of the months and a little For loop to good effect:

    var months = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
    for (x = 0; x < months.length; x++) {
     
    if (months[x] == month) {
       
    month = x + 1;
     
    }
    }

    We first hard code the array values, then set a for loop to iterate through each month in the array. On each cycle, the code will execute the if statement that looks for a match between the current array item and the value of our month variable. When the match is found, the script changes the value of our variable to the current value of x. We have to add 1 to the number held in the x variable because the array, and therefore x, start at zero.

    To tidy the month up further, we can add some code that will make the month two digits for any month. Just below the line of code that reads month = x + 1 add:

    if (month<= 9) {
     
    month = "0" + month.toString();
    }

    We could probably come up with a part of the function that changes a day that is less than 9 to a two digit string as well, but the easiest thing to do is to go back to the HTML file and add a zero to the front of the first nine day elements. Next, we can concatenate our variables to create one master string:

    var datestring = day + "/" + month + "/" + year;

    More JavaScript Articles
    More By Dan Wellman


       · Hi everyone,Thanks for perservering, the date-picker described here does have...
     

    JAVASCRIPT ARTICLES

    - Using Click Interceptions with a Database-Dr...
    - Using JavaScript Click Interceptions in an I...
    - Using Click Interceptions with JavaScript
    - QuickSort in Action
    - Quicksort
    - Using Mod_Security to Protect Your Server
    - Detecting and Countering Server Intrusions
    - Securing Your Web Server
    - Building a Secure Web Server
    - Protecting the Server
    - Book Review: Learning the Yahoo! User Interf...
    - Dynamically Generate a Selection List in a R...
    - Intergrate DWR into Your Java Web Application
    - Detect Browser Compatibility with the Reques...
    - Using the EXT JS Date Picker Widget






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT