• Recent
  • Tags
  • Unsolved
  • Solved
  • MagicMirror² Repository
  • Documentation
  • 3rd-Party-Modules
  • Donate
  • Discord
  • Register
  • Login
MagicMirror Forum
  • Recent
  • Tags
  • Unsolved
  • Solved
  • MagicMirror² Repository
  • Documentation
  • 3rd-Party-Modules
  • Donate
  • Discord
  • Register
  • Login
A New Chapter for MagicMirror: The Community Takes the Lead
Read the statement by Michael Teeuw here.

Changing moment.js behavior

Scheduled Pinned Locked Moved Troubleshooting
12 Posts 5 Posters 9.0k Views 3 Watching
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    yo-less Module Developer
    last edited by yo-less Aug 30, 2016, 6:58 PM Aug 30, 2016, 6:57 PM

    I guess this question would be directed at @MichMich, but anyone who can help me with this, your ideas would be much appreciated.

    The calendar currently shows “tomorrow” for events that are scheduled to occur tomorrow. Events that are scheduled to occur a day after tomorrow are shown as “in a day”. Now “tomorrow” and “in a day” are pretty much the same thing in German, so I wanted to include “the day after tomorrow” and was pointed in a good direction by the moment.js people.

    However, I’m not able to change the ‘de’ locale file so that it reflects that on my mirror, it just keeps showing the default day definitions, no matter what I do. And I was wondering where I could make the ‘diff < 3’ adjustment for the day after tomorrow so I could include a new moment definition?

    1 Reply Last reply Reply Quote 0
    • K Offline
      KirAsh4 Moderator
      last edited by Aug 30, 2016, 7:01 PM

      That’s strictly a moment.js thing … However, what we humans perceive as ‘in a day’ and ‘tomorrow’ aren’t actually the same thing.

      moment.js calculates based on midnight. So, an event that happens before midnight tomorrow is considered to be tomorrow. An event that happens after midnight tomorrow, is considered ‘in a day’ because it counts starting AT midnight, and not from the current time. So current time TO midnight, plus 1 day = ‘in a day’. It’s not how we calculate time intuitively, but it’s how computers calculate time, and thus moment.js does that.

      A Life? Cool! Where can I download one of those from?

      Y 1 Reply Last reply Aug 30, 2016, 7:09 PM Reply Quote 0
      • Y Offline
        yo-less Module Developer @KirAsh4
        last edited by Aug 30, 2016, 7:09 PM

        @KirAsh4 Thanks for the helpful clarification. Wouldn’t it be easy to change that behavior so that it would start calculating based on the current point of time?

        Regardless of that, I would still like to change the “in a day” notice to “the day after tomorrow”, do you know how I could accomplish that? It seems that /vendor/moment/moment-with-locales.js has role to play there.

        1 Reply Last reply Reply Quote 0
        • K Offline
          KirAsh4 Moderator
          last edited by Aug 30, 2016, 7:18 PM

          You’d have to talk to the moment.js folks (or figure out how to calculate the time difference yourself and pass it on to moment.js to parse for you). If you manually change the moment locales, it will get overwritten at the next update.

          A Life? Cool! Where can I download one of those from?

          1 Reply Last reply Reply Quote 0
          • Y Offline
            yo-less Module Developer
            last edited by Aug 30, 2016, 7:50 PM

            If I can figure out a way to change it locally in my MagicMirror, it’s easier for me to suggest changes to the moment.js crew, however as of now, I can’t change anything, no matter what I do, my calendar keeps reporting the same old information, especially for “tomorrow”. I have figured out a way to change some of the information, such as “in two days” to something like “inn two days” just to see where adjustments have to be made, but no matter what I do, it always says “morgen” for “tomorrow”.

            I was actually trying to change what the folks at moment.js suggested, but it seemed like my modifications had no impact whatsoever :)

            1 Reply Last reply Reply Quote 0
            • Y Offline
              yo-less Module Developer
              last edited by yo-less Aug 30, 2016, 9:24 PM Aug 30, 2016, 9:22 PM

              I’ve just realized that both “today” and “tomorrow” are taken over by the MagicMirror translation engine, this would explain why no change to moments.js locale files made any difference :).

              So if anyone is ever thinking about adjusting the calendar phrase for “today” or “tomorrow”, take a look at your MagicMirror/translations/ folder.

              1 Reply Last reply Reply Quote 0
              • Y Offline
                yo-less Module Developer
                last edited by Aug 30, 2016, 9:39 PM

                As the calender.js file takes over the days “TODAY” and “TOMORROW” it was actually quite easy to add another definition to take over the “DAYAFTERTOMORROW”.

                I’ve changed the ‘calender.js’ code to look like this:

                ...
                if (event.fullDayEvent) {
                	if (event.today) {
                		timeWrapper.innerHTML = this.translate("TODAY");
                	} else if (event.startDate - now < one_day && event.startDate - now > 0) {
                		timeWrapper.innerHTML = this.translate("TOMORROW");
                	} else if (event.startDate - now < 2*one_day && event.startDate - now > 0) {
                		timeWrapper.innerHTML = this.translate("DAYAFTERTOMORROW");
                	} else {
                ...
                

                And I’ve adjusted /MagicMirror/translations/de.json to look like this

                ...
                	"TOMORROW": "Morgen",
                	"DAYAFTERTOMORROW": "Übermorgen",
                	"RUNNING": "noch",
                ...
                

                Which led to this:
                0_1472593100725_termine.png

                Great, just what I needed… :)

                I’m aware that this will be overwritten by any update I might perform, but I can’t think of any other way to implement this feature.

                1 Reply Last reply Reply Quote 1
                • M Offline
                  MichMich Admin
                  last edited by Aug 31, 2016, 9:34 AM

                  Feel free to send a pull request so anyone can benefit from this. :)

                  1 Reply Last reply Reply Quote 0
                  • Y Offline
                    yo-less Module Developer
                    last edited by Aug 31, 2016, 12:19 PM

                    @MichMich Well, I sort of assumed that a change that somehow breaks the calendar for any other language than German would probably not be merged into the develop branch ^^. But I can have it depend on an if-condition and we can take a look at whether it makes sense to include it in the calendar, will submit a pull request and we’ll take it from there.

                    1 Reply Last reply Reply Quote 1
                    • M Offline
                      MichMich Admin
                      last edited by Sep 1, 2016, 7:48 AM

                      The PR was just merged into the develop branch. I’ve added a translation for Dutch as well.

                      1 Reply Last reply Reply Quote 1
                      • 1
                      • 2
                      • 1 / 2
                      1 / 2
                      • First post
                        1/12
                        Last post
                      Enjoying MagicMirror? Please consider a donation!
                      MagicMirror created by Michael Teeuw.
                      Forum managed by Sam, technical setup by Karsten.
                      This forum is using NodeBB as its core | Contributors
                      Contact | Privacy Policy