Click here to Skip to main content
6,306,412 members and growing! (18,858 online)
Email Password   helpLost your password?
Web Development » Client side scripting » General     Intermediate

JavaScript DatePicker

By Nikita D. Sinelnikoff

The DatePicker is a control for use on the client side of ASP.NET applications.
C#, Javascript, Windows, .NET, ASP.NET, IIS, Visual Studio, WebForms, Dev
Posted:13 Oct 2005
Updated:24 Feb 2006
Views:110,170
Bookmarked:64 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
17 votes for this article.
Popularity: 5.33 Rating: 4.33 out of 5

1

2
2 votes, 11.8%
3
4 votes, 23.5%
4
11 votes, 64.7%
5

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):

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:

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

About the Author

Nikita D. Sinelnikoff


Member

Occupation: Web Developer
Location: Russian Federation Russian Federation

Other popular Client side scripting articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 31 (Total in Forum: 31) (Refresh)FirstPrevNext
GeneralTwo Coordinated Dates Pinmemberkrishna1120:55 22 Apr '08  
GeneralEncapsulate this Class inside a Custom Control is done PinmemberStephan Pilz4:54 22 Sep '06  
Questionabout date control Pinmemberdisak20:14 22 Sep '06  
AnswerRe: about date control PinmemberStephan Pilz21:15 24 Sep '06  
GeneralRe: about date control Pinmemberdisak22:32 24 Sep '06  
GeneralRe: about date control PinmemberStephan Pilz0:03 25 Sep '06  
GeneralRe: Encapsulate this Class inside a Custom Control is done Pinmembershrirampophali22:19 29 Oct '07  
GeneralRe: Encapsulate this Class inside a Custom Control is done Pinmembershrirampophali22:20 29 Oct '07  
Questionabout javascript Pinmemberdisak2:14 22 Sep '06  
GeneralSomebody is ripping you off PinmemberWorkDoggy4:16 29 Aug '06  
AnswerRe: Somebody is ripping you off PinmemberStephan Pilz4:59 7 Sep '06  
GeneralRe: Somebody is ripping you off PinmemberGuyon A. Roche11:41 7 Sep '06  
AnswerRe: Somebody is ripping you off PinmemberWorkDoggy2:10 8 Sep '06  
GeneralRe: Somebody is ripping you off PinmemberGuyon A. Roche11:53 8 Sep '06  
GeneralRe: Somebody is ripping you off PinmemberStephan Pilz4:47 8 Sep '06  
NewsThe coolest date-time picker is here Pinmemberkan_izh23:20 27 Feb '06  
GeneralRe: The coolest date-time picker Pinmemberdisak2:32 22 Sep '06  
GeneralProvide us please with the proper encoding PinmemberJailorSubRuberoid3:36 21 Feb '06  
AnswerRe: Provide us please with the proper encoding PinmemberNikita D. Sinelnikoff2:22 24 Feb '06  
GeneralA Bugfix and some little extensions in Code PinmemberStephan Pilz21:33 20 Dec '05  
GeneralRe: A Bugfix and some little extensions in Code PinmemberGuyon A. Roche12:16 8 Sep '06  
GeneralRe: A Bugfix and some little extensions in Code PinmemberStephan Pilz4:52 22 Sep '06  
QuestionBase class? Pinmemberiammudman8:26 31 Oct '05  
AnswerRe: Base class? PinmemberKamelJabber7:03 13 Mar '06  
Generalsome question! Pinmembergyokusei1:38 16 Oct '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 24 Feb 2006
Editor: Smitha Vijayan
Copyright 2005 by Nikita D. Sinelnikoff
Everything else Copyright © CodeProject, 1999-2009
Web20 | Advertise on the Code Project