Click here to Skip to main content
11,478,619 members (69,488 online)
Click here to Skip to main content

A JavaScript Weekly Calendar

, 14 Feb 2006 CPOL 284.2K 2.6K 66
Rate this:
Please Sign up or sign in to vote.
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:

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

Share

About the Author

Massimo Beatini
Web Developer
Italy Italy
No Biography provided

Comments and Discussions

 
GeneralWeek Dates is not dispaying particularly Pin
sonashish9-May-10 12:11
membersonashish9-May-10 12:11 
QuestionHow to get the date with format lke ''dd/MM/yyyy" Pin
krishbnr18-Dec-08 5:15
memberkrishbnr18-Dec-08 5:15 
AnswerRe: How to get the date with format lke ''dd/MM/yyyy" Pin
Massimo Beatini18-Dec-08 21:59
memberMassimo Beatini18-Dec-08 21:59 
GeneralOctber Pin
cuervomonkey1-Jul-08 4:44
membercuervomonkey1-Jul-08 4:44 
GeneralRe: Octber Pin
evarona14-Jul-08 12:15
memberevarona14-Jul-08 12:15 
Generalno images displayed in calendar! Pin
azizo234-Jun-08 3:01
memberazizo234-Jun-08 3:01 
Questionfill fields by default? Pin
nalanthi25-Apr-08 10:23
membernalanthi25-Apr-08 10:23 
QuestionHow can i get week numbering in place of "&lt;" sign (In Week column) in IE7 Pin
rajkumaryadav25-Mar-08 3:17
memberrajkumaryadav25-Mar-08 3:17 
Questionhow to disable all the future dates Pin
sriram543119-Mar-08 23:07
membersriram543119-Mar-08 23:07 
QuestionHow to disable all the days after today's date Pin
sriram543119-Mar-08 23:05
membersriram543119-Mar-08 23:05 
QuestionDisable all the days before today plus 3 days of today? Pin
Member 238401726-Feb-08 8:40
memberMember 238401726-Feb-08 8:40 
Generaljavascript event calendar Pin
nbohr99a9-Sep-07 5:03
membernbohr99a9-Sep-07 5:03 
this is a fine example of a date picker, but if you want a javascript event calendar, check out http://www.scriptcalendar.com

