Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions CalendarPicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,25 @@ if (!Element.prototype.closest) {
*/
function CalendarPicker(element, options) {
// Core variables.
this.date = new Date();
this._formatDateToInit(this.date);
var date = options.date ?
new Date(Math.min(options.max || Infinity,
Math.max(options.min || -Infinity, options.date))
) : new Date();
this.date = this._truncateTime(date);

this.day = this.date.getDay()
this.month = this.date.getMonth();
this.year = this.date.getFullYear();

// Storing the todays date for practical reasons.
this.today = this.date;
this.today = this._truncateTime(new Date());

// The calendars value should always be the current date.
this.value = this.date;

// Ranges for the calendar (optional).
this.min = options.min;
this.max = options.max;
this._formatDateToInit(this.min);
this._formatDateToInit(this.max);
this.min = this._truncateTime(options.min);
this.max = this._truncateTime(options.max);

// Element to insert calendar in.
this.userElement = document.querySelector(element);
Expand Down Expand Up @@ -121,12 +122,13 @@ CalendarPicker.prototype._getDaysInMonth = function (month, year) {
}

/**
* @param {DateObject} date.
* @description Sets the clock of a date to 00:00:00 to be consistent.
* @param {Date} date.
* @return {Date} new date with time set to 00:00:00.
* @description Sets the time of a date to 00:00:00 to be consistent.
*/
CalendarPicker.prototype._formatDateToInit = function (date) {
CalendarPicker.prototype._truncateTime = function (date) {
if (!date) return;
date.setHours(0, 0, 0);
return new Date(date.getFullYear(), date.getMonth(), date.getDate())
}

/**
Expand Down