Click here to Skip to main content
6,295,667 members and growing! (15,598 online)
Email Password   helpLost your password?
Web Development » Client side scripting » General     Intermediate License: The Code Project Open License (CPOL)

A JavaScript Weekly Calendar

By Massimo Beatini

This calendar can be used as a date picker or to select a weekly day range in the selected month.
Javascript, HTML, Windows, Visual Studio, Dev
Posted:30 Jan 2006
Updated:14 Feb 2006
Views:143,659
Bookmarked:62 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
28 votes for this article.
Popularity: 6.83 Rating: 4.72 out of 5

1

2
2 votes, 7.1%
3
5 votes, 17.9%
4
21 votes, 75.0%
5

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:

// 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.

//

// 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.

<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.

<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.

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

About the Author

Massimo Beatini


Member

Occupation: Web Developer
Location: Italy Italy

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 82 (Total in Forum: 82) (Refresh)FirstPrevNext
GeneralHow to get the date with format lke ''dd/MM/yyyy" Pinmemberkrishbnr5:15 18 Dec '08  
GeneralRe: How to get the date with format lke ''dd/MM/yyyy" PinmemberMassimo Beatini21:59 18 Dec '08  
GeneralOctber Pinmembercuervomonkey4:44 1 Jul '08  
GeneralRe: Octber Pinmemberevarona12:15 14 Jul '08  
Generalno images displayed in calendar! Pinmemberazizo233:01 4 Jun '08  
Generalfill fields by default? Pinmembernalanthi10:23 25 Apr '08  
QuestionHow can i get week numbering in place of "<" sign (In Week column) in IE7 Pinmemberrajkumaryadav3:17 25 Mar '08  
Questionhow to disable all the future dates Pinmembersriram543123:07 19 Mar '08  
GeneralHow to disable all the days after today's date Pinmembersriram543123:05 19 Mar '08  
GeneralDisable all the days before today plus 3 days of today? PinmemberMember 23840178:40 26 Feb '08  
Generaljavascript event calendar Pinmembernbohr99a5:03 9 Sep '07  
QuestionHow to disable particular days of a week in a month. Pinmemberprakash.konakanchi0:34 28 Aug '07  
Questionchange output type and get value in textbox [modified] Pinmemberwilliams5518:14 10 May '07  
AnswerRe: change output type and get value in textbox PinmemberMassimo Beatini23:34 10 May '07  
GeneralRe: change output type and get value in textbox Pinmemberwilliams5523:55 10 May '07  
GeneralDoesn't work outside of included downloads Pinmemberxizwyck5:50 28 Feb '07  
QuestionSeparate text field: the month, day and year? Pinmemberjogijopo17:24 21 Feb '07  
GeneralHow to select the whole week regardless of month Pinmemberyysharon10:07 4 Jan '07  
GeneralRe: How to select the whole week regardless of month Pinmembernalanthi7:51 5 Jun '08  
Generalsimple question Pinmemberhuseyin5:16 14 Dec '06  
GeneralHow to set WeekDate the first day of row ?! exactly that it is one day of the previous month Pinmemberleopx10:37 14 Nov '06  
AnswerRe: How to set WeekDate the first day of row ?! exactly that it is one day of the previous month PinmemberMassimo Beatini23:02 14 Nov '06  
GeneralRe: How to set WeekDate the first day of row ?! exactly that it is one day of the previous month Pinmemberleopx10:36 6 Dec '06  
GeneralRe: How to set WeekDate the first day of row ?! exactly that it is one day of the previous month Pinmemberlokesh718623:54 24 Mar '09  
GeneralWay to pass date to calendar ? Pinmemberbluter5:25 13 Oct '06  

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

PermaLink | Privacy | Terms of Use
Last Updated: 14 Feb 2006
Editor: Smitha Vijayan
Copyright 2006 by Massimo Beatini
Everything else Copyright © CodeProject, 1999-2009
Web11 | Advertise on the Code Project