0

I saw a number of examples. Some of these were even using jQuery. What I am looking for is the simplest way to change:

var diffMins = 99;

into something like "1 hour 39 minutes"

I do this only once in my application so I am not looking to rely on any functions. I would just like to know if the latest java offers a very simple way to do this.

Please note that in the title I use the word Javascript and in the question tag I use the word Javascript. I'm surprised now that two people are asking if it is Java :-(

Alan2
  • 23,493
  • 79
  • 256
  • 450

3 Answers3

1

Use mod operator (http://www.w3schools.com/js/js_operators.asp)

parseInt(99/60) + " hour " + (99%60) + " + minutes"

http://jsbin.com/xisuqefona/1/edit

Audrey
  • 148
  • 1
  • 8
  • My VS2013 compiler complains about parseInt – Alan2 Mar 28 '15 at 09:23
  • Error 2 Supplied parameters do not match any signature of call target: Could not apply type 'string' to argument 1 which is of type 'number'. – Alan2 Mar 28 '15 at 09:26
  • @Alan—there's a syntax error: `" + minutes"` should be `+ " minutes"`. – RobG Mar 28 '15 at 10:13
  • @Audrey—there is a syntax error. A radix to [*parseInt*](http://ecma-international.org/ecma-262/5.1/#sec-15.1.2.2) is only sensible if the argument is a string. Please reference relevant standards (e.g. ES5) rather than sites like [*w3schools*](http://www.w3fools.com). – RobG Mar 28 '15 at 10:16
  • Removed my comment to add radix, the original answer works just fine without radix. Tested in both Chrome and Firefox. IE might be a different beast if anyone can verify. – Audrey Mar 29 '15 at 02:28
0

Floor for hours and mod60 for minutes:

var diffMins = 99;    
Math.floor(diffMins/60) +" Hour "+ diffMins%60 + " Minutes.";

Result:

4EACH
  • 2,132
  • 4
  • 20
  • 28
0

Try using /, % and | operators:

((99/60) | 0) + " hour(s) " + (99%60) + " minute(s)"

var diffMins = 99;
var text = ((diffMins / 60) | 0) + " hour(s) " + (diffMins % 60) + " minute(s)";
alert(text);