Click here to Skip to main content
15,879,095 members
Articles / Web Development / HTML
Article

A JavaScript Weekly Calendar

Rate me:
Please Sign up or sign in to vote.
4.76/5 (30 votes)
14 Feb 2006CPOL1 min read 371.4K   2.8K   67   88
This calendar can be used as a date picker or to select a weekly day range in the selected month.

Sample Image - js_weekly_calendar_IE.jpg

Introduction

The calendar generated by this script can be used as a date picker or to select a weekly day range in the selected month. This script has been tested only with Internet Explorer 6.0, Firefox 1.0.7, and Opera 8.51 on a Windows 2003 Server.

Usage

Using this calendar, you can select the start week day, the default is Monday. The calendar's layout is customizable by CSS. To use it, you must insert the following HTML code in the head section of your page:

HTML
// insert this code in the head section
// use this css to modify the calendar layout
<LINK REL=StyleSheet HREF="calendar.css" TYPE="text/css">
// include the calendar javascript
<SCRIPT LANGUAGE="JavaScript" 
         SRC="weeklycalendar.js"></SCRIPT>
<script language="javascript">
    // call the function to build the calendar
    // function's param specify the first day of week 
    // 0=Sunday, 1 = Monday, ..., 6=Saturday
    buildWeeklyCalendar(1);
</script>

If the first day of the week is Monday, the column week contains the week number, otherwise it shows the "<" symbol.

Sample Image - js_weekly_calendar_firefox.jpg

The calendar is dynamically added to the HTML page and it is contained in a div element. To display the calendar, it is necessary to call the w_displayCalendar function. This function accepts two input parameters; they must be the existing element IDs used by the script to return values.

JavaScript
//
// display calendar
//
function w_displayCalendar(linkedId1, linkedId2) 
{
    w_linkedInputText_1 = linkedId1;
    w_linkedInputText_2 = linkedId2;
    
    w_renderCalendar(0);
    if(navigator.userAgent.indexOf("MSIE") != -1) 
    {
        weeklyCalendar.style.left=
          window.event.x+document.body.scrollLeft;
        weeklyCalendar.style.top=
          window.event.y+document.body.scrollTop;
    } 
    else if ((navigator.appName.indexOf("Netscape") != -1) || 
             (navigator.appName.indexOf("Opera") != -1))
    {
        document.getElementById('weeklyCalendar').style.left=gx + 5;
        document.getElementById('weeklyCalendar').style.top=gy + 5;
    }
    document.getElementById('weeklyCalendar').style.visibility = "visible";
}

Using the calendar as a date picker, it automatically fills an input text element passing its ID as a parameter of the w_displayCalendar function.

HTML
<tr>
   <td style="height: 78px">Date picker</td><td style="height: 78px">
      <!--  this is the datepicker input text -->    
      <input type="text"  name="DatePicker" id="DatePicker" size="35" maxlength="80">
      <!--  attach the w_displayCalendar function to the onClick event -->
      <input type="button" value="..." 
         onClick="w_displayCalendar('DatePicker',null);">
   </td>
</tr>

Using the calendar as a weekly range selector, clicking on the week number or the < symbol, it automatically fills the two input text.

HTML
<tr>
    <td style="height: 131px">Weekly Range Selector</td>
      <!--  this is start range input text -->    
      <td style="height: 131px">Start day:<input type="text" 
         name="WeeklyDateStart" id="WeeklyDateStart" 
         size="35" maxlength="80"><br />
      <!--  this is end range input text -->
      End day: <input type="text"  name="WeeklyDateEnd" 
         id="WeeklyDateEnd" size="35" maxlength="80">
      <!--  attach the w_displayCalendar function to the onClick event -->
      <input type="button" value="..." 
        onClick="w_displayCalendar('WeeklyDateStart','WeeklyDateEnd');">
   </td>
</tr>

Sample Image - js_weekly_calendar_opera.jpg

New Feature

Now you can use the calendar as a simple date picker using the new function w_displayDatePicker. This function shows the calendar and automatically hides the column week using a new CSS class.

