Skip to content

JavaScript Calendar

JavaScript Calendar

Note: Looking for enhanced accessibility and advanced features? Check out CalendarJS, our feature-rich calendar component with comprehensive ARIA support and keyboard navigation. jSuites Calendar is perfect for lightweight implementations.

The jSuites Calendar is a lightweight JavaScript Calendar plugin and web component crafted to facilitate the creation of calendar components. It supports date selection, date pickers, and datetime pickers. Engineered for an outstanding user experience, it ensures compatibility across various devices.

  • Framework Agnostic: Seamless integration with React, Angular, VueJS, and more.
  • Responsiveness: Optimized for mobile devices.
  • Functionality: Enables date and time selection.
  • Custom Views: Supports year-month only display.
  • Event Handling: Offers multiple events for straightforward integration.
  • Flexibility: Available as a JavaScript Plugin or Web Component.
  • Date Constraints: Allows for the definition of valid date ranges.
  • Localization: Supports international settings for global usability.
npm install jsuites

Upon initializing a calendar instance, the following methods are available for programmatic interaction:

MethodDescription
open()Opens the calendar picker, allowing user interaction.
close(ignoreEvents)Closes the calendar. If ignoreEvents is set to true, the onclose event will not be triggered.
@param boolean ignoreEvents - Skips the onclose event.
getValue()Retrieves the currently selected date.
setValue(newValue)Sets a new date for the calendar.
@param mixed newValue - The new date, formatted as YYYY-MM-DD HH:MM:SS.
reset()Clears the current selection, resetting the input value.
next()Advances the calendar to the next month.
prev()Moves the calendar back to the previous month.

You can bind one of the following events to your calendar to seamlessly integrate it with your application:

MethodDescription
onchangeFires a method when the calendar's value changes.
onupdateInvokes a method whenever any update occurs in the calendar picker.
oncloseActivates a method once the calendar picker is closed.
onopenInitiates a method when the calendar picker is opened.

Customize the behavior of the calendar with the following initial settings:

PropertyDescription
type: stringDefines the picker type as either 'default' or 'year-month-picker'. Default is 'default'.
validRange: [ date, date ]Restricts date selection to a specified range. Default is null, allowing for unrestricted selection.
startingDay: numberSets the starting day of the week, with 0 being Sunday and 6 being Saturday. Default is 0 (Sunday).
format: stringSets the date format. Default format is 'YYYY-MM-DD'.
readonly: booleanIf true, disables manual changes to the input field.
today: booleanIf true, opens the calendar with today's date when no default value is set. Default is false.
time: booleanIf true, includes a time picker in the calendar. Default is false.
resetButton: booleanIf true, a reset button is displayed. Default is true.
placeholder: stringText displayed when the input field is empty.
months: arrayArray containing short names of the months. Default is ['Jan', 'Feb', 'Mar', ...].
monthsFull: arrayArray containing full names of the months. Default is ['January', 'February', ...].
weekdays: arrayArray containing names of the weekdays. Default is ['Sunday', 'Monday', ...].
weekdays_short: arrayArray containing short names of the weekdays. Default is ['S', 'M', ...].
value: stringSets a default date.
fullscreen: booleanIf true, renders the calendar in fullscreen. Default is true for screens with a width of 800px or less.
opened: booleanThe calendar opens by default if set to true. Default is false.
textDone: stringSets the text of the 'Done' button.
textReset: stringSets the text of the 'Reset' button.
textUpdate: stringSets the text of the 'Update' button.

Utilize the following static methods for general date and time operations:

Description
jSuites.calendar.prettify(date: string, text: object)
Converts a date string into a user-friendly time lapse from the current date.
jSuites.calendar.prettify()
Retrieves all dates on a page with the class .prettydate and prettifies them.
jSuites.calendar.now()
Returns the current date as a string.
jSuites.calendar.dateToNum(date: string|object) => number
Converts a date string or object into an Excel-like number (e.g., 45134).
jSuites.calendar.numToDate(date: number) => string
Converts an Excel-like number into a date string.
jSuites.calendar.extractDateFromString(date: string, format: string) => string
Attempts to convert a formatted date string into an ISO date string based on the provided format.
jSuites.calendar.getDateString(date: string, format: string)
Converts an ISO date string or date object into a formatted string.

The format consists of a versatile set of tokens that define the representation of dates and times. Here are a few examples of combination of tokens you can utilize:

TokenDescription
DAYRepresents the day of the week (e.g., Monday).
WDRepresents the abbreviated day of the week (e.g., Mon).
DDDDRepresents the full name of the day of the week (e.g., Monday).
DDDRepresents the abbreviated name of the day of the week (e.g., Mon).
DDRepresents the two-digit day of the month (e.g., 05).
DRepresents the day of the month (e.g., 5).
QRepresents the quarter of the year (e.g., Q1).
HH24Represents the hour in 24-hour format (e.g., 13 for 1 PM).
HH12Represents the hour in 12-hour format (e.g., 01 for 1 AM/PM).
HHRepresents the two-digit hour (e.g., 01 or 13).
HRepresents the hour (e.g., 1 or 13).
AM\PMRepresents the AM/PM indicator.
MIRepresents the two-digit minute (e.g., 07).
SSRepresents the two-digit second (e.g., 42).
MSRepresents the milliseconds (e.g., 345).
YYYYRepresents the four-digit year (e.g., 2022).
YYYRepresents the three-digit year (e.g., 022).
YYRepresents the two-digit year (e.g., 22).
YRepresents the year (e.g., 2022).
MONTHRepresents the full name of the month (e.g., January).
MONRepresents the abbreviated name of the month (e.g., Jan).
MMMMMRepresents the abbreviated name of the month (e.g., Jan).
MMMMRepresents the full name of the month (e.g., January).
MMMRepresents the abbreviated name of the month (e.g., Jan).
MMRepresents the two-digit month (e.g., 01).
MRepresents the month (e.g., 1).
Format examples
mm/dd/yyyy
dd/mm/yyyy
d/m/y
hh:mm:ss
d/m/h h:m:s

