HISE Logo Forum
    • Categories
    • Register
    • Login

    Calculating "days left" between two dates

    Scheduled Pinned Locked Moved General Questions
    8 Posts 4 Posters 447 Views
    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.
    • Gabor.KG
      Gabor.K
      last edited by

      Hi everyone,

      I am currently building a 14-day demo mode into my plugin.
      Is there a quick way to calculate "days left" between two dates in this format: 20240326?

      I am trying to avoid feeding every month, and the number of days for each month in there, and then calculating everything separately for the year, month, and days.

      Of course, if there is no other choice, I will do it, but if there is a more elegant way, that would be awesome. I could not find anything in the API for this task.

      Thanks a lot!
      Gábor

      1 Reply Last reply Reply Quote 0
      • d.healeyD
        d.healey
        last edited by

        I have a video on Patreon that shows this exact thing

        Just a moment...

        favicon

        (www.patreon.com)

        Libre Wave - Freedom respecting instruments and effects
        My Patreon - HISE tutorials
        YouTube Channel - Public HISE tutorials

        Gabor.KG clevername27C 2 Replies Last reply Reply Quote 1
        • Gabor.KG
          Gabor.K @d.healey
          last edited by

          @d-healey Oh, great! Thanks a lot!

          A 1 Reply Last reply Reply Quote 0
          • A
            aaronventure @Gabor.K
            last edited by

            @Gabor-K Engine.getSystemTime() returns a standardised format string that you can do string operations on.

            You might also want to consider calling with get some of the online clocks, as system time can be manipulated by simply rewinding the system clock.

            d.healeyD 1 Reply Last reply Reply Quote 0
            • d.healeyD
              d.healey @aaronventure
              last edited by d.healey

              @aaronventure I think the date class is better for this use case.

              5f26a27d-cf7f-4670-b42e-becdfb6e2726-image.png

              Libre Wave - Freedom respecting instruments and effects
              My Patreon - HISE tutorials
              YouTube Channel - Public HISE tutorials

              A 1 Reply Last reply Reply Quote 0
              • A
                aaronventure @d.healey
                last edited by

                @d-healey I think the format is the same, the date class has some methods with calculations already done. OP will still have to do string operations. The most interesting one is to find out the day of the year.

                inline function getDayOfYear()
                {
                    // Get the current time in ISO 8601 format without milliseconds
                    local dateTimeISO8601 = Date.getSystemTimeISO8601(false);
                    
                    // Extract year, month, and day from the ISO 8601 format
                    local year = parseInt(dateTimeISO8601.substring(0, 4));
                    local month = parseInt(dateTimeISO8601.substring(4, 6));
                    local day = parseInt(dateTimeISO8601.substring(6, 8));
                    
                    // Initialize an array to hold the number of days for each month
                    local daysInMonth = [31, isLeapYear(year) ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
                    
                    // Calculate the day of the year
                    local dayOfYear = day;
                    for( i = 0; i < month - 1; ++i) // Subtract 1 because array is zero-based and months are 1-based
                    {
                        dayOfYear += daysInMonth[i];
                    }
                    
                    return dayOfYear;
                }
                
                // Function to check if a year is a leap year
                inline function isLeapYear(year)
                {
                    return (year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0));
                }
                
                d.healeyD 1 Reply Last reply Reply Quote 1
                • d.healeyD
                  d.healey @aaronventure
                  last edited by

                  @aaronventure said in Calculating "days left" between two dates:

                  The most interesting one is to find out the day of the year.

                  I agree it's interesting to do, instead of substring you can probably just use split on the - character. But why does he need to know the day?

                  This is my update checker function. It checks if 7 days have passed since the last check and if so it performs a new check and updates the saved timestamp - https://codeberg.org/LibreWave/Rhapsody/src/branch/main/Scripts/UpdateChecker.js#L95

                  Libre Wave - Freedom respecting instruments and effects
                  My Patreon - HISE tutorials
                  YouTube Channel - Public HISE tutorials

                  1 Reply Last reply Reply Quote 0
                  • clevername27C
                    clevername27 @d.healey
                    last edited by clevername27

                    I have a video on Patreon that shows this exact thing

                    @d-healey LOL of course you do.

                    1 Reply Last reply Reply Quote 1
                    • First post
                      Last post

                    17

                    Online

                    1.7k

                    Users

                    11.8k

                    Topics

                    102.6k

                    Posts