Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3398226
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T04:37:24+00:00 2026-05-18T04:37:24+00:00

Trying to emulate the rollover of a 24 hour clock by hand (with math

  • 0

Trying to emulate the rollover of a 24 hour clock by hand (with math vs. using the timespan classes). The incrementing part was easy to figure out how to roll over from 23:00 to 0:00 and from, but getting it to go the other way is turning out to be really confusing. Here’s what I have so far:

static void IncrementMinute(int min, int incr)
{
    int newMin = min + incr,
                hourIncrement = newMin / 60;

    //increment or decrement the hour
    if((double)newMin % 60 < 0 && (double)newMin % 60 > -1)
        hourIncrement = -1;

    Console.WriteLine("Hour increment is {0}: ", hourIncrement);
}

The problem that im finding is when going backwards, if the the modulus of is between numbers, it will not decrement correctly. Example: it is 12:00 and you subtract 61 minutes, we know the time would be 10:59 as the hour should roll back 1 hour for going from 12:00 to 11:59, then back again for going from 11:00 to 10:59. Unfortunately the way im calculating it: newMin % 60 in this case, only grabs the first hour rollback, but since the second rollback is technically -1.0166 as a remainder, and since mod only returns a whole number, its rounding off. Im sure im missing some basic math here, but could someone help me out?

EDIT: I’ve written this a number of ways long and short. Some are closer than others, but I know this is simpler than it seems. I know this one seems kinda “wtf was he doing”, but you should be able to see basically what Im trying to do. Incrementing a clock and having it rollover from 23:59 to 0:00 is easy. Going backwards has proven to be not so easy.

OK, here’s the incrementMinute with the rollover. Simple. But try to go backwards. Doesn’t work.

static void IncrementMinute(int min, int incr)

        {
            int newMin = min + incr,
                hourIncrement = newMin / 60;

            min = newMin % 60;

            Console.WriteLine("The new minute is {0} and the hour has incremented by {1}", min, hourIncrement);
        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T04:37:25+00:00Added an answer on May 18, 2026 at 4:37 am

    I’d go for something a bit simpler

    public class Clock
    {
        public const int HourPerDay = 24;
        public const int MinutesPerHour = 60;
        public const int MinutesPerDay = MinutesPerHour * HourPerDay;
    
        private int totalMinutes;
    
        public int Minute
        {
            get { return this.totalMinutes % MinutesPerHour; }
        }
    
        public int Hour
        {
            get { return this.totalMinutes / MinutesPerHour; }
        }
    
        public void AddMinutes(int minutes)
        {
            this.totalMinutes += minutes;
            this.totalMinutes %= MinutesPerDay;
            if (this.totalMinutes < 0)
                this.totalMinutes += MinutesPerDay;
        }
    
        public void AddHours(int hours)
        {
            this.AddMinutes(hours * MinutesPerHour);
        }
    
        public override string ToString()
        {
            return string.Format("{0:00}:{1:00}", this.Hour, this.Minute);
        }
    }
    

    Sample usage :

    new Clock().AddMinutes(-1);    // 23:59
    new Clock().AddMinutes(-61);   // 22:59
    new Clock().AddMinutes(-1441); // 23:59
    new Clock().AddMinutes(1);     // 00:01
    new Clock().AddMinutes(61);    // 01:01
    new Clock().AddMinutes(1441);  // 00:01
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to emulate a system of type classes in F#; I would
I'm getting in trouble. I'm trying to emulate the call Application.Run using Application.DoEvents... this
I'm trying to emulate the Safari / Chrome tabs using CSS (without using tables).
I'm trying to emulate some pseudo-classes and attribute selectors in Internet Explorer 6 and
I am trying to emulate a software keyboard using an Activity with Theme.Dialog. The
I am trying to emulate some java.lang and java.io classes, e.g. OutputStream within GWT.
I am trying to emulate click events in Swing using following code: event =
I'm using String.format() in Java trying to emulate the printf() control channel available in
Is it possible to run a MySQL query using jQuery? I'm trying to emulate
I'm actually trying to emulate the linux kernel using Qemu and busybox. So far

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.