Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

base-60 adition 1

Status
Not open for further replies.

BigBadDave

Programmer
May 31, 2001
1,069
EU
Does any body know how I can add up numbers using base-60?

I need it to add up time eg:

1:40 + 1:30 = 3:10

NOT:

1.4 + 1.3 = 2.7

any insight welcome Regards

Big Bad Dave

logo.gif


davidbyng@hotmail.com
 

Hmmm.. here's my 2 cents worth...

You have to choose whether to:

a. convert all values to a common unit, such as minutes, perform the addition, then unconvert back to a divided-by-60 format for output.

OR

b. do some real ugly arithmetic (72 that's 12 plus carry one of the sixties...)

OR

c. write your own general purpose math-like object that eats up base-60 nunmbers


I am not aware of real math-type nice objects that you feed streams of data and operators to and the act on it as if it were whatever base you suggest.

I would have a peek at sample javascripts available from site that have 'unit of measurement conversion' scripts .. they may have some tricks.

Alas, in the end, a programmer is always on his own.
 
Have you tried something like:
to ad var a and var b:
Code:
var c = parseInt(a,60) + parseInt(b,60);
Of course this will only round it up to the integer. If you always have the same number of characters after the decimal point you might try something like:
Code:
var d = 2;   // number of points after the decimal,
// must be greater than 0
var aa = a*60*d;
var bb + b*60*d;
var c = (parseInt(aa,60)+ parseInt(bb*60))/d;
[code]
This will probably need some fine tuning.
 
This is what i've got so far :

<script language=&quot;JavaScript&quot; type=&quot;text/JavaScript&quot;>
<!--
function timeCalc() {
var t1 = document.calc.time1.value.split(':');
var minutes1 = eval(t1[0]) * 60 + eval(t1[1]);
var time1t = minutes1 * 60 + eval(t1[2]);

var t2 = document.calc.time2.value.split(':');
var minutes2 = eval(t2[0]) * 60 + eval(t2[1]);
var time2t = minutes2 * 60 + eval(t2[2]);

var seconds = time1t + time2t;

var addDate = new Date();
var hours = seconds/360;
addDate.setHours(hours);
seconds -= addDate.getHours() * 3600;
var minutes = seconds/60;
addDate.setMinutes(minutes);
seconds -= addDate.getMinutes() * 60;
addDate.setSeconds(seconds);

document.calc.timeOut.value = addDate.getHours() + ':' + addDate.getMinutes() + ':' + addDate.getSeconds();
}
//-->
</script>

<form name=&quot;calc&quot;>
<input type=&quot;text&quot; name=&quot;time1&quot; value=&quot;00:00:00&quot;> +
<input type=&quot;text&quot; name=&quot;time2&quot; value=&quot;00:00:00&quot;><br />
<input type=&quot;text&quot; name=&quot;timeOut&quot;>
<input type=&quot;button&quot; value=&quot;=&quot; onclick=&quot;timeCalc();&quot;>
</form>

The minutes and seconds work fine, but the hours bit dosn't seem to work. Any ideas?? Regards

Big Bad Dave

logo.gif


davidbyng@hotmail.com
 
When we do this kind of thing where I work, we convert to minutes (and seconds, if necessary), do the math, then convert back. We also add error checking in case someone is viewing the page running the script from before midnight to after midnight, and add the appropriate number of minutes or seconds if the ending time is smaller than the start. To convert the time back to hours and minutes, you first divide the total time by 60 to get hours, then do modulus division to get the number of minutes:

//in minutes
var totaltime;

var totalminutes = totaltime % 60;
var totalhours = Math.floor(totaltime / 60);

 
The JavaScript Date object will do nearly all of this for you if you're lazy enough to let it. (I am.)

Code:
  var gDateZero = &quot;January 01, 1970 &quot;
  var gGMT      = &quot; GMT&quot;
//
// (using the Beginning of Time and GMT/UTC lets us use
//    Date methods without adjusting for current date.
//    easier debugging, etc ...)
//
function timeCalc() {
  Time1   = new Date(gDateZero + document.calc.time1.value + gGMT)
  Time2   = new Date(gDateZero + document.calc.time2.value + gGMT)
  TimeTot = new Date(Time1.valueOf() + Time2.valueOf())
  sTot    = TimeTot.toGMTString()

  document.calc.timeOut.value = sTot.substr(sTot.length-12,8)
}

If you expect overflow check TimeTot.getUTCDate() and adjust total hours (or display # of days ...)if greater than 1.

The wheel wasn't invented in a day (whatever that means).
 
Here is my completed function :

<script language=&quot;JavaScript&quot; type=&quot;text/JavaScript&quot;>
<!--
function timeCalc() {
var arr1 = document.calc.time1.value.split(':');
var arr2 = document.calc.time2.value.split(':');
var hour = eval(arr1[0]) + eval(arr2[0]);
var hours1 = 0;
var minutes1 = eval(arr1[1]);
var seconds1 = eval(arr1[2]);
var hours2 = 0;
var minutes2 = eval(arr2[1]);
var seconds2 = eval(arr2[2]);
while(seconds1>60) {
minutes1++;
seconds1 -= 60;
}
while(minutes1>60) {
hours1++;
minutes1 -= 60;
}
while(seconds2>60) {
minutes2++;
seconds2 -= 60;
}
while(minutes2>60) {
hours2++;
minutes2 -= 60;
}
var hours = hour + hours1 + hours2
var time1 = new Date(&quot;January 01, 1970 0:&quot; + minutes1 + &quot;:&quot; + seconds1 + &quot; GMT&quot;);
var time2 = new Date(&quot;January 01, 1970 0:&quot; + minutes2 + &quot;:&quot; + seconds2 + &quot; GMT&quot;);
var timeOut = new Date(time1.valueOf() + time2.valueOf());
document.calc.timeOut.value = timeOut.getHours() + hours + ':' + timeOut.getMinutes() + ':' + timeOut.getSeconds();
}
//-->
</script>

<form name=&quot;calc&quot;>
<input type=&quot;text&quot; size=&quot;10&quot; name=&quot;time1&quot; value=&quot;2:57:22&quot;> +
<input type=&quot;text&quot; size=&quot;10&quot; name=&quot;time2&quot; value=&quot;60:30:57&quot;>
<input type=&quot;button&quot; value=&quot;=&quot; onclick=&quot;timeCalc();&quot;>
<input type=&quot;text&quot; size=&quot;10&quot; name=&quot;timeOut&quot;>
</form>

Thanks to everyone that helped Regards

Big Bad Dave

logo.gif


davidbyng@hotmail.com
 
Calling timeOut.getMinutes()may cause problems in those parts of the world using half-hour adjustments from &quot;standard&quot; time when figuring local time. (Afghanistan, India, parts of Australia, others ...)

Changing to timeOut.getUTCMinutes()will avoid problems for JavaScripts configured for those time zones.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top