HTML
<tr>
   <td style="height: 78px">Date picker</td>
   <td style="height: 78px">
      <!--  this is the datepicker input text -->
      <input type="text"  name="DatePicker" 
             id="DatePicker" size="35" maxlength="80">
      <!--  attach the w_displayCalendar function to the onClick event -->
      <input type="button" value="..."
         onClick="w_displayDatePicker('DatePicker');">
   </td>
</tr>

Conclusion

The Zip source file contains the weekly calendar script, the CSS and two demo HTML pages.

I hope you enjoy this article.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


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

Comments and Discussions

 
QuestionChrome help Pin
SeanJohn2429-Aug-16 9:03
SeanJohn2429-Aug-16 9:03 
GeneralWeek Dates is not dispaying particularly Pin
sonashish9-May-10 11:11
sonashish9-May-10 11:11 
QuestionHow to get the date with format lke ''dd/MM/yyyy" Pin
krishbnr18-Dec-08 4:15
krishbnr18-Dec-08 4:15 
AnswerRe: How to get the date with format lke ''dd/MM/yyyy" Pin
Massimo Beatini18-Dec-08 20:59
Massimo Beatini18-Dec-08 20:59 
GeneralOctber Pin
cuervomonkey1-Jul-08 3:44
cuervomonkey1-Jul-08 3:44 
GeneralRe: Octber Pin
evarona14-Jul-08 11:15
evarona14-Jul-08 11:15 
Generalno images displayed in calendar! Pin
azizo234-Jun-08 2:01
azizo234-Jun-08 2:01 
Questionfill fields by default? Pin
nalanthi25-Apr-08 9:23
nalanthi25-Apr-08 9:23 
QuestionHow can i get week numbering in place of "&lt;" sign (In Week column) in IE7 Pin
rajkumaryadav25-Mar-08 2:17
rajkumaryadav25-Mar-08 2:17 
Questionhow to disable all the future dates Pin
sriram543119-Mar-08 22:07
sriram543119-Mar-08 22:07 
QuestionHow to disable all the days after today's date Pin
sriram543119-Mar-08 22:05
sriram543119-Mar-08 22:05 
QuestionDisable all the days before today plus 3 days of today? Pin
Member 238401726-Feb-08 7:40
Member 238401726-Feb-08 7:40 
Generaljavascript event calendar Pin
nbohr99a9-Sep-07 4:03
nbohr99a9-Sep-07 4:03 
QuestionHow to disable particular days of a week in a month. Pin
prakash.konakanchi27-Aug-07 23:34
prakash.konakanchi27-Aug-07 23:34 
Questionchange output type and get value in textbox [modified] Pin
williams5510-May-07 17:14
williams5510-May-07 17:14 
AnswerRe: change output type and get value in textbox Pin
Massimo Beatini10-May-07 22:34
Massimo Beatini10-May-07 22:34 
GeneralRe: change output type and get value in textbox Pin
williams5510-May-07 22:55
williams5510-May-07 22:55 
GeneralDoesn't work outside of included downloads Pin
xizwyck28-Feb-07 4:50
xizwyck28-Feb-07 4:50 
QuestionSeparate text field: the month, day and year? Pin
jogijopo21-Feb-07 16:24
jogijopo21-Feb-07 16:24 
QuestionHow to select the whole week regardless of month Pin
yysharon4-Jan-07 9:07
yysharon4-Jan-07 9:07 
AnswerRe: How to select the whole week regardless of month Pin
nalanthi5-Jun-08 6:51
nalanthi5-Jun-08 6:51 
Generalsimple question Pin
huseyin14-Dec-06 4:16
huseyin14-Dec-06 4:16 
QuestionHow to set WeekDate the first day of row ?! exactly that it is one day of the previous month Pin
leopx14-Nov-06 9:37
leopx14-Nov-06 9:37 
AnswerRe: How to set WeekDate the first day of row ?! exactly that it is one day of the previous month Pin
Massimo Beatini14-Nov-06 22:02
Massimo Beatini14-Nov-06 22:02 
GeneralRe: How to set WeekDate the first day of row ?! exactly that it is one day of the previous month Pin
leopx6-Dec-06 9:36
leopx6-Dec-06 9:36 

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.