From 2e1a8711d333b0035ada6c0845b162aac63dadfe Mon Sep 17 00:00:00 2001 From: Philip Allgaier Date: Tue, 29 Dec 2020 07:17:47 +0100 Subject: [PATCH 1/2] Fix incorrect date selection ranges for history and logbook --- src/panels/history/ha-panel-history.ts | 5 +++-- src/panels/logbook/ha-panel-logbook.ts | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/panels/history/ha-panel-history.ts b/src/panels/history/ha-panel-history.ts index 566aa3c0da4d..725ad41f12f9 100644 --- a/src/panels/history/ha-panel-history.ts +++ b/src/panels/history/ha-panel-history.ts @@ -119,13 +119,13 @@ class HaPanelHistory extends LitElement { todayEnd.setDate(todayEnd.getDate() + 1); todayEnd.setMilliseconds(todayEnd.getMilliseconds() - 1); - const todayCopy = new Date(today); - + let todayCopy = new Date(today); const yesterday = new Date(todayCopy.setDate(today.getDate() - 1)); const yesterdayEnd = new Date(yesterday); yesterdayEnd.setDate(yesterdayEnd.getDate() + 1); yesterdayEnd.setMilliseconds(yesterdayEnd.getMilliseconds() - 1); + todayCopy = new Date(today); // set again to prevent issues in case yesterdayEnd crossed into previous month const thisWeekStart = new Date( todayCopy.setDate(today.getDate() - today.getDay()) ); @@ -134,6 +134,7 @@ class HaPanelHistory extends LitElement { ); thisWeekEnd.setMilliseconds(thisWeekEnd.getMilliseconds() - 1); + todayCopy = new Date(today); // set again to prevent issues in case thisWeekEnd crossed into next month const lastWeekStart = new Date( todayCopy.setDate(today.getDate() - today.getDay() - 7) ); diff --git a/src/panels/logbook/ha-panel-logbook.ts b/src/panels/logbook/ha-panel-logbook.ts index 87e1b1f5193e..c2d6c6779a5c 100644 --- a/src/panels/logbook/ha-panel-logbook.ts +++ b/src/panels/logbook/ha-panel-logbook.ts @@ -147,13 +147,13 @@ export class HaPanelLogbook extends LitElement { todayEnd.setDate(todayEnd.getDate() + 1); todayEnd.setMilliseconds(todayEnd.getMilliseconds() - 1); - const todayCopy = new Date(today); - + let todayCopy = new Date(today); const yesterday = new Date(todayCopy.setDate(today.getDate() - 1)); const yesterdayEnd = new Date(yesterday); yesterdayEnd.setDate(yesterdayEnd.getDate() + 1); yesterdayEnd.setMilliseconds(yesterdayEnd.getMilliseconds() - 1); + todayCopy = new Date(today); // set again to prevent issues in case yesterdayEnd crossed into previous month const thisWeekStart = new Date( todayCopy.setDate(today.getDate() - today.getDay()) ); @@ -162,6 +162,7 @@ export class HaPanelLogbook extends LitElement { ); thisWeekEnd.setMilliseconds(thisWeekEnd.getMilliseconds() - 1); + todayCopy = new Date(today); // set again to prevent issues in case thisWeekEnd crossed into next month const lastWeekStart = new Date( todayCopy.setDate(today.getDate() - today.getDay() - 7) ); From 231128f9126e9d1725e8a342ae247744c7a5bd04 Mon Sep 17 00:00:00 2001 From: Philip Allgaier Date: Sat, 2 Jan 2021 14:25:45 +0100 Subject: [PATCH 2/2] Streamlined date range calculations --- src/panels/history/ha-panel-history.ts | 29 ++++++++++---------------- src/panels/logbook/ha-panel-logbook.ts | 29 ++++++++++---------------- 2 files changed, 22 insertions(+), 36 deletions(-) diff --git a/src/panels/history/ha-panel-history.ts b/src/panels/history/ha-panel-history.ts index 725ad41f12f9..dcfa3272855a 100644 --- a/src/panels/history/ha-panel-history.ts +++ b/src/panels/history/ha-panel-history.ts @@ -119,28 +119,21 @@ class HaPanelHistory extends LitElement { todayEnd.setDate(todayEnd.getDate() + 1); todayEnd.setMilliseconds(todayEnd.getMilliseconds() - 1); - let todayCopy = new Date(today); - const yesterday = new Date(todayCopy.setDate(today.getDate() - 1)); - const yesterdayEnd = new Date(yesterday); - yesterdayEnd.setDate(yesterdayEnd.getDate() + 1); + const yesterday = new Date(today); + yesterday.setDate(today.getDate() - 1); + const yesterdayEnd = new Date(today); yesterdayEnd.setMilliseconds(yesterdayEnd.getMilliseconds() - 1); - todayCopy = new Date(today); // set again to prevent issues in case yesterdayEnd crossed into previous month - const thisWeekStart = new Date( - todayCopy.setDate(today.getDate() - today.getDay()) - ); - const thisWeekEnd = new Date( - todayCopy.setDate(thisWeekStart.getDate() + 7) - ); + const thisWeekStart = new Date(today); + thisWeekStart.setDate(today.getDate() - today.getDay()); + const thisWeekEnd = new Date(thisWeekStart); + thisWeekEnd.setDate(thisWeekStart.getDate() + 7); thisWeekEnd.setMilliseconds(thisWeekEnd.getMilliseconds() - 1); - todayCopy = new Date(today); // set again to prevent issues in case thisWeekEnd crossed into next month - const lastWeekStart = new Date( - todayCopy.setDate(today.getDate() - today.getDay() - 7) - ); - const lastWeekEnd = new Date( - todayCopy.setDate(lastWeekStart.getDate() + 7) - ); + const lastWeekStart = new Date(today); + lastWeekStart.setDate(today.getDate() - today.getDay() - 7); + const lastWeekEnd = new Date(lastWeekStart); + lastWeekEnd.setDate(lastWeekStart.getDate() + 7); lastWeekEnd.setMilliseconds(lastWeekEnd.getMilliseconds() - 1); this._ranges = { diff --git a/src/panels/logbook/ha-panel-logbook.ts b/src/panels/logbook/ha-panel-logbook.ts index c2d6c6779a5c..f9054ff86eb9 100644 --- a/src/panels/logbook/ha-panel-logbook.ts +++ b/src/panels/logbook/ha-panel-logbook.ts @@ -147,28 +147,21 @@ export class HaPanelLogbook extends LitElement { todayEnd.setDate(todayEnd.getDate() + 1); todayEnd.setMilliseconds(todayEnd.getMilliseconds() - 1); - let todayCopy = new Date(today); - const yesterday = new Date(todayCopy.setDate(today.getDate() - 1)); - const yesterdayEnd = new Date(yesterday); - yesterdayEnd.setDate(yesterdayEnd.getDate() + 1); + const yesterday = new Date(today); + yesterday.setDate(today.getDate() - 1); + const yesterdayEnd = new Date(today); yesterdayEnd.setMilliseconds(yesterdayEnd.getMilliseconds() - 1); - todayCopy = new Date(today); // set again to prevent issues in case yesterdayEnd crossed into previous month - const thisWeekStart = new Date( - todayCopy.setDate(today.getDate() - today.getDay()) - ); - const thisWeekEnd = new Date( - todayCopy.setDate(thisWeekStart.getDate() + 7) - ); + const thisWeekStart = new Date(today); + thisWeekStart.setDate(today.getDate() - today.getDay()); + const thisWeekEnd = new Date(thisWeekStart); + thisWeekEnd.setDate(thisWeekStart.getDate() + 7); thisWeekEnd.setMilliseconds(thisWeekEnd.getMilliseconds() - 1); - todayCopy = new Date(today); // set again to prevent issues in case thisWeekEnd crossed into next month - const lastWeekStart = new Date( - todayCopy.setDate(today.getDate() - today.getDay() - 7) - ); - const lastWeekEnd = new Date( - todayCopy.setDate(lastWeekStart.getDate() + 7) - ); + const lastWeekStart = new Date(today); + lastWeekStart.setDate(today.getDate() - today.getDay() - 7); + const lastWeekEnd = new Date(lastWeekStart); + lastWeekEnd.setDate(lastWeekStart.getDate() + 7); lastWeekEnd.setMilliseconds(lastWeekEnd.getMilliseconds() - 1); this._ranges = {