phone calculator and code calculation return two different answers

So, I perform this calculation on a phone: (((7 - 8)+ 2) %7) and the result is 9.

it’s a calculation to get the next X day of the weeks date. I am specifically getting the date of the next Monday (9th)from the current Sunday. (8th) although intend to use it for other days too.

But when I perform it in my code it returns 0. Why? 

I don’t think the calculation is necessarily the correct one, I’ve been playing around but I am curious as to why it provides two different answers.

  • System.println( (((7 - 8)+ 2) %7) );
    ... delivers 1. And that's the correct result.

    7-8 = -1 +2 = 1 mod 7 = 1
  • The “%” key on your phone’s calculator probably means percentage, not mod as it does in most programming languages.

    When I open my iPhone’s built-in calculator in portrait mode, I see the following keys: C, +/-, %,÷, x, - +, =, ., 0-9 (just like a basic physical calculator)

    If I type 7 - 8 + 2 =, the result is 1. If now press % the display shows 0.01, but if I press 7 =, the display shows 9, same result as you got. I didn’t use parens, but I figure that pressing = after 2 should be equivalent.

    I tried this with other numbers in pace of the final 7 and it appears that the answer is always 2 + X (where X is the number I typed).

    I don’t really understand this behavior, except that obviously the calculator is taking the 2nd last number that was typed, and adding it to the last number (but why?)

    I do think the % is being totally ignored here. You’ll get the same answer if you type 7 - 8 + 2 = 7

    So in general, the pattern is:

    X + Y = Z results in Y + Z. Same with X + Y = % Z

    X - Y = Z results in -Y + Z. Same with X - Y = % Z

    I tried the same thing in the Windows calculator (in standard mode) and got the same results. So it must be how basic physical calculators are supposed to work. Honestly, basic physical calculators suck for anything math, engineering/physics or programming-related, and same with the apps which emulate them.

    If you want to play with arithmetic in programming outside of Monkey C, I would suggest using python or javascript (you can use the developer console in Chrome/Firefox or install node).

  • I see, I guess I’ll have to keep fiddling with the code then, it’s confusing working with time in code when maths isn’t your strongest point.

  • "I am specifically getting the date of the next Monday (9th)from the current Sunday. (8th) although intend to use it for other days too."

    I think you can break this problem down into 3 steps:

    1) Determine the current date and day of week

    2) Get the desired "starting date" (in this case, Sunday of the current week)

    3) Determine the date of Monday (in this case, 1 day from Sunday)

    I'm not sure how you will define "other days" (in relation to today), so I will give an example for the problem as stated above.

    function nextMonday() {
        var now = Time.now();
        System.println("The current moment is " + now.value());
        var today = Gregorian.info(now, Time.FORMAT_SHORT);
        System.println("The current date is " +
            today.year + "-" +
            today.month.format("%02d") + "-" +
            today.day.format("%02d") +
            ". The day of week is " +
            today.day_of_week + " (Sunday = 1, Saturday = 7)");
    
        // Determine the number of days between now and this week's Sunday,
        // assuming that the first day of the week is Sunday
        // e.g. if today is Saturday (7), then deltaSunday will be -6.
        var deltaSunday = 1 - today.day_of_week;
        
        // Get a moment of time for this week's Sunday
        var sundayMoment = now.add(new Time.Duration(deltaSunday * Gregorian.SECONDS_PER_DAY));
        System.println("The moment for this week's Sunday is " + sundayMoment.value());
    
        // Monday is 1 day from Sunday
        var nextMondayMoment = sundayMoment.add(new Time.Duration(1 * Gregorian.SECONDS_PER_DAY));
        System.println("The moment for Monday is " + nextMondayMoment.value());
        var nextMonday = Gregorian.info(nextMondayMoment, Time.FORMAT_SHORT);
        System.println("The date for Monday is " +
            nextMonday.year + "-" +
            nextMonday.month.format("%02d") + "-" +
            nextMonday.day.format("%02d") +
            ". The day of week is " +
            nextMonday.day_of_week + " (Sunday = 1, Saturday = 7)");
    }

    When I run this code today (Sunday, October 8), the following output is produced:

    The current moment is ...
    The current date is 2023-10-08. The day of week is 1 (Sunday = 1, Saturday = 7)
    The moment for this week's Sunday is ...
    The moment for Monday is ...
    The date for Monday is 2023-10-09. The day of week is 2 (Sunday = 1, Saturday = 7)

  • Ooh, that’s very helpful, thank you. Currently I’ve determined the days of the weeks as integers from 1-7 where 1 is Sunday, 2 is Monday, etc up to 7 on Saturday.

    Would I be able to modify the Monday code to calculate any provided day of the users choice? 

  • Here's an example of determining the data of the next occurrence of a given day of the week. I'm assuming you don't want any dates in the past - e.g. if today is Wednesday and the input is Monday, I assume you want a date 5 days in the future as opposed to 2 days in the past. Also, if today is Wednesday and the input is Wednesday, the code will return today. If you have different requirements, you should be able to make the appropriate changes.

        // weekday: 1 = Sunday, ..., 7 = Saturday
        function nextWeekday(weekday as Lang.Number) {
            var nowMoment = Time.now();
            System.println("The current moment is " + nowMoment.value());
            var nowInfo = Gregorian.info(nowMoment, Time.FORMAT_SHORT);
            System.println("The current date is " +
                nowInfo.year + "-" +
                nowInfo.month.format("%02d") + "-" +
                nowInfo.day.format("%02d") +
                ". The day of week is " +
                nowInfo.day_of_week + " (Sunday = 1, Saturday = 7)");
    
            // Determine the number of days between today and the *next* occurence of the given weekday
            if (weekday < nowInfo.day_of_week) {
                weekday += 7;
            }
            var deltaWeekday = weekday - nowInfo.day_of_week;
    
            var weekdayMoment = nowMoment.add(new Time.Duration(deltaWeekday * Gregorian.SECONDS_PER_DAY));
            System.println("The moment for the desired weekday is " + weekdayMoment.value());
            var weekdayInfo = Gregorian.info(weekdayMoment, Time.FORMAT_SHORT);
            System.println("The date for the desired weekday is " +
                weekdayInfo.year + "-" +
                weekdayInfo.month.format("%02d") + "-" +
                weekdayInfo.day.format("%02d") +
                ". The day of week is " +
                weekdayInfo.day_of_week + " (Sunday = 1, Saturday = 7)");
        }

    Test cases:

    nextWeekday(1);
    nextWeekday(2);
    nextWeekday(3);
    nextWeekday(4);
    nextWeekday(5);
    nextWeekday(6);
    nextWeekday(7);

    Output:

    The current moment is 1696865163
    The current date is 2023-10-09. The day of week is 2 (Sunday = 1, Saturday = 7)
    The moment for the desired weekday is 1697383563
    The date for the desired weekday is 2023-10-15. The day of week is 1 (Sunday = 1, Saturday = 7)

    The current moment is 1696865163
    The current date is 2023-10-09. The day of week is 2 (Sunday = 1, Saturday = 7)
    The moment for the desired weekday is 1696865163
    The date for the desired weekday is 2023-10-09. The day of week is 2 (Sunday = 1, Saturday = 7)

    The current moment is 1696865163
    The current date is 2023-10-09. The day of week is 2 (Sunday = 1, Saturday = 7)
    The moment for the desired weekday is 1696951563
    The date for the desired weekday is 2023-10-10. The day of week is 3 (Sunday = 1, Saturday = 7)

    The current moment is 1696865163
    The current date is 2023-10-09. The day of week is 2 (Sunday = 1, Saturday = 7)
    The moment for the desired weekday is 1697037963
    The date for the desired weekday is 2023-10-11. The day of week is 4 (Sunday = 1, Saturday = 7)

    The current moment is 1696865163
    The current date is 2023-10-09. The day of week is 2 (Sunday = 1, Saturday = 7)
    The moment for the desired weekday is 1697124363
    The date for the desired weekday is 2023-10-12. The day of week is 5 (Sunday = 1, Saturday = 7)


    The current moment is 1696865163

    The current date is 2023-10-09. The day of week is 2 (Sunday = 1, Saturday = 7)
    The moment for the desired weekday is 1697210763
    The date for the desired weekday is 2023-10-13. The day of week is 6 (Sunday = 1, Saturday = 7)

    The current moment is 1696865163
    The current date is 2023-10-09. The day of week is 2 (Sunday = 1, Saturday = 7)
    The moment for the desired weekday is 1697297163
    The date for the desired weekday is 2023-10-14. The day of week is 7 (Sunday = 1, Saturday = 7)

  • You are good at what you do, thank you for your assistance flowstate

  • Hey Flowstate, so the calculations work but how might I give off an alert in the background when the widget is closed to tell the user the time now equals the user provided date?