Author: | marssola |
---|---|
Views Total: | 21,054 views |
Official Page: | Go to website |
Last Update: | October 11, 2019 |
License: | MIT |
Preview:

Description:
A simple, lightweight, vanilla JavaScript inline calendar that allows you to filter dates, set available dates/weekdays, enable/disable past dates and much more.
How to use it:
Import the vanilla-calendar’s JavaScript and CSS files:
<link rel="stylesheet" href="src/css/vanilla-calendar-min.css"> <script src="src/js/vanilla-calendar-min.js"></script>
Create a placeholder element for the inline calendar.
<div id="myCalendar" class="vanilla-calendar"></div>
Create a new instance and render a basic calendar inside the container you just created.
let myCalendar = new VanillaCalendar({ selector: "#myCalendar" })
Determine whether to enable past dates. Default: true.
let myCalendar = new VanillaCalendar({ selector: "#myCalendar", pastDates: false })
Set the available dates & weekdays. Default: empty array.
let myCalendar = new VanillaCalendar({ selector: "#myCalendar", availableWeekDays: [ {day: 'monday', others: values}, {day: 'tuesday', others: values} ], availableDates: [ {date: '2019-09-15', others: values}, {date: '2019-09-16', others: values}, {date: '2019-09-17', others: values}, {date: '2019-09-25', others: values}, {date: '2019-09-26', others: values} ] })
Enable/disable date filtering. Default: false.
let myCalendar = new VanillaCalendar({ selector: "#myCalendar", datesFilter: true })
Localize the month & week names.
let myCalendar = new VanillaCalendar({ selector: "#myCalendar", months: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'], shortWeekday: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'] })
Do some cool stuff as the user select a date.
let myCalendar = new VanillaCalendar({ selector: "#myCalendar", onSelect: (data, elem) => {} })
Customize the init date & today’s date.
let myCalendar = new VanillaCalendar({ selector: "#myCalendar", date: new Date(), todaysDate: new Date() })
API methods.
// re-init the calendar myCalendar.init(); // destroy the calendar myCalendar.destroy(); // reset the calendar myCalendar.reset(); // update options myCalendar.set(options);
Changelog:
10/11/2019
- Change month buttons submit forms if calendar is placed inside a form
It works very nice. Congratulations !
Question : How do you do for starting the week on monday ? (instead of sunday ?)
Did you figure out how to access date values from onSelector: (data, elem) => {}
I rewrite part of source code ( https://github.com/marssola/vanilla-calendar/blob/master/src/js/vanilla-calendar.js ) for create Monday first day of week
crete getDay function
“`
const getDay = function (date) {
let d = date.getDay() – 1
if (d === -1) d = 6
return d
}
“`
and replace in code all date.getDay() to getDay(date)
and this is my weekdays list – shortWeekday: [‘Пн’, ‘Вт’, ‘Ср’, ‘Чт’, ‘Пт’, ‘Сб’, ‘Вс’]
Thanks, useful correction
Thanks ! your code tweak worked but with one small change at line 2, where the console gave me a “illegal character error”.
I changed this line->
/*
let d = date.getDay() – 1
*/
With this ->
/*
let d = date.getDay() – 1
*/
Thanks @Alex, really handy version.
To clarify for anybody who doesn’t speak your language, the next step is to move “sun” to the end of the array as in
shortWeekday: [‘Mon’, ‘Tue’, ‘Wed’, ‘Thu’, ‘Fri’, ‘Sat’,”Sun”],
How do you access the date value?
onSelect: (data, elem) => {
console.log(data, elem);
alert(data.date);
}
// data.date is the datevalue
how to differentiate holidays in different colours?
Hi there,
How to set a start date ? ie: i don’t want the user be able to select a date before today’s date +2.
Did you ever get this answered.. I am also interested
Hello ,
How to split value ‘ date month year ‘ like 20-10-2020
cause when i’m use data.date it like ” Tue Oct 20 2002 14:28:42 … ”
I’m just want date month and year but i don’t know how to get it
so, thank for help
ok i’m slice them by js
How can i set a custom date to selected date in calendar?
Yes, after a user selects a date, we save it to the db. next time user returns, we want to show/highlight the date they chose previously. certainly, there must be way? otherwise, although nice it’s free, is useless. thanks
To set a selected date you can do
// set the selected date of the calendar
let selectedDate = document.querySelectorAll(‘[data-calendar-date=”‘ + mySelectedDate + ‘”]’)
selectedDate.forEach(date => {
date.classList.add(‘vanilla-calendar-date–selected’)
})