Click here to Skip to main content
15,895,142 members
Articles / Web Development / HTML
Article

JavaScript DatePicker

Rate me:
Please Sign up or sign in to vote.
4.30/5 (21 votes)
24 Feb 2006 257.4K   3.1K   71   34
The DatePicker is a control for use on the client side of ASP.NET applications.

Sample Image - datepicker.gif

Introduction

Here is the sample code for a DatePicker in JavaScript. It can be used on HTML pages or as an ASP control - like I use it (see demo files).

Here is the code snippet of the JS code (part of the "render" function):

JavaScript
DatePicker.prototype.render = function()
{
     var oT1, oTR1, oTD1, oTH1;
     var oT2, oTR2, oTD2;
     this.oSpan = document.getElementById(this.name);
     this.oSpan.appendChild(oT1 = document.createElement("table"));
     oT1.width = "200";
     oT1.border = 1;
     oTR1 = oT1.insertRow(oT1.rows.length);
     oTD1 = oTR1.insertCell(oTR1.cells.length);
     oTD1.colSpan = 7;
     oTD1.className = 'DatePickerHdr';
     oT2 = document.createElement("table");
     oT2.width = "100%";
     oTD1.appendChild(oT2);
     oT2.border = 0;
     // New row.
     oTR2 = oT2.insertRow(oT2.rows.length);
     // Previous month.
     oTD2 = oTR2.insertCell(oTR2.cells.length);
     oTD2.title = this.texts.prevMonth;
     oTD2.onclick = function() { this.oDatePicker.onPrev(); }
     oTD2.oDatePicker = this;
     oTD2.innerHTML = "<img src="images/datepicker/prev.gif">";
     oTD2.className = 'DatePickerHdrBtn';
     // Month combo.
     oTD2 = oTR2.insertCell(oTR2.cells.length);
     oTD2.title = this.texts.monthTitle;
     this.oMonth = document.createElement("select");
     oTD2.appendChild(this.oMonth);
     this.oMonth.oDatePicker = this;
     this.oMonth.onchange = this.oMonth.onkeyup = 
          function() { this.oDatePicker.onMonth(); }
     for(var i = 0; i < 12; i++)
     {
        this.oMonth.add(new Option(this.texts.months[i], i),undefined);
     }
     this.oMonth.className = 'DatePickerHdrBtn';
     // Year combo.
     oTD2 = oTR2.insertCell(oTR2.cells.length);
     oTD2.title = this.texts.yearTitle;
     this.oYear = document.createElement("select");
     oTD2.appendChild(this.oYear);
     this.oYear.oDatePicker = this;
     this.oYear.onchange = this.oYear.onkeyup = 
          function() { this.oDatePicker.onYear(); }

I did use Russian language and Russian date format because the need of the picker was for a Russian site. But the language and format the picker uses can be changed very simply - just change the array values and the "fill" method:

JavaScript
DatePicker.prototype.fill = function()
{
     // first clear all
     this.clear();
     // place the dates in the calendar
     var nRow = 0;
     var d = new Date(this.dt.getTime());
     var m = d.getMonth();
     for ( d.setDate(1); d.getMonth() == m; 
           d.setTime(d.getTime() + 86400000) ) {
        var nCol = d.getDay();
        if(nCol == 0) nCol = 7;
        nCol = nCol-1;
        this.aCells[nRow][nCol].innerHTML = d.getDate();
        if ( d.getDate() == this.dt.getDate() ) {
           this.aCells[nRow][nCol].className = 
                             'DatePickerBtnSelect';
        }
        if ( nCol == 6 ) nRow++;
     }
     // set the month combo
     this.oMonth.value = m;
     // set the year text
     this.oYear.value = this.dt.getFullYear();
}

That is all. I do really think the control is very useful.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Russian Federation Russian Federation
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: A Bugfix and some little extensions in Code Pin
Stephan Pilz22-Sep-06 3:52
Stephan Pilz22-Sep-06 3:52 
QuestionBase class? Pin
iammudman31-Oct-05 7:26
iammudman31-Oct-05 7:26 
AnswerRe: Base class? Pin
KamelJabber13-Mar-06 6:03
KamelJabber13-Mar-06 6:03 
Generalsome question! Pin
gyokusei16-Oct-05 0:38
gyokusei16-Oct-05 0:38 
AnswerRe: some question! Pin
Nikita D. Sinelnikoff17-Oct-05 3:18
Nikita D. Sinelnikoff17-Oct-05 3:18 
GeneralRe: some question! Pin
gyokusei18-Oct-05 19:30
gyokusei18-Oct-05 19:30 
GeneralRe: some question! Pin
disak22-Sep-06 1:24
disak22-Sep-06 1:24 
GeneralHTML version Pin
Steven A Bristol13-Oct-05 6:48
Steven A Bristol13-Oct-05 6:48 
For those of you who do not want to have to compile this to check it out, here is the html:
<br />
<script language="javascript" src="DatePicker.js"></script><br />
<br />
<span id="theDatePicker" name="theDatePicker" class="DatePicker"></span><br />
<!-- x:DatePicker --><br />
<input type="text" class="DatePickerValue" name="xvb" id="xvb" style="width: 64px;" value="1/1/2005" title="title"> <br />
<img src="images/buttons/dp.gif" class="LinkedElement" onclick="DatePickerShow(xvb, xvbCh);" title="Âûáðàòü" name="xvbCh" id="xvbCh" title="âûáðàòü"><br />
<!-- /x:DatePicker -->



Over all it is kinda cool. Could hide and show the calendar a little better, and an english version would be great (and easy to do (line 427)).

Nice work.

Steven A Bristol

-- modified at 8:50 Tuesday 18th October, 2005
GeneralRe: HTML version Pin
jsanjosembet.es17-Oct-05 20:45
jsanjosembet.es17-Oct-05 20:45 
GeneralRe: HTML version Pin
Steven A Bristol18-Oct-05 2:50
Steven A Bristol18-Oct-05 2:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.