jclarke0000 / MMM-MyCalendar

Alternative calendar module for MagicMirror.
57 stars 17 forks source link

Today/tomorrow still wrong #3

Closed skre closed 6 years ago

skre commented 6 years ago

24h ahead of time :-)

BobH1954 commented 6 years ago

Still wrong for me, too

Daveladd commented 6 years ago

Has anyone found the bug yet? My todays and tomorrows are all silly too.

j0nix commented 6 years ago

Looking @ code there seems to be some assumption that if time between now and start for event is less than one day we are in the context of today. But what one should establish is if event and now is on the same day.

Made a quickfix, that I haven't properly tested so there could be errors... but perhaps gives a hint of how I think this should be solved.

diff -Naur MMM-MyCalendar.js MMM-MyCalendar.js.new
--- MMM-MyCalendar.js   2017-11-10 23:06:08.954998592 +0100
+++ MMM-MyCalendar.js.new   2017-11-10 23:04:06.793270392 +0100
@@ -253,18 +253,30 @@
            } else {
                if (event.startDate >= new Date()) {
                    if (event.startDate - now < 6 * oneDay) {
+
+                       var eventAsDateObj = new Date(parseInt(event.startDate));
+                       var EVENT = eventAsDateObj.getDay();
+                       var TODAY = now.getDay();
+                       var TOMORROW = TODAY+1;
+                       // Do we need to wrap to day 0?
+                       if(TOMORROW == 7) {
+                           TOMORROW = 0; 
+                       }
+                           
                        if (event.startDate - now < this.config.getRelative * oneHour) {
                            // If event is within 6 hour, display 'in xxx' time format or moment.fromNow()
                            timeWrapper.innerHTML = this.capFirst(moment(event.startDate, "x").fromNow());
-                       } else if (event.startDate - now < 1 * oneDay) {
+
+                       } else if (EVENT == TODAY) {
                            // This event is today
                            timeWrapper.innerHTML = this.capFirst(this.translate("TODAY")) + " " + this.config.joiningWord + " " + this.capFirst(moment(event.startDate, "x").format(this.config.timeFormat));
-                       } else if (event.startDate - now < 2 * oneDay) {
+                       } else if (EVENT == TOMORROW) {
                            // This event is tomorrow
                            timeWrapper.innerHTML = this.capFirst(this.translate("TOMORROW")) + " " + this.config.joiningWord + " " + this.capFirst(moment(event.startDate, "x").format(this.config.timeFormat));
-                     } else {
+                       } else {
                            timeWrapper.innerHTML = this.capFirst(moment(event.startDate, "x").format(this.config.dayOfWeekFormat + " [" + this.config.joiningWord + "] " + this.config.timeFormat));
                        }
+
                    } else {
                        /* Check to see if the user displays absolute or relative dates with their events
                         * Also check to see if an event is happening within an 'urgency' time frameElement
Daveladd commented 6 years ago

^ Looks pretty good to me, I think using .getDay() is a better idea than new Date() since on each refresh any startDate < 24hr is registering as today. I still wonder why this bug just surfaced been working fine for ~6 months and the code for selecting today/tomorrow looks the same as that of the default calendar. I'll try out your code and let you know if there are any issues with it.Thanks.

ghost commented 6 years ago

I am having the same troubles. Is there a fix. If the code above is good can I get a copy of the .js file

jclarke0000 commented 6 years ago

This is fixed now. If you are on MagicMirror 2.2.0 or newer, just do a git pull. If you are on an earlier version, then you'll need to manually install v1.0, here: https://github.com/jclarke0000/MMM-MyCalendar/releases/tag/v1.0