Click here to Skip to main content
Click here to Skip to main content

A JavaScript Weekly Calendar

By , 14 Feb 2006
 

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
Web Developer
Italy Italy
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralRe: How to set WeekDate the first day of row ?! exactly that it is one day of the previous monthmembercpsaran15 Sep '09 - 4:19 
GeneralRe: How to set WeekDate the first day of row ?! exactly that it is one day of the previous month [modified]memberMassimo Beatini15 Sep '09 - 21:38 
GeneralRe: How to set WeekDate the first day of row ?! exactly that it is one day of the previous month [modified]memberDaFleaster19 Mar '12 - 8:20 
GeneralRe: How to set WeekDate the first day of row ?! exactly that it is one day of the previous monthmemberMassimo Beatini15 Sep '09 - 21:51 
QuestionWay to pass date to calendar ?memberbluter13 Oct '06 - 4:25 
AnswerRe: Way to pass date to calendar ?memberMassimo Beatini14 Nov '06 - 23:10 
QuestionDisable all the days before today!memberwebmangr9 Oct '06 - 3:27 
AnswerRe: Disable all the days before today!memberMassimo Beatini9 Oct '06 - 4:27 
GeneralRe: Disable all the days before today!memberwebmangr9 Oct '06 - 21:31 
GeneralRe: Disable all the days before today!memberwebmangr9 Oct '06 - 21:38 
GeneralRe: Disable all the days before today!memberwebmangr9 Oct '06 - 21:44 
GeneralRe: Disable all the days before today!memberMassimo Beatini9 Oct '06 - 23:08 
GeneralRe: Disable all the days before today!memberMassimo Beatini9 Oct '06 - 22:38 
AnswerRe: Disable all the days before today!memberclausont24 May '08 - 6:19 
Generalsmart navigation problemmembershesho64211 Sep '06 - 2:07 
GeneralInformations about calendar scriptmemberEnrico Amisano7 Sep '06 - 6:51 
AnswerRe: Informations about calendar scriptmemberMassimo Beatini8 Sep '06 - 4:58 
QuestionRe: Informations about calendar scriptmemberwebmangr9 Oct '06 - 2:41 
GeneralPositioning and Changing the beginning of the Weekmembervanvank7 Sep '06 - 3:37 
AnswerRe: Positioning and Changing the beginning of the WeekmemberMassimo Beatini8 Sep '06 - 5:21 
Questionhow to display the day of the week and week number for given datememberjanes7613 Jul '06 - 0:06 
AnswerRe: how to display the day of the week and week number for given datememberMassimo Beatini13 Jul '06 - 5:56 
QuestionHide onclick outmemberwebmangr12 Jul '06 - 23:43 
AnswerRe: Hide onclick outmemberMassimo Beatini14 Jul '06 - 4:54 
Questionhow to display the week number if the default start day for week is sundaymemberhappy_she2 Apr '06 - 21:16 

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 14 Feb 2006
Article Copyright 2006 by Massimo Beatini
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid