Site Logo
RanAwaySuccessfully's Website

Separating and Joining Time (seconds, minutes, hours, etc.)

Time is measured in non-uniform units, which can make joining together a lot of them together or separating them into one a slightly more complicated task that it should be, though those of you in the United States also need to deal with the U.S. customary units, so maybe you're more used to it.

Either way, there's a simple method for separating time and joining it, although it sadly only works for commerical time without some adjustments. Commercial time means, months count as having 30 days and years count as having 360 days (12 times 30). What's that easy method? Using division of course! But also the power of a little other secret: the modulo operation.

Separating Time

Well, let's say we look at a database that stores time in milliseconds and want to know the rough estimate time those milliseconds mean. To convert it to a more humanly readable format, you'd need to separate each one into seconds, minutes, hours, etc. Using division and modulo is an easy way to do it. Let's look at an example:

We want to convert 3456 seconds into a more humanly-readable time. Since every 60 seconds is a minute, we can divide 3456 by 60 to get 57.6 minutes, but now we have .6 of a minute in our result. What in the world is .6 of a minute? How about just leaving what can't be converted into full minutes in seconds (ex. 2 minutes and 30 seconds rather than 2.5 minutes)? This is where the modulo operation comes in, it's like a standard division but the result is the remainder of the equation. Say we have 5 divided by 3, if we stop before getting to a decimal place, we end up with 1 as our result and 2 as our remainder. That remainder is the result of a modulo operation: if we do 5 modulo by 3, our result is 2.

Remember that in order to have a remainder we need to stop the division before we get to any decimal places, so instead of 57.6 minutes we have 57. Then, we do 3456 mod 60 and we get 36. So that means 3456 seconds actually means 57 minutes and 36 seconds. Almost an hour! Here's an (poorly made) image to better help illustrate the point:

We can keep doing this process for a while, getting the number of seconds, then minutes, and so forth.

Joining Time

For the reverse process, it's actually very simple, we multiply the number of years by the amount of months in a year, then add the amount of months that was provided, then redo the process again for days and so forth. It's the exact reverse process. Say we have 3 years and 20 months and want to convert it to days, multiply the years by 12 (amount of months in a year) to get 36, add the amount of months and get 56 and then multiply it by 30 (amounts of days in a commercial month) to get 1680 days. It's actually pretty straight forward.

Code Time

Here's an example of this algorithm done in JavaScript (feel free to use it however you like):

function separatingTime(t) {
    var ms = t % 1000;
    ms = parseInt(ms);
    t = Math.floor(t / 1000);
    var s = t % 60;
    t = Math.floor(t / 60);
    var min = t % 60;
    t = Math.floor(t / 60);
    var h = t % 24;
    t = Math.floor(t / 24);
    var d = t % 30;
    t = Math.floor(t / 30);
    var m = t % 12;
    var y = Math.floor(t / 12);
    var r = y + " years, " + m + " months, " + d + " days, " + h + " hours, " + min + " min, " + s + " seconds and " + ms + " milliseconds.";
    return r;
function joiningTime(y, m, d, h, min, s, ms) {
    y *= 12;
    m += y;
    m *= 30;
    d += m;
    d *= 24;
    h += d;
    h *= 60;
    min += h;
    min *= 60;
    s += min;
    s *= 1000;
    ms += s;
    return ms;

And here's a live demo of it. The demo has an additional feature where it lets you decide the unit used for the calculation and the unit you want for the result of the reverse process. It's all thanks to this:

var units = [1, 1000, 60000, 3600000, 86400000, 2592000000, 31104000000];

For the main process, it'll multiply the input by the value of 1 if it's in milliseconds, by 1000 if it's seconds, by 60 thousand (1000 times 60) if it's in minutes, 360 thousand (1000 times 60 times 60) if it's in hours and so forth before the division and modulo operations happens. For the reverse calculation, the value of 1, 1000, 60000, etc. is instead divided by the result after the whole thing happens.

And I think that's it. I'll be honest, this ended up being more simple than I thought it would be.