Skip to Content | Skip to Navigation


dijit/Calendar

Authors:Adam Peller
Project owner:Adam Peller

The Calendar widget displays a localized month-view calendar and allows the user to navigate months and years and select a date. It is typically used as part of the DateTextBox which includes a text box and uses the Calendar as a drop-down. Localizations for hundreds of languages and cultures are included as part of the Dojo Toolkit. The locale will be chosen based on the dojoConfig.locale setting of your page, using navigator.language by default.

Navigating between months is possible with the arrow icons, and the next and previous year may be clicked to change to that year. Holding the mouse down on these controls will repeat the action. Starting with version 1.4, the month name is a drop-down control for selecting a different month.

The Calendar widget has been supported as a standalone widget since version 1.4. Prior to that, to meet the accessibility requirements of the Dijit project which requires accessibility on all widgets, the implementation was private and began with an underscore character: dijit._Calendar.

Non-Gregorian calendar use is possible using the datePackage attribute and experimental date classes in dojox.date.

Examples

A plain Calendar widget with the formatted date below:

require([
    "dojo/parser",
    "dijit/dijit", // loads the optimized dijit layer
    "dijit/Calendar"
]);
<div data-dojo-type="dijit/Calendar">
    <script type="dojo/method" data-dojo-event="onChange" data-dojo-args="value">
        require(["dojo/dom", "dojo/date/locale"], function(dom, locale){
            dom.byId('formatted').innerHTML = locale.format(value, {formatLength: 'full', selector:'date'});
        });
    </script>
</div>
<p id="formatted"></p>
.{{ theme }} table.dijitCalendarContainer {
    margin: 25px auto;
}
#formatted {
    text-align: center;
}

With an initial selection and weekends disabled:

require([
    "dojo/parser",
    "dijit/dijit", // loads the optimized dijit layer
    "dijit/Calendar"
]);
<div id="mycal" data-dojo-type="dijit/Calendar" value="2009-08-07" data-dojo-props="isDisabledDate:dojo.date.locale.isWeekend"></div>
.{{ theme }} .dijitCalendarDisabledDate {
    background-color:#333 !important;
    text-decoration:none !important;
}

.{{ theme }} table.dijitCalendarContainer {
    margin: 25px auto;
}

JavaScript declaration, with a restriction of +/- one week from the current date:

require([
    "dojo/ready",
    "dijit/dijit", // loads the optimized dijit layer
    "dijit/Calendar",
    "dojo/date"
], function(ready, dijit, Calendar, date){
    ready(function(){
        new Calendar({
        value: new Date(),
        isDisabledDate: function(d){
            var d = new Date(d); d.setHours(0, 0, 0, 0);
            var today = new Date(); today.setHours(0, 0, 0, 0);
            return Math.abs(date.difference(d, today, "week")) > 0;
        }
        }, "mycal");
    });
});
<div id="mycal"></div>
.{{ theme }} table.dijitCalendarContainer {
  margin: 25px auto;
  width: 200px;
}

Custom styling:

require([
    "dojo/parser",
    "dijit/dijit", // loads the optimized dijit layer
    "dijit/Calendar"
]);
<input id="calendar5" data-dojo-type="dijit/Calendar" data-dojo-props="dayWidth:'abbr'" value="2008-03-13" />
#calendar5 .dijitCalendarDateTemplate { height: 50px; width: 50px; border: 1px solid #ccc; vertical-align: top }
#calendar5 .dijitCalendarDateLabel, #calendar5 .dijitCalendarDateTemplate { text-align: inherit }
#calendar5 .dijitCalendarDayLabel { font-weight: bold }
#calendar5 .dijitCalendarSelectedYear { font-size: 1.5em }
#calendar5 .dijitCalendarMonthLabel { font-family: serif; letter-spacing: 0.2em; font-size: 2em }

[1.4+] Non-Gregorian calendars:

  require([
    "dijit/dijit", // loads the optimized dijit layer
    "dijit/Calendar",
    "dijit/registry",
    "dojo/dom",
    "dojox/date/hebrew",
    "dojox/date/hebrew/Date",
    "dojox/date/hebrew/locale",
    "dojox/date/islamic",
    "dojox/date/islamic/Date",
    "dojox/date/islamic/locale"
], function(dijit, Calendar, registry, dom, hebrew){
    this.publishing = false;

    this.publishDate = function(d){
        if(!publishing){
            publishing = true;
            dojo.publish("date", [{date: d.toGregorian ? d.toGregorian() : d, id: this.id}]);
            publishing = false;
        }
    }

    dojo.subscribe("date", function(data){
        registry.filter(function(widget){ return widget.id != data.id; }).forEach(function(widget){ widget.set('value', data.date); });
    });

    this.formatDate = function(d){
        var datePackage = (this.id == "gregorian") ? dojo.date : dojox.date[this.id];
            dom.byId(this.id+"Formatted").innerHTML = datePackage.locale.format(arguments[0], {
            formatLength: 'long',
            selector: 'date'
        });
    }
});
<table class="container">
    <tr>
        <td>
            <div id="hebrew" data-dojo-type="dijit/Calendar" data-dojo-props="datePackage:dojox.date.hebrew, onValueSelected:publishDate, onChange:formatDate"></div>
            <div id="hebrewFormatted"></div>
        </td>
        <td>
            <div id="islamic" data-dojo-type="dijit/Calendar" data-dojo-props="datePackage:dojox.date.islamic, onValueSelected:publishDate, onChange:formatDate"></div>
            <div id="islamicFormatted"></div>
        </td>
        <td>
            <div id="gregorian" data-dojo-type="dijit/Calendar" data-dojo-props="onValueSelected:publishDate, onChange:formatDate"></div>
            <div id="gregorianFormatted"></div>
        </td>
    </tr>
</table>

Note

dijit._Calendar was upgraded to dijit.Calendar in version 1.4. An alias is provided for backwards compatibility.

See Also

  • dojox.widget.Calendar - An enhanced but still experimental calendar widget which has additional capabilities like year-only views and animation effects.

Accessibility

As of 1.6 full keyboard support has been implemented for the Calendar.

Keyboard

Action Key
Navigate between date cells Left, Right, Up, and down arrows
Navigate to same day in next month Page-down
Navigate to same day in previous month Page-up
Navigate to same day in next year Control+Page-down
Navigate to same day in previous year Control+Page-up
Navigate to first day in month Home
Navigate to last day in month End
Select the date Enter, Space

Screen Reader Issues

The Calendar has been implemented as a table so standard table announcements and navigation work as expected with JAWS 12. As the user arrows through the table the day number is announced. As the user moves from column to column the weekday column headers are announced as well. For en-us locales these are the first letters of the days of the week: S, M, T, W, T, F, S. The month name is also included when it changes. The current year has been assigned as the label for the Calendar table and is also announced when it changes.