From 133858fccfcaa17a56f3e16f2c20618a067d193c Mon Sep 17 00:00:00 2001 From: Jarmo Pertman Date: Sun, 27 Mar 2022 13:36:31 +0300 Subject: [PATCH 1/5] Add support for specifying initial date instead of today --- CalendarPicker.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/CalendarPicker.js b/CalendarPicker.js index 758d334..34dd929 100644 --- a/CalendarPicker.js +++ b/CalendarPicker.js @@ -18,7 +18,11 @@ if (!Element.prototype.closest) { */ function CalendarPicker(element, options) { // Core variables. - this.date = new Date(); + this.date = options.date ? + new Date(Math.min( + options.max || new Date(9999, 0), + Math.max(options.min || null, options.date)) + ) : new Date(); this._formatDateToInit(this.date); this.day = this.date.getDay() From f7de646ccef16f9e0516fc6cef89a7139ccd9a16 Mon Sep 17 00:00:00 2001 From: Jarmo Pertman Date: Sun, 27 Mar 2022 13:47:13 +0300 Subject: [PATCH 2/5] Use better values for setting date via options --- CalendarPicker.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CalendarPicker.js b/CalendarPicker.js index 34dd929..9b23f60 100644 --- a/CalendarPicker.js +++ b/CalendarPicker.js @@ -20,8 +20,8 @@ function CalendarPicker(element, options) { // Core variables. this.date = options.date ? new Date(Math.min( - options.max || new Date(9999, 0), - Math.max(options.min || null, options.date)) + options.max || Infinity, + Math.max(options.min || 0, options.date)) ) : new Date(); this._formatDateToInit(this.date); From c1f0c28432638c7666c1b22ef25afed30d8f394d Mon Sep 17 00:00:00 2001 From: Jarmo Pertman Date: Sun, 27 Mar 2022 14:11:24 +0300 Subject: [PATCH 3/5] Add support for specifying initial date with year less than 1970 --- CalendarPicker.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/CalendarPicker.js b/CalendarPicker.js index 9b23f60..54f4718 100644 --- a/CalendarPicker.js +++ b/CalendarPicker.js @@ -19,9 +19,8 @@ if (!Element.prototype.closest) { function CalendarPicker(element, options) { // Core variables. this.date = options.date ? - new Date(Math.min( - options.max || Infinity, - Math.max(options.min || 0, options.date)) + new Date(Math.min(options.max || Infinity, + Math.max(options.min || new Date().setFullYear(1), options.date)) ) : new Date(); this._formatDateToInit(this.date); From 7d2470feb709b9bbc8fddef17729ee474a85cc03 Mon Sep 17 00:00:00 2001 From: Jarmo Pertman Date: Sun, 27 Mar 2022 14:41:36 +0300 Subject: [PATCH 4/5] Use even better value for setting initial date --- CalendarPicker.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CalendarPicker.js b/CalendarPicker.js index 54f4718..5b20dd4 100644 --- a/CalendarPicker.js +++ b/CalendarPicker.js @@ -20,7 +20,7 @@ function CalendarPicker(element, options) { // Core variables. this.date = options.date ? new Date(Math.min(options.max || Infinity, - Math.max(options.min || new Date().setFullYear(1), options.date)) + Math.max(options.min || -Infinity, options.date)) ) : new Date(); this._formatDateToInit(this.date); From 44f44addb2508af82177dbdb15ac0afeed973d92 Mon Sep 17 00:00:00 2001 From: Jarmo Pertman Date: Mon, 20 Jun 2022 13:53:58 +0300 Subject: [PATCH 5/5] Fix today when this.date is provided via options and might not be today --- CalendarPicker.js | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/CalendarPicker.js b/CalendarPicker.js index 5b20dd4..31f7aa8 100644 --- a/CalendarPicker.js +++ b/CalendarPicker.js @@ -18,27 +18,25 @@ if (!Element.prototype.closest) { */ function CalendarPicker(element, options) { // Core variables. - this.date = options.date ? + var date = options.date ? new Date(Math.min(options.max || Infinity, Math.max(options.min || -Infinity, options.date)) ) : new Date(); - this._formatDateToInit(this.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); @@ -124,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()) } /**