javascript calendar
QuestionHow to disable particular days of a week in a month. Pin
prakash.konakanchi28-Aug-07 0:34
memberprakash.konakanchi28-Aug-07 0:34 
Questionchange output type and get value in textbox [modified] Pin
williams5510-May-07 18:14
memberwilliams5510-May-07 18:14 
AnswerRe: change output type and get value in textbox Pin
Massimo Beatini10-May-07 23:34
memberMassimo Beatini10-May-07 23:34 
GeneralRe: change output type and get value in textbox Pin
williams5510-May-07 23:55
memberwilliams5510-May-07 23:55 
GeneralDoesn't work outside of included downloads Pin
xizwyck28-Feb-07 5:50
memberxizwyck28-Feb-07 5:50 
QuestionSeparate text field: the month, day and year? Pin
jogijopo21-Feb-07 17:24
memberjogijopo21-Feb-07 17:24 
QuestionHow to select the whole week regardless of month Pin
yysharon4-Jan-07 10:07
memberyysharon4-Jan-07 10:07 
AnswerRe: How to select the whole week regardless of month Pin
nalanthi5-Jun-08 7:51
membernalanthi5-Jun-08 7:51 
Generalsimple question Pin
huseyin14-Dec-06 5:16
memberhuseyin14-Dec-06 5:16 
QuestionHow to set WeekDate the first day of row ?! exactly that it is one day of the previous month Pin
leopx14-Nov-06 10:37
memberleopx14-Nov-06 10: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 23:02
memberMassimo Beatini14-Nov-06 23: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 10:36
memberleopx6-Dec-06 10:36 
GeneralRe: How to set WeekDate the first day of row ?! exactly that it is one day of the previous month Pin
lokesh718624-Mar-09 23:54
memberlokesh718624-Mar-09 23:54 
GeneralRe: How to set WeekDate the first day of row ?! exactly that it is one day of the previous month Pin
cpsaran15-Sep-09 5:19
membercpsaran15-Sep-09 5:19 
GeneralRe: How to set WeekDate the first day of row ?! exactly that it is one day of the previous month [modified] Pin
Massimo Beatini15-Sep-09 22:38
memberMassimo Beatini15-Sep-09 22:38 
GeneralRe: How to set WeekDate the first day of row ?! exactly that it is one day of the previous month [modified] Pin
DaFleaster19-Mar-12 9:20
memberDaFleaster19-Mar-12 9:20 
GeneralRe: How to set WeekDate the first day of row ?! exactly that it is one day of the previous month Pin
Massimo Beatini15-Sep-09 22:51
memberMassimo Beatini15-Sep-09 22:51 
QuestionWay to pass date to calendar ? Pin
bluter13-Oct-06 5:25
memberbluter13-Oct-06 5:25 
AnswerRe: Way to pass date to calendar ? Pin
Massimo Beatini15-Nov-06 0:10
memberMassimo Beatini15-Nov-06 0:10 
QuestionDisable all the days before today! Pin
webmangr9-Oct-06 4:27
memberwebmangr9-Oct-06 4:27 
AnswerRe: Disable all the days before today! Pin
Massimo Beatini9-Oct-06 5:27
memberMassimo Beatini9-Oct-06 5:27 
GeneralRe: Disable all the days before today! Pin
webmangr9-Oct-06 22:31
memberwebmangr9-Oct-06 22:31 
GeneralRe: Disable all the days before today! Pin
webmangr9-Oct-06 22:38
memberwebmangr9-Oct-06 22:38 
GeneralRe: Disable all the days before today! Pin
webmangr9-Oct-06 22:44
memberwebmangr9-Oct-06 22:44 
GeneralRe: Disable all the days before today! Pin
Massimo Beatini10-Oct-06 0:08
memberMassimo Beatini10-Oct-06 0:08 
GeneralRe: Disable all the days before today! Pin
Massimo Beatini9-Oct-06 23:38
memberMassimo Beatini9-Oct-06 23:38 
AnswerRe: Disable all the days before today! Pin
clausont24-May-08 7:19
memberclausont24-May-08 7:19 
Generalsmart navigation problem Pin
shesho64211-Sep-06 3:07
membershesho64211-Sep-06 3:07 
GeneralInformations about calendar script Pin
Enrico Amisano7-Sep-06 7:51
memberEnrico Amisano7-Sep-06 7:51 
AnswerRe: Informations about calendar script Pin
Massimo Beatini8-Sep-06 5:58
memberMassimo Beatini8-Sep-06 5:58 
QuestionRe: Informations about calendar script Pin
webmangr9-Oct-06 3:41
memberwebmangr9-Oct-06 3:41 
GeneralPositioning and Changing the beginning of the Week Pin
vanvank7-Sep-06 4:37
membervanvank7-Sep-06 4:37 
AnswerRe: Positioning and Changing the beginning of the Week Pin
Massimo Beatini8-Sep-06 6:21
memberMassimo Beatini8-Sep-06 6:21 
Questionhow to display the day of the week and week number for given date Pin
janes7613-Jul-06 1:06
memberjanes7613-Jul-06 1:06 
AnswerRe: how to display the day of the week and week number for given date Pin
Massimo Beatini13-Jul-06 6:56
memberMassimo Beatini13-Jul-06 6:56 
QuestionHide onclick out Pin
webmangr13-Jul-06 0:43
memberwebmangr13-Jul-06 0:43 
AnswerRe: Hide onclick out Pin
Massimo Beatini14-Jul-06 5:54
memberMassimo Beatini14-Jul-06 5:54 
Questionhow to display the week number if the default start day for week is sunday Pin
happy_she2-Apr-06 22:16
memberhappy_she2-Apr-06 22:16 

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

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

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