Create a simple date picker with default settings:

<html>
<script src="https://jsuites.net/v6/jsuites.js"></script>
<link rel="stylesheet" href="https://jsuites.net/v6/jsuites.css" type="text/css" />

<input id='calendar' />

<script>
jSuites.calendar(document.getElementById('calendar'), {
    format: 'DD/MM/YYYY',
    placeholder: 'Select a date'
});
</script>
</html>

Implement a responsive date and time input with a calendar picker. This tool enables users to select dates and times according to a specified format and monitors events triggered by changes.

<html>
<script src="https://jsuites.net/v6/jsuites.js"></script>
<link rel="stylesheet" href="https://jsuites.net/v6/jsuites.css" type="text/css" />
<input id='calendar' />
<input type="button" value="getValue()" id="btn1" />
<script>
let instance = jSuites.calendar(document.getElementById('calendar'), {
    format: 'DD/MM/YYYY HH:MM',
    time: true,
    onchange: function() {
        console.log(arguments)
    }
});

document.getElementById('btn1').addEventListener('click', function() {
    alert(instance.getValue());
});
</script>
</html>

Incorporate a calendar picker with customizable actions to streamline date and time selection while enabling automated programmatic responses.

<html>
<script src="https://jsuites.net/v6/jsuites.js"></script>
<link rel="stylesheet" href="https://jsuites.net/v6/jsuites.css" type="text/css" />
<div id='calendar'></div>
<input type="button" value="getValue()" id="btn2"/>
<script>
let instance = jSuites.calendar(document.getElementById('calendar'), {
    format: 'DD/MM/YYYY HH:MM',
    onupdate: function() {
        console.log(arguments)
    }
});

document.getElementById('btn2').addEventListener('click', function() {
    alert(instance.getValue());
})
</script>
</html>

Explore utilizing the jSuites calendar as a calendar picker web component and binding JavaScript events for enhanced functionality.

<html>
<script src="https://jsuites.net/v6/jsuites.js"></script>
<script src="https://jsuites.net/v6/jsuites.webcomponents.js"></script>
<link rel="stylesheet" href="https://jsuites.net/v6/jsuites.css" type="text/css" />

<jsuites-calendar value="2020-01-20"></jsuites-calendar>

<script>
document.querySelector('jsuites-calendar').addEventListener('onchange', function(e) {
    console.log('New value: ' + e.target.value);
});
document.querySelector('jsuites-calendar').addEventListener('onclose', function(e) {
    console.log('Calendar is closed');
});
</script>
</html>

Restrict date selection to a specific range:

<html>
<script src="https://jsuites.net/v6/jsuites.js"></script>
<link rel="stylesheet" href="https://jsuites.net/v6/jsuites.css" type="text/css" />

<input id='calendar' />

<script>
jSuites.calendar(document.getElementById('calendar'), {
    format: 'DD/MM/YYYY',
    validRange: ['2024-01-01', '2024-12-31'],
    placeholder: 'Select a date in 2024'
});
</script>
</html>

Create a calendar that only allows year and month selection:

<html>
<script src="https://jsuites.net/v6/jsuites.js"></script>
<link rel="stylesheet" href="https://jsuites.net/v6/jsuites.css" type="text/css" />

<input id='calendar' />

<script>
jSuites.calendar(document.getElementById('calendar'), {
    type: 'year-month-picker',
    format: 'MMM/YYYY',
    placeholder: 'Select month and year'
});
</script>
</html>

Implement a calendar with a custom date format:

<html>
<script src="https://jsuites.net/v6/jsuites.js"></script>
<link rel="stylesheet" href="https://jsuites.net/v6/jsuites.css" type="text/css" />

<input id='calendar' />

<script>
jSuites.calendar(document.getElementById('calendar'), {
    format: 'DDD, DD MMMM YYYY',
    value: '2024-06-15'
});
</script>
</html>

Implement comprehensive event handlers to respond to calendar interactions:

<html>
<script src="https://jsuites.net/v6/jsuites.js"></script>
<link rel="stylesheet" href="https://jsuites.net/v6/jsuites.css" type="text/css" />

<input id='calendar' />
<div id="console" style="margin-top: 20px; padding: 10px; background: #f5f5f5;"></div>

<script>
jSuites.calendar(document.getElementById('calendar'), {
    format: 'DD/MM/YYYY',
    value: '2024-06-15',
    onchange: function(el, currentValue, previousValue) {
        document.getElementById('console').innerHTML =
            'Changed from: ' + previousValue + ' to: ' + currentValue;
    },
    onopen: function(el) {
        console.log('Calendar opened');
    },
    onclose: function(el) {
        console.log('Calendar closed');
    },
    onupdate: function(el, value) {
        console.log('Calendar updated:', value);
    }
});
</script>
</html>

Explore additional use cases for the jSuites JavaScript Calendar plugin:

LemonadeJS Reactive JavaScript Calendar is free JavaScript Plugin that can create range picker, has keyboard navigation and focus on the best user experience.


For applications requiring advanced features, CalendarJS offers:

  • Enhanced keyboard navigation with full ARIA support
  • Advanced date range selection with improved UX
  • Comprehensive validation options
  • Framework-agnostic with React/Vue/Angular support
  • Smaller bundle size (3.18KB gzipped)

Explore CalendarJS →